From 380e706255335f76b8649e892159c25c60dc1c99 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 13 Jan 2022 04:34:14 +0300 Subject: [PATCH] core/state*: drop blockchainer.Blockchainer use --- pkg/core/blockchain.go | 2 +- pkg/core/stateroot/module.go | 33 ++++++++++++++++++++------------- pkg/core/stateroot/store.go | 2 +- pkg/core/statesync/module.go | 16 ++++++++++++++-- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index bc93aac6a..3b083a81b 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -267,7 +267,7 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L contracts: *native.NewContracts(cfg), } - bc.stateRoot = stateroot.NewModule(bc, bc.log, bc.dao.Store) + bc.stateRoot = stateroot.NewModule(bc.GetConfig(), bc.VerifyWitness, bc.log, bc.dao.Store) bc.contracts.Designate.StateRootService = bc.stateRoot if err := bc.init(); err != nil { diff --git a/pkg/core/stateroot/module.go b/pkg/core/stateroot/module.go index 322e23c8e..9092a5bf9 100644 --- a/pkg/core/stateroot/module.go +++ b/pkg/core/stateroot/module.go @@ -6,11 +6,13 @@ import ( "fmt" "sync" + "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/netmode" - "github.com/nspcc-dev/neo-go/pkg/core/blockchainer" "github.com/nspcc-dev/neo-go/pkg/core/mpt" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/storage" + "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/util" "go.uber.org/atomic" @@ -18,13 +20,17 @@ import ( ) type ( + // VerifierFunc is a function that allows to check witness of account + // for Hashable item with GAS limit. + VerifierFunc func(util.Uint160, hash.Hashable, *transaction.Witness, int64) (int64, error) // Module represents module for local processing of state roots. Module struct { - Store *storage.MemCachedStore - network netmode.Magic - mpt *mpt.Trie - bc blockchainer.Blockchainer - log *zap.Logger + Store *storage.MemCachedStore + network netmode.Magic + srInHead bool + mpt *mpt.Trie + verifier VerifierFunc + log *zap.Logger currentLocal atomic.Value localHeight atomic.Uint32 @@ -45,12 +51,13 @@ type ( ) // NewModule returns new instance of stateroot module. -func NewModule(bc blockchainer.Blockchainer, log *zap.Logger, s *storage.MemCachedStore) *Module { +func NewModule(cfg config.ProtocolConfiguration, verif VerifierFunc, log *zap.Logger, s *storage.MemCachedStore) *Module { return &Module{ - network: bc.GetConfig().Magic, - bc: bc, - log: log, - Store: s, + network: cfg.Magic, + srInHead: cfg.StateRootInHeader, + verifier: verif, + log: log, + Store: s, } } @@ -191,7 +198,7 @@ func (s *Module) UpdateCurrentLocal(mpt *mpt.Trie, sr *state.MPTRoot) { s.mpt = mpt s.currentLocal.Store(sr.Root) s.localHeight.Store(sr.Index) - if s.bc.GetConfig().StateRootInHeader { + if s.srInHead { s.validatedHeight.Store(sr.Index) updateStateHeightMetric(sr.Index) } @@ -216,6 +223,6 @@ func (s *Module) verifyWitness(r *state.MPTRoot) error { s.mtx.Lock() h := s.getKeyCacheForHeight(r.Index).validatorsHash s.mtx.Unlock() - _, err := s.bc.VerifyWitness(h, r, &r.Witness[0], maxVerificationGAS) + _, err := s.verifier(h, r, &r.Witness[0], maxVerificationGAS) return err } diff --git a/pkg/core/stateroot/store.go b/pkg/core/stateroot/store.go index ee5a52f78..f059d1c31 100644 --- a/pkg/core/stateroot/store.go +++ b/pkg/core/stateroot/store.go @@ -83,7 +83,7 @@ func (s *Module) AddStateRoot(sr *state.MPTRoot) error { return err } s.validatedHeight.Store(sr.Index) - if !s.bc.GetConfig().StateRootInHeader { + if !s.srInHead { updateStateHeightMetric(sr.Index) } return nil diff --git a/pkg/core/statesync/module.go b/pkg/core/statesync/module.go index ebf888e2a..ccacd3f61 100644 --- a/pkg/core/statesync/module.go +++ b/pkg/core/statesync/module.go @@ -23,6 +23,7 @@ import ( "fmt" "sync" + "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/blockchainer" "github.com/nspcc-dev/neo-go/pkg/core/dao" @@ -59,6 +60,17 @@ const ( blocksSynced ) +// Ledger is the interface required from Blockchain for Module to operate. +type Ledger interface { + AddHeaders(...*block.Header) error + BlockHeight() uint32 + GetConfig() config.ProtocolConfiguration + GetHeader(hash util.Uint256) (*block.Header, error) + GetHeaderHash(int) util.Uint256 + GetStateModule() blockchainer.StateRoot + HeaderHeight() uint32 +} + // Module represents state sync module and aimed to gather state-related data to // perform an atomic state jump. type Module struct { @@ -75,7 +87,7 @@ type Module struct { blockHeight uint32 dao *dao.Simple - bc blockchainer.Blockchainer + bc Ledger mptpool *Pool billet *mpt.Billet @@ -84,7 +96,7 @@ type Module struct { } // NewModule returns new instance of statesync module. -func NewModule(bc blockchainer.Blockchainer, log *zap.Logger, s *dao.Simple, jumpCallback func(p uint32) error) *Module { +func NewModule(bc Ledger, log *zap.Logger, s *dao.Simple, jumpCallback func(p uint32) error) *Module { if !(bc.GetConfig().P2PStateExchangeExtensions && bc.GetConfig().RemoveUntraceableBlocks) { return &Module{ dao: s,