2019-10-13 22:51:40 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/filecoin-project/go-lotus/build"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
|
|
|
"golang.org/x/xerrors"
|
2019-10-15 05:47:56 +00:00
|
|
|
"math/big"
|
2019-10-13 22:51:40 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
ret, err := cs.call(ctx, &types.Message{
|
|
|
|
From: actors.StorageMarketAddress,
|
|
|
|
To: actors.StorageMarketAddress,
|
|
|
|
Method: actors.SPAMethods.GetTotalStorage,
|
|
|
|
}, ts)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, xerrors.Errorf("failed to get total power from chain: %w", err)
|
|
|
|
}
|
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
return types.EmptyInt, xerrors.Errorf("failed to get total power from chain (exit code %d)", ret.ExitCode)
|
|
|
|
}
|
2019-10-15 05:47:56 +00:00
|
|
|
log2P := int64(0)
|
|
|
|
tpow := types.BigFromBytes(ret.Return)
|
|
|
|
if tpow.GreaterThan(zero) {
|
|
|
|
log2P = int64(tpow.BitLen() - 1)
|
|
|
|
}
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2019-10-15 13:04:01 +00:00
|
|
|
out.Add(out, big.NewInt(log2P*256))
|
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-10-15 05:47:56 +00:00
|
|
|
wRatioNum := int64(1)
|
|
|
|
wRatioDen := 2
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2019-10-15 05:47:56 +00:00
|
|
|
eWeight := big.NewInt((log2P * int64(len(ts.Blocks())) * wRatioNum) << 8)
|
|
|
|
eWeight.Div(eWeight, big.NewInt(int64(build.BlocksPerEpoch*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-10-15 04:33:29 +00:00
|
|
|
r := NewChainRand(cs, ts.Cids(), ts.Height(), nil)
|
2019-10-13 22:51:40 +00:00
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
vmi, err := vm.NewVM(bstate, ts.Height(), r, actors.NetworkAddress, cs.bs)
|
2019-10-13 22:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to set up vm: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.GasLimit == types.EmptyInt {
|
|
|
|
msg.GasLimit = types.NewInt(10000000000)
|
|
|
|
}
|
|
|
|
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?
|
|
|
|
ret, err := vmi.ApplyMessage(ctx, msg)
|
|
|
|
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
|
|
|
|
}
|