2019-10-13 22:51:40 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-19 15:53:00 +00:00
|
|
|
"math/big"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-02-19 17:55:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2020-02-19 17:55:59 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2019-10-15 05:47:56 +00:00
|
|
|
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
|
|
|
|
}
|
2019-10-15 05:47:56 +00:00
|
|
|
// >>> w[r] <<< + wFunction(totalPowerAtTipset(ts)) * 2^8 + (wFunction(totalPowerAtTipset(ts)) * len(ts.blocks) * wRatio_num * 2^8) / (e * wRatio_den)
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2019-10-15 13:04:01 +00:00
|
|
|
var out = new(big.Int).Set(ts.Blocks()[0].ParentWeight.Int)
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2019-10-15 05:47:56 +00:00
|
|
|
// >>> wFunction(totalPowerAtTipset(ts)) * 2^8 <<< + (wFunction(totalPowerAtTipset(ts)) * len(ts.blocks) * wRatio_num * 2^8) / (e * wRatio_den)
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2020-02-19 17:55:59 +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
|
|
|
}
|
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
|
|
|
|
2019-10-15 05:47:56 +00:00
|
|
|
// (wFunction(totalPowerAtTipset(ts)) * len(ts.blocks) * wRatio_num * 2^8) / (e * wRatio_den)
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2019-11-01 01:43:22 +00:00
|
|
|
eWeight := big.NewInt((log2P * int64(len(ts.Blocks())) * build.WRatioNum) << 8)
|
2019-11-09 00:18:32 +00:00
|
|
|
eWeight.Div(eWeight, big.NewInt(int64(build.BlocksPerEpoch*build.WRatioDen)))
|
2019-10-15 13:04:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// todo: dedupe with state manager
|
|
|
|
func (cs *ChainStore) call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) {
|
|
|
|
bstate := ts.ParentState()
|
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
r := NewChainRand(cs, ts.Cids(), ts.Height())
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2020-02-25 20:54:58 +00:00
|
|
|
vmi, err := vm.NewVM(bstate, ts.Height(), r, builtin.SystemActorAddr, cs.bs, cs.vmcalls)
|
2019-10-13 22:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to set up vm: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-03-18 20:45:37 +00:00
|
|
|
if msg.GasLimit == 0 {
|
|
|
|
msg.GasLimit = 10000000000
|
2019-10-13 22:51:40 +00:00
|
|
|
}
|
|
|
|
if msg.GasPrice == types.EmptyInt {
|
|
|
|
msg.GasPrice = types.NewInt(0)
|
|
|
|
}
|
|
|
|
if msg.Value == types.EmptyInt {
|
|
|
|
msg.Value = types.NewInt(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fromActor, err := vmi.StateTree().GetActor(msg.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("call raw get actor: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Nonce = fromActor.Nonce
|
|
|
|
|
|
|
|
// TODO: maybe just use the invoker directly?
|
2020-03-25 11:29:35 +00:00
|
|
|
// TODO: use signed message length for secp messages
|
2020-03-25 19:13:09 +00:00
|
|
|
ret, err := vmi.ApplyMessage(ctx, msg)
|
2019-10-13 22:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("apply message failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.ActorErr != nil {
|
|
|
|
log.Warnf("chain call failed: %s", ret.ActorErr)
|
|
|
|
}
|
|
|
|
return &ret.MessageReceipt, nil
|
|
|
|
}
|