core/state*: drop blockchainer.Blockchainer use

This commit is contained in:
Roman Khimov 2022-01-13 04:34:14 +03:00
parent 44fd7af044
commit 380e706255
4 changed files with 36 additions and 17 deletions

View file

@ -267,7 +267,7 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
contracts: *native.NewContracts(cfg), 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 bc.contracts.Designate.StateRootService = bc.stateRoot
if err := bc.init(); err != nil { if err := bc.init(); err != nil {

View file

@ -6,11 +6,13 @@ import (
"fmt" "fmt"
"sync" "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/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/mpt"
"github.com/nspcc-dev/neo-go/pkg/core/state" "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/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/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"go.uber.org/atomic" "go.uber.org/atomic"
@ -18,13 +20,17 @@ import (
) )
type ( 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 represents module for local processing of state roots.
Module struct { Module struct {
Store *storage.MemCachedStore Store *storage.MemCachedStore
network netmode.Magic network netmode.Magic
mpt *mpt.Trie srInHead bool
bc blockchainer.Blockchainer mpt *mpt.Trie
log *zap.Logger verifier VerifierFunc
log *zap.Logger
currentLocal atomic.Value currentLocal atomic.Value
localHeight atomic.Uint32 localHeight atomic.Uint32
@ -45,12 +51,13 @@ type (
) )
// NewModule returns new instance of stateroot module. // 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{ return &Module{
network: bc.GetConfig().Magic, network: cfg.Magic,
bc: bc, srInHead: cfg.StateRootInHeader,
log: log, verifier: verif,
Store: s, log: log,
Store: s,
} }
} }
@ -191,7 +198,7 @@ func (s *Module) UpdateCurrentLocal(mpt *mpt.Trie, sr *state.MPTRoot) {
s.mpt = mpt s.mpt = mpt
s.currentLocal.Store(sr.Root) s.currentLocal.Store(sr.Root)
s.localHeight.Store(sr.Index) s.localHeight.Store(sr.Index)
if s.bc.GetConfig().StateRootInHeader { if s.srInHead {
s.validatedHeight.Store(sr.Index) s.validatedHeight.Store(sr.Index)
updateStateHeightMetric(sr.Index) updateStateHeightMetric(sr.Index)
} }
@ -216,6 +223,6 @@ func (s *Module) verifyWitness(r *state.MPTRoot) error {
s.mtx.Lock() s.mtx.Lock()
h := s.getKeyCacheForHeight(r.Index).validatorsHash h := s.getKeyCacheForHeight(r.Index).validatorsHash
s.mtx.Unlock() s.mtx.Unlock()
_, err := s.bc.VerifyWitness(h, r, &r.Witness[0], maxVerificationGAS) _, err := s.verifier(h, r, &r.Witness[0], maxVerificationGAS)
return err return err
} }

View file

@ -83,7 +83,7 @@ func (s *Module) AddStateRoot(sr *state.MPTRoot) error {
return err return err
} }
s.validatedHeight.Store(sr.Index) s.validatedHeight.Store(sr.Index)
if !s.bc.GetConfig().StateRootInHeader { if !s.srInHead {
updateStateHeightMetric(sr.Index) updateStateHeightMetric(sr.Index)
} }
return nil return nil

View file

@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"sync" "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/block"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer" "github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
"github.com/nspcc-dev/neo-go/pkg/core/dao" "github.com/nspcc-dev/neo-go/pkg/core/dao"
@ -59,6 +60,17 @@ const (
blocksSynced 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 // Module represents state sync module and aimed to gather state-related data to
// perform an atomic state jump. // perform an atomic state jump.
type Module struct { type Module struct {
@ -75,7 +87,7 @@ type Module struct {
blockHeight uint32 blockHeight uint32
dao *dao.Simple dao *dao.Simple
bc blockchainer.Blockchainer bc Ledger
mptpool *Pool mptpool *Pool
billet *mpt.Billet billet *mpt.Billet
@ -84,7 +96,7 @@ type Module struct {
} }
// NewModule returns new instance of statesync module. // 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) { if !(bc.GetConfig().P2PStateExchangeExtensions && bc.GetConfig().RemoveUntraceableBlocks) {
return &Module{ return &Module{
dao: s, dao: s,