lotus/chain/store/weight.go

75 lines
2.4 KiB
Go
Raw Normal View History

2019-10-13 22:51:40 +00:00
package store
import (
"context"
"math/big"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/types"
big2 "github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/power"
cbor "github.com/ipfs/go-ipld-cbor"
2019-10-13 22:51:40 +00:00
"golang.org/x/xerrors"
)
var zero = types.NewInt(0)
2019-10-13 22:51:40 +00:00
func (cs *ChainStore) Weight(ctx context.Context, ts *types.TipSet) (types.BigInt, error) {
2019-10-15 04:33:29 +00:00
if ts == nil {
return types.NewInt(0), nil
}
// >>> w[r] <<< + wFunction(totalPowerAtTipset(ts)) * 2^8 + (wFunction(totalPowerAtTipset(ts)) * sum(ts.blocks[].ElectionProof.WinCount) * wRatio_num * 2^8) / (e * wRatio_den)
2019-10-13 22:51:40 +00:00
var out = new(big.Int).Set(ts.ParentWeight().Int)
2019-10-13 22:51:40 +00:00
// >>> wFunction(totalPowerAtTipset(ts)) * 2^8 <<< + (wFunction(totalPowerAtTipset(ts)) * sum(ts.blocks[].ElectionProof.WinCount) * wRatio_num * 2^8) / (e * wRatio_den)
2019-10-13 22:51:40 +00:00
tpow := big2.Zero()
{
cst := cbor.NewCborStore(cs.Blockstore())
state, err := state.LoadStateTree(cst, ts.ParentState())
if err != nil {
return types.NewInt(0), xerrors.Errorf("load state tree: %w", err)
}
act, err := state.GetActor(builtin.StoragePowerActorAddr)
if err != nil {
return types.NewInt(0), xerrors.Errorf("get power actor: %w", err)
}
var st power.State
if err := cst.Get(ctx, act.Head, &st); err != nil {
return types.NewInt(0), xerrors.Errorf("get power actor head: %w", err)
}
2020-04-10 12:19:06 +00:00
tpow = st.TotalQualityAdjPower // TODO: REVIEW: Is this correct?
2019-10-13 22:51:40 +00:00
}
log2P := int64(0)
if tpow.GreaterThan(zero) {
log2P = int64(tpow.BitLen() - 1)
2019-10-31 10:54:13 +00:00
} else {
2019-11-09 00:18:32 +00:00
// Not really expect to be here ...
2019-11-01 01:43:22 +00:00
return types.EmptyInt, xerrors.Errorf("All power in the net is gone. You network might be disconnected, or the net is dead!")
}
2019-10-13 22:51:40 +00:00
2019-11-09 00:18:32 +00:00
out.Add(out, big.NewInt(log2P<<8))
2019-10-13 22:51:40 +00:00
// (wFunction(totalPowerAtTipset(ts)) * sum(ts.blocks[].ElectionProof.WinCount) * wRatio_num * 2^8) / (e * wRatio_den)
2019-10-13 22:51:40 +00:00
totalJ := int64(0)
for _, b := range ts.Blocks() {
totalJ += b.ElectionProof.WinCount
}
eWeight := big.NewInt((log2P * build.WRatioNum))
eWeight = eWeight.Lsh(eWeight, 8)
eWeight = eWeight.Mul(eWeight, new(big.Int).SetInt64(totalJ))
eWeight = eWeight.Div(eWeight, big.NewInt(int64(build.BlocksPerEpoch*build.WRatioDen)))
out = out.Add(out, eWeight)
2019-10-13 22:51:40 +00:00
2019-10-15 13:04:01 +00:00
return types.BigInt{Int: out}, nil
2019-10-13 22:51:40 +00:00
}