From a7c19d445bf38cf888e7eef66c6bb139ea6389bb Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 8 Apr 2020 13:56:04 +0300 Subject: [PATCH] core: move Blockchainer interface to a separate package --- pkg/consensus/consensus.go | 3 ++- pkg/core/{ => blockchainer}/blockchainer.go | 2 +- pkg/core/interop_system.go | 3 ++- pkg/core/interops.go | 5 +++-- pkg/network/blockqueue.go | 6 +++--- pkg/network/server.go | 5 +++-- pkg/rpc/response/result/block.go | 4 ++-- pkg/rpc/response/result/block_header.go | 4 ++-- pkg/rpc/response/result/tx_raw_output.go | 4 ++-- pkg/rpc/response/result/unclaimed.go | 3 ++- pkg/rpc/response/result/unspents.go | 4 ++-- pkg/rpc/server/server.go | 5 +++-- 12 files changed, 27 insertions(+), 21 deletions(-) rename pkg/core/{ => blockchainer}/blockchainer.go (99%) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 25b16b7c7..e22deaf8f 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -12,6 +12,7 @@ import ( "github.com/nspcc-dev/dbft/payload" "github.com/nspcc-dev/neo-go/pkg/core" 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/mempool" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" @@ -77,7 +78,7 @@ type Config struct { // about the new block that needs to be broadcasted. RelayBlock func(b *coreb.Block) // Chain is a core.Blockchainer instance. - Chain core.Blockchainer + Chain blockchainer.Blockchainer // RequestTx is a callback to which will be called // when a node lacks transactions present in a block. RequestTx func(h ...util.Uint256) diff --git a/pkg/core/blockchainer.go b/pkg/core/blockchainer/blockchainer.go similarity index 99% rename from pkg/core/blockchainer.go rename to pkg/core/blockchainer/blockchainer.go index a633bb523..1e368f90f 100644 --- a/pkg/core/blockchainer.go +++ b/pkg/core/blockchainer/blockchainer.go @@ -1,4 +1,4 @@ -package core +package blockchainer import ( "github.com/nspcc-dev/neo-go/pkg/config" diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index 9e9000a45..0fd792dbb 100644 --- a/pkg/core/interop_system.go +++ b/pkg/core/interop_system.go @@ -6,6 +6,7 @@ import ( "math" "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/state" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -32,7 +33,7 @@ type StorageContext struct { // getBlockHashFromElement converts given vm.Element to block hash using given // Blockchainer if needed. Interop functions accept both block numbers and // block hashes as parameters, thus this function is needed. -func getBlockHashFromElement(bc Blockchainer, element *vm.Element) (util.Uint256, error) { +func getBlockHashFromElement(bc blockchainer.Blockchainer, element *vm.Element) (util.Uint256, error) { var hash util.Uint256 hashbytes := element.Bytes() if len(hashbytes) <= 5 { diff --git a/pkg/core/interops.go b/pkg/core/interops.go index ea0ed68b0..458d9ffad 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -11,6 +11,7 @@ import ( "sort" "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/state" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -22,7 +23,7 @@ import ( ) type interopContext struct { - bc Blockchainer + bc blockchainer.Blockchainer trigger trigger.Type block *block.Block tx *transaction.Transaction @@ -31,7 +32,7 @@ type interopContext struct { log *zap.Logger } -func newInteropContext(trigger trigger.Type, bc Blockchainer, d dao.DAO, block *block.Block, tx *transaction.Transaction, log *zap.Logger) *interopContext { +func newInteropContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO, block *block.Block, tx *transaction.Transaction, log *zap.Logger) *interopContext { dao := dao.NewCached(d) nes := make([]state.NotificationEvent, 0) return &interopContext{bc, trigger, block, tx, dao, nes, log} diff --git a/pkg/network/blockqueue.go b/pkg/network/blockqueue.go index 762e569fa..ed6eac1ae 100644 --- a/pkg/network/blockqueue.go +++ b/pkg/network/blockqueue.go @@ -2,8 +2,8 @@ package network import ( "github.com/Workiva/go-datastructures/queue" - "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" "go.uber.org/zap" ) @@ -11,11 +11,11 @@ type blockQueue struct { log *zap.Logger queue *queue.PriorityQueue checkBlocks chan struct{} - chain core.Blockchainer + chain blockchainer.Blockchainer relayF func(*block.Block) } -func newBlockQueue(capacity int, bc core.Blockchainer, log *zap.Logger, relayer func(*block.Block)) *blockQueue { +func newBlockQueue(capacity int, bc blockchainer.Blockchainer, log *zap.Logger, relayer func(*block.Block)) *blockQueue { if log == nil { return nil } diff --git a/pkg/network/server.go b/pkg/network/server.go index efd6d5e3f..729995067 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -13,6 +13,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/consensus" "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/transaction" "github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/util" @@ -53,7 +54,7 @@ type ( transport Transporter discovery Discoverer - chain core.Blockchainer + chain blockchainer.Blockchainer bQueue *blockQueue consensus consensus.Service @@ -84,7 +85,7 @@ func randomID() uint32 { } // NewServer returns a new Server, initialized with the given configuration. -func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) (*Server, error) { +func NewServer(config ServerConfig, chain blockchainer.Blockchainer, log *zap.Logger) (*Server, error) { if log == nil { return nil, errors.New("logger is a required parameter") } diff --git a/pkg/rpc/response/result/block.go b/pkg/rpc/response/result/block.go index e021cc64a..8f02a2999 100644 --- a/pkg/rpc/response/result/block.go +++ b/pkg/rpc/response/result/block.go @@ -5,8 +5,8 @@ import ( "errors" "fmt" - "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/transaction" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/io" @@ -50,7 +50,7 @@ type ( ) // NewBlock creates a new Block wrapper. -func NewBlock(b *block.Block, chain core.Blockchainer) Block { +func NewBlock(b *block.Block, chain blockchainer.Blockchainer) Block { res := Block{ Version: b.Version, Hash: b.Hash(), diff --git a/pkg/rpc/response/result/block_header.go b/pkg/rpc/response/result/block_header.go index f333d54de..886903500 100644 --- a/pkg/rpc/response/result/block_header.go +++ b/pkg/rpc/response/result/block_header.go @@ -3,8 +3,8 @@ package result import ( "strconv" - "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/transaction" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/io" @@ -31,7 +31,7 @@ type ( ) // NewHeader creates a new Header wrapper. -func NewHeader(h *block.Header, chain core.Blockchainer) Header { +func NewHeader(h *block.Header, chain blockchainer.Blockchainer) Header { res := Header{ Hash: h.Hash(), Size: io.GetVarSize(h), diff --git a/pkg/rpc/response/result/tx_raw_output.go b/pkg/rpc/response/result/tx_raw_output.go index d039e2eb6..3c0269022 100644 --- a/pkg/rpc/response/result/tx_raw_output.go +++ b/pkg/rpc/response/result/tx_raw_output.go @@ -4,8 +4,8 @@ import ( "encoding/json" "errors" - "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/transaction" "github.com/nspcc-dev/neo-go/pkg/util" ) @@ -27,7 +27,7 @@ type TransactionMetadata struct { } // NewTransactionOutputRaw returns a new ransactionOutputRaw object. -func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, chain core.Blockchainer) TransactionOutputRaw { +func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, chain blockchainer.Blockchainer) TransactionOutputRaw { // confirmations formula confirmations := int(chain.BlockHeight() - header.Base.Index + 1) // set index position diff --git a/pkg/rpc/response/result/unclaimed.go b/pkg/rpc/response/result/unclaimed.go index e636f5c1a..f8cd8d1a9 100644 --- a/pkg/rpc/response/result/unclaimed.go +++ b/pkg/rpc/response/result/unclaimed.go @@ -2,6 +2,7 @@ package result import ( "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/state" "github.com/nspcc-dev/neo-go/pkg/util" ) @@ -14,7 +15,7 @@ type Unclaimed struct { } // NewUnclaimed creates a new Unclaimed wrapper using given Blockchainer. -func NewUnclaimed(a *state.Account, chain core.Blockchainer) (*Unclaimed, error) { +func NewUnclaimed(a *state.Account, chain blockchainer.Blockchainer) (*Unclaimed, error) { var ( available util.Fixed8 unavailable util.Fixed8 diff --git a/pkg/rpc/response/result/unspents.go b/pkg/rpc/response/result/unspents.go index 623d2e1a5..9a1613b86 100644 --- a/pkg/rpc/response/result/unspents.go +++ b/pkg/rpc/response/result/unspents.go @@ -1,7 +1,7 @@ package result import ( - "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/state" "github.com/nspcc-dev/neo-go/pkg/util" ) @@ -29,7 +29,7 @@ var GlobalAssets = map[string]string{ } // NewUnspents creates a new Account wrapper using given Blockchainer. -func NewUnspents(a *state.Account, chain core.Blockchainer, addr string) Unspents { +func NewUnspents(a *state.Account, chain blockchainer.Blockchainer, addr string) Unspents { res := Unspents{ Address: addr, Balance: make([]UnspentBalanceInfo, 0, len(a.Balances)), diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 3d5da1e2b..87d9e3506 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -10,6 +10,7 @@ import ( "net/http" "strconv" + "github.com/nspcc-dev/neo-go/pkg/core/blockchainer" "github.com/nspcc-dev/neo-go/pkg/rpc" "github.com/nspcc-dev/neo-go/pkg/core" @@ -35,7 +36,7 @@ type ( // Server represents the JSON-RPC 2.0 server. Server struct { *http.Server - chain core.Blockchainer + chain blockchainer.Blockchainer config rpc.Config coreServer *network.Server log *zap.Logger @@ -81,7 +82,7 @@ var invalidBlockHeightError = func(index int, height int) error { } // New creates a new Server struct. -func New(chain core.Blockchainer, conf rpc.Config, coreServer *network.Server, log *zap.Logger) Server { +func New(chain blockchainer.Blockchainer, conf rpc.Config, coreServer *network.Server, log *zap.Logger) Server { httpServer := &http.Server{ Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10), }