wip weight function

This commit is contained in:
Łukasz Magiera 2019-10-14 00:51:40 +02:00
parent 007bdfaf42
commit 2ce338e162
10 changed files with 100 additions and 14 deletions

View File

@ -130,6 +130,7 @@ type FullNode interface {
StateWaitMsg(context.Context, cid.Cid) (*MsgWait, error)
StateListMiners(context.Context, *types.TipSet) ([]address.Address, error)
StateListActors(context.Context, *types.TipSet) ([]address.Address, error)
StateTipSetWeight(context.Context, *types.TipSet) (types.BigInt, error)
PaychGet(ctx context.Context, from, to address.Address, ensureFunds types.BigInt) (*ChannelInfo, error)
PaychList(context.Context) ([]address.Address, error)

View File

@ -97,6 +97,7 @@ type FullNodeStruct struct {
StateWaitMsg func(context.Context, cid.Cid) (*MsgWait, error) `perm:"read"`
StateListMiners func(context.Context, *types.TipSet) ([]address.Address, error) `perm:"read"`
StateListActors func(context.Context, *types.TipSet) ([]address.Address, error) `perm:"read"`
StateTipSetWeight func(context.Context, *types.TipSet) (types.BigInt, error) `perm:"read"`
PaychGet func(ctx context.Context, from, to address.Address, ensureFunds types.BigInt) (*ChannelInfo, error) `perm:"sign"`
PaychList func(context.Context) ([]address.Address, error) `perm:"read"`

View File

@ -39,7 +39,7 @@ const AllowableClockDrift = BlockDelay * 2
const ForkLengthThreshold = 100
// Blocks (e)
const BlocksPerRound = 1
const BlocksPerEpoch = 1
// /////
// Proofs / Mining

View File

@ -413,7 +413,7 @@ func IsRoundWinner(ctx context.Context, ts *types.TipSet, ticks []*types.Ticket,
return false, nil, xerrors.Errorf("failed to check power: %w", err)
}
return types.PowerCmp(vrfout, types.BigMul(pow.MinerPower, types.NewInt(build.BlocksPerRound)), pow.TotalPower), vrfout, nil
return types.PowerCmp(vrfout, types.BigMul(pow.MinerPower, types.NewInt(build.BlocksPerEpoch)), pow.TotalPower), vrfout, nil
}
type SignFunc func(context.Context, address.Address, []byte) (*types.Signature, error)

View File

@ -2,7 +2,6 @@ package stmgr
import (
"context"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"

View File

@ -377,10 +377,6 @@ func (cs *ChainStore) ReorgOps(a, b *types.TipSet) ([]*types.TipSet, []*types.Ti
return leftChain, rightChain, nil
}
func (cs *ChainStore) Weight(ts *types.TipSet) uint64 {
return ts.Blocks()[0].ParentWeight.Uint64() + uint64(len(ts.Cids()))
}
func (cs *ChainStore) GetHeaviestTipSet() *types.TipSet {
cs.heaviestLk.Lock()
defer cs.heaviestLk.Unlock()

89
chain/store/weight.go Normal file
View File

@ -0,0 +1,89 @@
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"
)
func (cs *ChainStore) Weight(ctx context.Context, ts *types.TipSet) (types.BigInt, error) {
// w[r+1] = w[r] + wFunction(totalPowerAtTipset(ts)) * 2^8 + (wFunction(totalPowerAtTipset(ts)) * len(ts.blocks) * wRatio_num * 2^8) / (e * wRatio_den)
// wr = wRatio_num(0.5) * 2^8 / wRatio_den(2)
wr := types.NewInt(256)
// wFunction(totalPowerAtTipset(ts)) * 2^8 + (wFunction(totalPowerAtTipset(ts)) * len(ts.blocks) * wr ) / e
// /////
// wFunction(totalPowerAtTipset(ts))
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)
}
totalPowerAtTipsetL2 := types.BigFromBytes(ret.Return).BitLen()
// //////
// w[r] + wFunction(totalPowerAtTipset(ts)) * 2^8
out := types.BigAdd(ts.Blocks()[0].ParentWeight, types.NewInt(uint64(totalPowerAtTipsetL2*256)))
// //////
// (wFunction(totalPowerAtTipset(ts)) * len(ts.blocks) * wr)
eWeight := types.BigMul(types.BigMul(types.NewInt(uint64(totalPowerAtTipsetL2)), types.NewInt(uint64(len(ts.Blocks())))), wr)
eWeight = types.BigDiv(eWeight, types.NewInt(build.BlocksPerEpoch))
return types.BigAdd(out, eWeight), nil
}
// todo: dedupe with state manager
func (cs *ChainStore) call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) {
bstate := ts.ParentState()
r := vm.NewChainRand(cs, ts.Cids(), ts.Height(), nil)
vmi, err := vm.NewVM(bstate, ts.Height(), r, actors.NetworkAddress, cs)
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
}

View File

@ -91,12 +91,6 @@ func (ts *TipSet) Height() uint64 {
return ts.height
}
func (ts *TipSet) Weight() BigInt {
// TODO: implement correctly
log.Warn("Called TipSet.Weight: TODO: correct implementation")
return BigAdd(ts.blks[0].ParentWeight, NewInt(1))
}
func (ts *TipSet) Parents() []cid.Cid {
return ts.blks[0].Parents
}

View File

@ -210,6 +210,8 @@ func (m *Miner) GetBestMiningCandidate() (*MiningBase, error) {
return m.lastWork, nil
}
if types.BigCmp(bts.Weight(), m.lastWork.ts.Weight()) <= 0 {
return m.lastWork, nil
}

View File

@ -209,7 +209,7 @@ func (a *StateAPI) StateWaitMsg(ctx context.Context, msg cid.Cid) (*api.MsgWait,
}
func (a *StateAPI) StateListMiners(ctx context.Context, ts *types.TipSet) ([]address.Address, error) {
var state actors.StorageMarketState
var state actors.StoragePowerState
if _, err := a.StateManager.LoadActorState(ctx, actors.StorageMarketAddress, &state, ts); err != nil {
return nil, err
}
@ -226,3 +226,7 @@ func (a *StateAPI) StateListMiners(ctx context.Context, ts *types.TipSet) ([]add
func (a *StateAPI) StateListActors(ctx context.Context, ts *types.TipSet) ([]address.Address, error) {
return a.StateManager.ListAllActors(ctx, ts)
}
func (a *StateAPI) StateTipSetWeight(ctx context.Context, ts *types.TipSet) (types.BigInt, error) {
return a.Chain.Weight(ctx, ts)
}