diff --git a/api/api.go b/api/api.go index 8b1e175cd..aa3a56df2 100644 --- a/api/api.go +++ b/api/api.go @@ -61,6 +61,7 @@ type FullNode interface { ChainReadObj(context.Context, cid.Cid) ([]byte, error) ChainSetHead(context.Context, *types.TipSet) error ChainGetGenesis(context.Context) (*types.TipSet, error) + ChainTipSetWeight(context.Context, *types.TipSet) (types.BigInt, error) // syncer SyncState(context.Context) (*SyncState, error) @@ -130,7 +131,6 @@ 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) diff --git a/api/struct.go b/api/struct.go index 299ab1770..93d670676 100644 --- a/api/struct.go +++ b/api/struct.go @@ -49,6 +49,7 @@ type FullNodeStruct struct { ChainReadObj func(context.Context, cid.Cid) ([]byte, error) `perm:"read"` ChainSetHead func(context.Context, *types.TipSet) error `perm:"admin"` ChainGetGenesis func(context.Context) (*types.TipSet, error) `perm:"read"` + ChainTipSetWeight func(context.Context, *types.TipSet) (types.BigInt, error) `perm:"read"` SyncState func(context.Context) (*SyncState, error) `perm:"read"` SyncSubmitBlock func(ctx context.Context, blk *types.BlockMsg) error `perm:"write"` @@ -97,7 +98,6 @@ 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"` @@ -317,6 +317,10 @@ func (c *FullNodeStruct) ChainGetGenesis(ctx context.Context) (*types.TipSet, er return c.Internal.ChainGetGenesis(ctx) } +func (c *FullNodeStruct) ChainTipSetWeight(ctx context.Context, ts *types.TipSet) (types.BigInt, error) { + return c.Internal.ChainTipSetWeight(ctx, ts) +} + func (c *FullNodeStruct) SyncState(ctx context.Context) (*SyncState, error) { return c.Internal.SyncState(ctx) } @@ -379,10 +383,6 @@ func (c *FullNodeStruct) StateListActors(ctx context.Context, ts *types.TipSet) return c.Internal.StateListActors(ctx, ts) } -func (c *FullNodeStruct) StateTipSetWeight(ctx context.Context, ts *types.TipSet) (types.BigInt, error) { - return c.Internal.StateTipSetWeight(ctx, ts) -} - func (c *FullNodeStruct) PaychGet(ctx context.Context, from, to address.Address, ensureFunds types.BigInt) (*ChannelInfo, error) { return c.Internal.PaychGet(ctx, from, to, ensureFunds) } diff --git a/chain/gen/mining.go b/chain/gen/mining.go index bead86d52..cb8a926b0 100644 --- a/chain/gen/mining.go +++ b/chain/gen/mining.go @@ -96,8 +96,11 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal } next.BLSAggregate = aggSig - pweight := sm.ChainStore().Weight(parents) - next.ParentWeight = types.NewInt(pweight) + pweight, err := sm.ChainStore().Weight(ctx, parents) + if err != nil { + return nil, err + } + next.ParentWeight = pweight cst := hamt.CSTFromBstore(sm.ChainStore().Blockstore()) tree, err := state.LoadStateTree(cst, st) diff --git a/chain/metrics/consensus.go b/chain/metrics/consensus.go index 297a28f28..0fae64667 100644 --- a/chain/metrics/consensus.go +++ b/chain/metrics/consensus.go @@ -89,11 +89,16 @@ func sendHeadNotifs(ctx context.Context, ps *pubsub.PubSub, topic string, chain case notif := <-notifs: n := notif[len(notif)-1] + w, err := chain.ChainTipSetWeight(ctx, n.Val) + if err != nil { + return err + } + m := message{ Cids: n.Val.Cids(), Blocks: n.Val.Blocks(), Height: n.Val.Height(), - Weight: n.Val.Weight(), + Weight: w, NodeName: nickname, } diff --git a/chain/sync.go b/chain/sync.go index 92ba286f6..8ce75744d 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -261,7 +261,7 @@ func computeMsgMeta(bs amt.Blocks, bmsgCids, smsgCids []cbg.CBORMarshaler) (cid. return mrcid, nil } -func (syncer *Syncer) selectHead(heads map[peer.ID]*types.TipSet) (*types.TipSet, error) { +func (syncer *Syncer) selectHead(ctx context.Context, heads map[peer.ID]*types.TipSet) (*types.TipSet, error) { var headsArr []*types.TipSet for _, ts := range heads { headsArr = append(headsArr, ts) @@ -298,7 +298,16 @@ func (syncer *Syncer) selectHead(heads map[peer.ID]*types.TipSet) (*types.TipSet return nil, fmt.Errorf("Conflict exists in heads set") } - if syncer.store.Weight(cur) > syncer.store.Weight(sel) { + curw, err := syncer.store.Weight(ctx, cur) + if err != nil { + return nil, err + } + selw, err := syncer.store.Weight(ctx, sel) + if err != nil { + return nil, err + } + + if curw.GreaterThan(selw) { sel = cur } } @@ -352,7 +361,7 @@ func (syncer *Syncer) Sync(ctx context.Context, maybeHead *types.TipSet) error { return xerrors.Errorf("collectChain failed: %w", err) } - if err := syncer.store.PutTipSet(maybeHead); err != nil { + if err := syncer.store.PutTipSet(ctx, maybeHead); err != nil { return xerrors.Errorf("failed to put synced tipset to chainstore: %w", err) } diff --git a/miner/miner.go b/miner/miner.go index 20cdbabdd..fb1f0b305 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -155,7 +155,7 @@ func (m *Miner) mine(ctx context.Context) { return } - base, err := m.GetBestMiningCandidate() + base, err := m.GetBestMiningCandidate(ctx) if err != nil { log.Errorf("failed to get best mining candidate: %s", err) continue @@ -199,8 +199,8 @@ type MiningBase struct { tickets []*types.Ticket } -func (m *Miner) GetBestMiningCandidate() (*MiningBase, error) { - bts, err := m.api.ChainHead(context.TODO()) +func (m *Miner) GetBestMiningCandidate(ctx context.Context) (*MiningBase, error) { + bts, err := m.api.ChainHead(ctx) if err != nil { return nil, err } @@ -210,7 +210,16 @@ func (m *Miner) GetBestMiningCandidate() (*MiningBase, error) { return m.lastWork, nil } - if types.BigCmp(bts.Weight(), m.lastWork.ts.Weight()) <= 0 { + btsw, err := m.api.ChainTipSetWeight(ctx, bts) + if err != nil { + return nil, err + } + ltsw, err := m.api.ChainTipSetWeight(ctx, m.lastWork.ts) + if err != nil { + return nil, err + } + + if types.BigCmp(btsw, ltsw) <= 0 { return m.lastWork, nil } } diff --git a/node/hello/hello.go b/node/hello/hello.go index aab4e519a..279a46fd3 100644 --- a/node/hello/hello.go +++ b/node/hello/hello.go @@ -3,6 +3,7 @@ package hello import ( "context" "fmt" + "github.com/filecoin-project/go-lotus/chain/types" "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" @@ -26,7 +27,7 @@ func init() { type Message struct { HeaviestTipSet []cid.Cid - HeaviestTipSetWeight uint64 + HeaviestTipSetWeight types.BigInt GenesisHash cid.Cid } @@ -83,7 +84,10 @@ func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error { defer s.Close() hts := hs.cs.GetHeaviestTipSet() - weight := hs.cs.Weight(hts) + weight, err := hs.cs.Weight(ctx, hts) + if err != nil { + return err + } gen, err := hs.cs.GetGenesis() if err != nil { return err diff --git a/node/impl/full/chain.go b/node/impl/full/chain.go index c94fc1bf1..f7670d303 100644 --- a/node/impl/full/chain.go +++ b/node/impl/full/chain.go @@ -160,3 +160,7 @@ func (a *ChainAPI) ChainGetGenesis(ctx context.Context) (*types.TipSet, error) { return types.NewTipSet([]*types.BlockHeader{genb}) } + +func (a *ChainAPI) ChainTipSetWeight(ctx context.Context, ts *types.TipSet) (types.BigInt, error) { + return a.Chain.Weight(ctx, ts) +} diff --git a/node/impl/full/state.go b/node/impl/full/state.go index 401509f23..c67571fba 100644 --- a/node/impl/full/state.go +++ b/node/impl/full/state.go @@ -226,7 +226,3 @@ 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) -}