2021-09-02 16:07:23 +00:00
|
|
|
package filcns
|
2019-10-13 22:51:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-19 15:53:00 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-09-02 16:07:23 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
"golang.org/x/xerrors"
|
2020-09-16 05:00:00 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
big2 "github.com/filecoin-project/go-state-types/big"
|
2021-09-02 16:07:23 +00:00
|
|
|
|
|
|
|
bstore "github.com/filecoin-project/lotus/blockstore"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2021-09-02 16:07:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
|
2020-02-19 17:55:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
2021-09-02 16:07:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-10-13 22:51:40 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 05:47:56 +00:00
|
|
|
var zero = types.NewInt(0)
|
|
|
|
|
2021-09-02 16:07:23 +00:00
|
|
|
func Weight(ctx context.Context, stateBs bstore.Blockstore, ts *types.TipSet) (types.BigInt, error) {
|
2019-10-15 04:33:29 +00:00
|
|
|
if ts == nil {
|
|
|
|
return types.NewInt(0), nil
|
|
|
|
}
|
2020-06-24 15:11:26 +00:00
|
|
|
// >>> 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
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
var out = new(big.Int).Set(ts.ParentWeight().Int)
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2020-06-24 15:11:26 +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
|
|
|
|
2022-11-24 16:32:27 +00:00
|
|
|
var tpow big2.Int
|
2020-02-19 17:55:59 +00:00
|
|
|
{
|
2021-09-02 16:07:23 +00:00
|
|
|
cst := cbor.NewCborStore(stateBs)
|
2020-09-14 22:43:12 +00:00
|
|
|
state, err := state.LoadStateTree(cst, ts.ParentState())
|
2020-02-19 17:55:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("load state tree: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-23 06:18:52 +00:00
|
|
|
act, err := state.GetActor(power.Address)
|
2020-02-19 17:55:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("get power actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:07:23 +00:00
|
|
|
powState, err := power.Load(store.ActorStore(ctx, stateBs), act)
|
2020-09-16 05:00:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("failed to load power actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
claim, err := powState.TotalPower()
|
|
|
|
if err != nil {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("failed to get total power: %w", err)
|
2020-02-19 17:55:59 +00:00
|
|
|
}
|
2020-09-16 05:00:00 +00:00
|
|
|
|
|
|
|
tpow = claim.QualityAdjPower // TODO: REVIEW: Is this correct?
|
2019-10-13 22:51:40 +00:00
|
|
|
}
|
2020-02-19 17:55:59 +00:00
|
|
|
|
2019-10-15 05:47:56 +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-15 05:47:56 +00:00
|
|
|
}
|
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
|
|
|
|
2020-06-24 15:11:26 +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
|
|
|
|
2020-06-24 17:44:05 +00:00
|
|
|
totalJ := int64(0)
|
2020-06-23 14:59:44 +00:00
|
|
|
for _, b := range ts.Blocks() {
|
|
|
|
totalJ += b.ElectionProof.WinCount
|
|
|
|
}
|
|
|
|
|
|
|
|
eWeight := big.NewInt((log2P * build.WRatioNum))
|
|
|
|
eWeight = eWeight.Lsh(eWeight, 8)
|
2020-06-24 17:44:05 +00:00
|
|
|
eWeight = eWeight.Mul(eWeight, new(big.Int).SetInt64(totalJ))
|
2020-06-23 14:59:44 +00:00
|
|
|
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
|
|
|
}
|