From 88542630ac582e4fb32b57946d5d4369e06ac72a Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 22 Jul 2022 23:14:02 +0300 Subject: [PATCH] blockchainer: drop the package completely It's not an ideal solution, but at least it solves the problem for now. Caveats: * consensus only needs one method, so it's mirrored to Blockchain * rpcsrv uses core.* definition of the StateRoot (so technically it might as well not have an internal Ledger), but it uses core already unfortunately --- internal/fakechain/fakechain.go | 6 ------ pkg/consensus/consensus.go | 10 +++++----- pkg/consensus/consensus_test.go | 2 +- pkg/core/blockchain.go | 20 ++++++++++++++++++-- pkg/core/blockchainer/state_root.go | 19 ------------------- pkg/services/rpcsrv/server.go | 3 +-- 6 files changed, 25 insertions(+), 35 deletions(-) delete mode 100644 pkg/core/blockchainer/state_root.go diff --git a/internal/fakechain/fakechain.go b/internal/fakechain/fakechain.go index 6bb90590f..0ac04a4c5 100644 --- a/internal/fakechain/fakechain.go +++ b/internal/fakechain/fakechain.go @@ -9,7 +9,6 @@ import ( "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/block" - "github.com/nspcc-dev/neo-go/pkg/core/blockchainer" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/mempool" "github.com/nspcc-dev/neo-go/pkg/core/mpt" @@ -293,11 +292,6 @@ func (chain *FakeChain) GetEnrollments() ([]state.Validator, error) { panic("TODO") } -// GetStateModule implements the Blockchainer interface. -func (chain *FakeChain) GetStateModule() blockchainer.StateRoot { - return nil -} - // GetStorageItem implements the Blockchainer interface. func (chain *FakeChain) GetStorageItem(id int32, key []byte) state.StorageItem { panic("TODO") diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index ec1273ef8..c9bce1663 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -13,9 +13,9 @@ import ( "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/netmode" coreb "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/interop" "github.com/nspcc-dev/neo-go/pkg/core/mempool" + "github.com/nspcc-dev/neo-go/pkg/core/state" "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" @@ -50,7 +50,7 @@ type Ledger interface { GetConfig() config.ProtocolConfiguration GetMemPool() *mempool.Pool GetNextBlockValidators() ([]*keys.PublicKey, error) - GetStateModule() blockchainer.StateRoot + GetStateRoot(height uint32) (*state.MPTRoot, error) GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error) GetValidators() ([]*keys.PublicKey, error) PoolTx(t *transaction.Transaction, pools ...*mempool.Pool) error @@ -250,7 +250,7 @@ func (s *service) newPrepareRequest() payload.PrepareRequest { r := new(prepareRequest) if s.ProtocolConfiguration.StateRootInHeader { r.stateRootEnabled = true - if sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1); err == nil { + if sr, err := s.Chain.GetStateRoot(s.dbft.BlockIndex - 1); err == nil { r.stateRoot = sr.Root } else { panic(err) @@ -535,7 +535,7 @@ func (s *service) verifyRequest(p payload.ConsensusPayload) error { return errInvalidVersion } if s.ProtocolConfiguration.StateRootInHeader { - sr, err := s.Chain.GetStateModule().GetStateRoot(s.dbft.BlockIndex - 1) + sr, err := s.Chain.GetStateRoot(s.dbft.BlockIndex - 1) if err != nil { return err } else if sr.Root != req.stateRoot { @@ -689,7 +689,7 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block { block.Block.Nonce = ctx.Nonce block.Block.Index = ctx.BlockIndex if s.ProtocolConfiguration.StateRootInHeader { - sr, err := s.Chain.GetStateModule().GetStateRoot(ctx.BlockIndex - 1) + sr, err := s.Chain.GetStateRoot(ctx.BlockIndex - 1) if err != nil { s.log.Fatal(fmt.Sprintf("failed to get state root: %s", err.Error())) } diff --git a/pkg/consensus/consensus_test.go b/pkg/consensus/consensus_test.go index 058875afb..a49af9759 100644 --- a/pkg/consensus/consensus_test.go +++ b/pkg/consensus/consensus_test.go @@ -321,7 +321,7 @@ func TestService_PrepareRequest(t *testing.T) { prevHash: prevHash, }) - sr, err := srv.Chain.GetStateModule().GetStateRoot(srv.dbft.BlockIndex - 1) + sr, err := srv.Chain.GetStateRoot(srv.dbft.BlockIndex - 1) require.NoError(t, err) checkRequest(t, errInvalidTransactionsCount, &prepareRequest{stateRootEnabled: true, diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 4d3ec192a..ca47cb7f2 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -15,7 +15,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/limits" "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" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/interop/contract" @@ -181,6 +180,18 @@ type Blockchain struct { unsubCh chan interface{} } +// StateRoot represents local state root module. +type StateRoot interface { + CurrentLocalHeight() uint32 + CurrentLocalStateRoot() util.Uint256 + CurrentValidatedHeight() uint32 + FindStates(root util.Uint256, prefix, start []byte, max int) ([]storage.KeyValue, error) + GetState(root util.Uint256, key []byte) ([]byte, error) + GetStateProof(root util.Uint256, key []byte) ([][]byte, error) + GetStateRoot(height uint32) (*state.MPTRoot, error) + GetLatestStateHeight(root util.Uint256) (uint32, error) +} + // bcEvent is an internal event generated by the Blockchain and then // broadcasted to other parties. It joins the new block and associated // invocation logs, all the other events visible from outside can be produced @@ -995,8 +1006,13 @@ func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) error { return nil } +// GetStateRoot returns state root for the given height. +func (bc *Blockchain) GetStateRoot(height uint32) (*state.MPTRoot, error) { + return bc.stateRoot.GetStateRoot(height) +} + // GetStateModule returns state root service instance. -func (bc *Blockchain) GetStateModule() blockchainer.StateRoot { +func (bc *Blockchain) GetStateModule() StateRoot { return bc.stateRoot } diff --git a/pkg/core/blockchainer/state_root.go b/pkg/core/blockchainer/state_root.go deleted file mode 100644 index 3b11f6c63..000000000 --- a/pkg/core/blockchainer/state_root.go +++ /dev/null @@ -1,19 +0,0 @@ -package blockchainer - -import ( - "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/util" -) - -// StateRoot represents local state root module. -type StateRoot interface { - CurrentLocalHeight() uint32 - CurrentLocalStateRoot() util.Uint256 - CurrentValidatedHeight() uint32 - FindStates(root util.Uint256, prefix, start []byte, max int) ([]storage.KeyValue, error) - GetState(root util.Uint256, key []byte) ([]byte, error) - GetStateProof(root util.Uint256, key []byte) ([][]byte, error) - GetStateRoot(height uint32) (*state.MPTRoot, error) - GetLatestStateHeight(root util.Uint256) (uint32, error) -} diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 3d09fbc73..b08968046 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -25,7 +25,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/core" "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/fee" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/interop/iterator" @@ -87,7 +86,7 @@ type ( GetNextBlockValidators() ([]*keys.PublicKey, error) GetNotaryContractScriptHash() util.Uint160 GetNotaryServiceFeePerKey() int64 - GetStateModule() blockchainer.StateRoot + GetStateModule() core.StateRoot GetStorageItem(id int32, key []byte) state.StorageItem GetTestHistoricVM(t trigger.Type, tx *transaction.Transaction, b *block.Block) (*interop.Context, error) GetTestVM(t trigger.Type, tx *transaction.Transaction, b *block.Block) *interop.Context