services/consensus: drop blockchainer.Blockchainer use

Simplify testing, avoid monstrous Blockchainer interface.
This commit is contained in:
Roman Khimov 2022-01-13 03:19:10 +03:00
parent fc45d3b132
commit 59b2696849
7 changed files with 80 additions and 24 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/config/netmode"
coreb "github.com/nspcc-dev/neo-go/pkg/core/block" 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/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/mempool"
"github.com/nspcc-dev/neo-go/pkg/core/native" "github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -43,6 +44,22 @@ const nsInMs = 1000000
// Category is message category for extensible payloads. // Category is message category for extensible payloads.
const Category = "dBFT" const Category = "dBFT"
// Ledger is the interface to Blockchain sufficient for Service.
type Ledger interface {
AddBlock(block *coreb.Block) error
ApplyPolicyToTxSet([]*transaction.Transaction) []*transaction.Transaction
GetMemPool() *mempool.Pool
GetNextBlockValidators() ([]*keys.PublicKey, error)
GetStateModule() blockchainer.StateRoot
GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error)
GetValidators() ([]*keys.PublicKey, error)
PoolTx(t *transaction.Transaction, pools ...*mempool.Pool) error
SubscribeForBlocks(ch chan<- *coreb.Block)
UnsubscribeFromBlocks(ch chan<- *coreb.Block)
interop.Ledger
mempool.Feer
}
// Service represents consensus instance. // Service represents consensus instance.
type Service interface { type Service interface {
// Start initializes dBFT and starts event loop for consensus service. // Start initializes dBFT and starts event loop for consensus service.
@ -92,8 +109,8 @@ type Config struct {
// Broadcast is a callback which is called to notify server // Broadcast is a callback which is called to notify server
// about new consensus payload to sent. // about new consensus payload to sent.
Broadcast func(p *npayload.Extensible) Broadcast func(p *npayload.Extensible)
// Chain is a core.Blockchainer instance. // Chain is a Ledger instance.
Chain blockchainer.Blockchainer Chain Ledger
// ProtocolConfiguration contains protocol settings. // ProtocolConfiguration contains protocol settings.
ProtocolConfiguration config.ProtocolConfiguration ProtocolConfiguration config.ProtocolConfiguration
// RequestTx is a callback to which will be called // RequestTx is a callback to which will be called

View file

@ -13,7 +13,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config" "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" "github.com/nspcc-dev/neo-go/pkg/core"
"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/fee"
"github.com/nspcc-dev/neo-go/pkg/core/native" "github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
@ -366,9 +365,10 @@ func TestService_OnPayload(t *testing.T) {
func TestVerifyBlock(t *testing.T) { func TestVerifyBlock(t *testing.T) {
srv := newTestService(t) srv := newTestService(t)
bc := srv.Chain.(*core.Blockchain)
srv.lastTimestamp = 1 srv.lastTimestamp = 1
t.Run("good empty", func(t *testing.T) { t.Run("good empty", func(t *testing.T) {
b := testchain.NewBlock(t, srv.Chain, 1, 0) b := testchain.NewBlock(t, bc, 1, 0)
require.True(t, srv.verifyBlock(&neoBlock{Block: *b})) require.True(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
t.Run("good pooled tx", func(t *testing.T) { t.Run("good pooled tx", func(t *testing.T) {
@ -377,7 +377,7 @@ func TestVerifyBlock(t *testing.T) {
addSender(t, tx) addSender(t, tx)
signTx(t, srv.Chain, tx) signTx(t, srv.Chain, tx)
require.NoError(t, srv.Chain.PoolTx(tx)) require.NoError(t, srv.Chain.PoolTx(tx))
b := testchain.NewBlock(t, srv.Chain, 1, 0, tx) b := testchain.NewBlock(t, bc, 1, 0, tx)
require.True(t, srv.verifyBlock(&neoBlock{Block: *b})) require.True(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
t.Run("good non-pooled tx", func(t *testing.T) { t.Run("good non-pooled tx", func(t *testing.T) {
@ -385,7 +385,7 @@ func TestVerifyBlock(t *testing.T) {
tx.ValidUntilBlock = 1 tx.ValidUntilBlock = 1
addSender(t, tx) addSender(t, tx)
signTx(t, srv.Chain, tx) signTx(t, srv.Chain, tx)
b := testchain.NewBlock(t, srv.Chain, 1, 0, tx) b := testchain.NewBlock(t, bc, 1, 0, tx)
require.True(t, srv.verifyBlock(&neoBlock{Block: *b})) require.True(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
t.Run("good conflicting tx", func(t *testing.T) { t.Run("good conflicting tx", func(t *testing.T) {
@ -402,11 +402,11 @@ func TestVerifyBlock(t *testing.T) {
signTx(t, srv.Chain, tx2) signTx(t, srv.Chain, tx2)
require.NoError(t, srv.Chain.PoolTx(tx1)) require.NoError(t, srv.Chain.PoolTx(tx1))
require.Error(t, srv.Chain.PoolTx(tx2)) require.Error(t, srv.Chain.PoolTx(tx2))
b := testchain.NewBlock(t, srv.Chain, 1, 0, tx2) b := testchain.NewBlock(t, bc, 1, 0, tx2)
require.True(t, srv.verifyBlock(&neoBlock{Block: *b})) require.True(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
t.Run("bad old", func(t *testing.T) { t.Run("bad old", func(t *testing.T) {
b := testchain.NewBlock(t, srv.Chain, 1, 0) b := testchain.NewBlock(t, bc, 1, 0)
b.Index = srv.Chain.BlockHeight() b.Index = srv.Chain.BlockHeight()
require.False(t, srv.verifyBlock(&neoBlock{Block: *b})) require.False(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
@ -417,11 +417,11 @@ func TestVerifyBlock(t *testing.T) {
tx.ValidUntilBlock = 1 tx.ValidUntilBlock = 1
addSender(t, tx) addSender(t, tx)
signTx(t, srv.Chain, tx) signTx(t, srv.Chain, tx)
b := testchain.NewBlock(t, srv.Chain, 1, 0, tx) b := testchain.NewBlock(t, bc, 1, 0, tx)
require.False(t, srv.verifyBlock(&neoBlock{Block: *b})) require.False(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
t.Run("bad timestamp", func(t *testing.T) { t.Run("bad timestamp", func(t *testing.T) {
b := testchain.NewBlock(t, srv.Chain, 1, 0) b := testchain.NewBlock(t, bc, 1, 0)
b.Timestamp = srv.lastTimestamp - 1 b.Timestamp = srv.lastTimestamp - 1
require.False(t, srv.verifyBlock(&neoBlock{Block: *b})) require.False(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
@ -431,7 +431,7 @@ func TestVerifyBlock(t *testing.T) {
addSender(t, tx) addSender(t, tx)
signTx(t, srv.Chain, tx) signTx(t, srv.Chain, tx)
tx.Scripts[0].InvocationScript[16] = ^tx.Scripts[0].InvocationScript[16] tx.Scripts[0].InvocationScript[16] = ^tx.Scripts[0].InvocationScript[16]
b := testchain.NewBlock(t, srv.Chain, 1, 0, tx) b := testchain.NewBlock(t, bc, 1, 0, tx)
require.False(t, srv.verifyBlock(&neoBlock{Block: *b})) require.False(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
t.Run("bad big sys fee", func(t *testing.T) { t.Run("bad big sys fee", func(t *testing.T) {
@ -442,7 +442,7 @@ func TestVerifyBlock(t *testing.T) {
addSender(t, txes[i]) addSender(t, txes[i])
signTx(t, srv.Chain, txes[i]) signTx(t, srv.Chain, txes[i])
} }
b := testchain.NewBlock(t, srv.Chain, 1, 0, txes...) b := testchain.NewBlock(t, bc, 1, 0, txes...)
require.False(t, srv.verifyBlock(&neoBlock{Block: *b})) require.False(t, srv.verifyBlock(&neoBlock{Block: *b}))
}) })
} }
@ -532,7 +532,7 @@ func addSender(t *testing.T, txs ...*transaction.Transaction) {
} }
} }
func signTx(t *testing.T, bc blockchainer.Blockchainer, txs ...*transaction.Transaction) { func signTx(t *testing.T, bc Ledger, txs ...*transaction.Transaction) {
validators := make([]*keys.PublicKey, 4) validators := make([]*keys.PublicKey, 4)
privNetKeys := make([]*keys.PrivateKey, 4) privNetKeys := make([]*keys.PrivateKey, 4)
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {

View file

@ -3,14 +3,23 @@ package chaindump
import ( import (
"fmt" "fmt"
"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/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
) )
// DumperRestorer in the interface to get/add blocks from/to.
type DumperRestorer interface {
AddBlock(block *block.Block) error
GetBlock(hash util.Uint256) (*block.Block, error)
GetConfig() config.ProtocolConfiguration
GetHeaderHash(int) util.Uint256
}
// Dump writes count blocks from start to the provided writer. // Dump writes count blocks from start to the provided writer.
// Note: header needs to be written separately by client. // Note: header needs to be written separately by client.
func Dump(bc blockchainer.Blockchainer, w *io.BinWriter, start, count uint32) error { func Dump(bc DumperRestorer, w *io.BinWriter, start, count uint32) error {
for i := start; i < start+count; i++ { for i := start; i < start+count; i++ {
bh := bc.GetHeaderHash(int(i)) bh := bc.GetHeaderHash(int(i))
b, err := bc.GetBlock(bh) b, err := bc.GetBlock(bh)
@ -31,7 +40,7 @@ func Dump(bc blockchainer.Blockchainer, w *io.BinWriter, start, count uint32) er
// Restore restores blocks from provided reader. // Restore restores blocks from provided reader.
// f is called after addition of every block. // f is called after addition of every block.
func Restore(bc blockchainer.Blockchainer, r *io.BinReader, skip, count uint32, f func(b *block.Block) error) error { func Restore(bc DumperRestorer, r *io.BinReader, skip, count uint32, f func(b *block.Block) error) error {
readBlock := func(r *io.BinReader) ([]byte, error) { readBlock := func(r *io.BinReader) ([]byte, error) {
var size = r.ReadU32LE() var size = r.ReadU32LE()
buf := make([]byte, size) buf := make([]byte, size)

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/internal/fakechain" "github.com/nspcc-dev/neo-go/internal/fakechain"
"github.com/nspcc-dev/neo-go/pkg/config" "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/mempool" "github.com/nspcc-dev/neo-go/pkg/core/mempool"
"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/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
@ -14,7 +13,7 @@ import (
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
func getTestNotary(t *testing.T, bc blockchainer.Blockchainer, walletPath, pass string) (*wallet.Account, *Notary, *mempool.Pool) { func getTestNotary(t *testing.T, bc Ledger, walletPath, pass string) (*wallet.Account, *Notary, *mempool.Pool) {
mainCfg := config.P2PNotary{ mainCfg := config.P2PNotary{
Enabled: true, Enabled: true,
UnlockWallet: config.Wallet{ UnlockWallet: config.Wallet{

View file

@ -11,7 +11,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config" "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/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/mempool" "github.com/nspcc-dev/neo-go/pkg/core/mempool"
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent" "github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -27,6 +26,16 @@ import (
) )
type ( type (
// Ledger is the interface to Blockchain sufficient for Notary.
Ledger interface {
BlockHeight() uint32
GetMaxVerificationGAS() int64
GetNotaryContractScriptHash() util.Uint160
SubscribeForBlocks(ch chan<- *block.Block)
UnsubscribeFromBlocks(ch chan<- *block.Block)
VerifyWitness(util.Uint160, hash.Hashable, *transaction.Witness, int64) (int64, error)
}
// Notary represents Notary module. // Notary represents Notary module.
Notary struct { Notary struct {
Config Config Config Config
@ -60,7 +69,7 @@ type (
// Config represents external configuration for Notary module. // Config represents external configuration for Notary module.
Config struct { Config struct {
MainCfg config.P2PNotary MainCfg config.P2PNotary
Chain blockchainer.Blockchainer Chain Ledger
Log *zap.Logger Log *zap.Logger
} }
) )

View file

@ -9,10 +9,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config" "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/block"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"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/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"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/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/util/slice"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
@ -20,6 +22,17 @@ import (
) )
type ( type (
// Ledger is the interface to Blockchain sufficient for Oracle.
Ledger interface {
BlockHeight() uint32
FeePerByte() int64
GetBaseExecFee() int64
GetConfig() config.ProtocolConfiguration
GetMaxVerificationGAS() int64
GetTestVM(t trigger.Type, tx *transaction.Transaction, b *block.Block) *interop.Context
GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error)
}
// Oracle represents oracle module capable of talking // Oracle represents oracle module capable of talking
// with the external world. // with the external world.
Oracle struct { Oracle struct {
@ -64,7 +77,7 @@ type (
Network netmode.Magic Network netmode.Magic
MainCfg config.OracleConfiguration MainCfg config.OracleConfiguration
Client HTTPClient Client HTTPClient
Chain blockchainer.Blockchainer Chain Ledger
ResponseHandler Broadcaster ResponseHandler Broadcaster
OnTransaction TxCallback OnTransaction TxCallback
URIValidator URIValidator URIValidator URIValidator

View file

@ -19,6 +19,15 @@ import (
) )
type ( type (
// Ledger is the interface to Blockchain sufficient for Service.
Ledger interface {
GetConfig() config.ProtocolConfiguration
GetStateModule() blockchainer.StateRoot
HeaderHeight() uint32
SubscribeForBlocks(ch chan<- *block.Block)
UnsubscribeFromBlocks(ch chan<- *block.Block)
}
// Service represents state root service. // Service represents state root service.
Service interface { Service interface {
blockchainer.StateRoot blockchainer.StateRoot
@ -31,7 +40,7 @@ type (
service struct { service struct {
blockchainer.StateRoot blockchainer.StateRoot
chain blockchainer.Blockchainer chain Ledger
MainCfg config.StateRoot MainCfg config.StateRoot
Network netmode.Magic Network netmode.Magic
@ -60,7 +69,7 @@ const (
) )
// New returns new state root service instance using underlying module. // New returns new state root service instance using underlying module.
func New(cfg config.StateRoot, log *zap.Logger, bc blockchainer.Blockchainer, cb RelayCallback) (Service, error) { func New(cfg config.StateRoot, log *zap.Logger, bc Ledger, cb RelayCallback) (Service, error) {
bcConf := bc.GetConfig() bcConf := bc.GetConfig()
s := &service{ s := &service{
StateRoot: bc.GetStateModule(), StateRoot: bc.GetStateModule(),