From fed6fba9b6249d7813b5a79d0801de74625cc336 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 15 Jan 2020 10:52:59 +0300 Subject: [PATCH] core: refactor out MemPool --- pkg/consensus/consensus_test.go | 9 ++++--- pkg/core/blockchain.go | 7 ++--- pkg/core/blockchainer.go | 5 ++-- pkg/core/{ => mempool}/feer.go | 2 +- pkg/core/{ => mempool}/mem_pool.go | 2 +- pkg/core/{ => mempool}/mem_pool_test.go | 9 ++++++- pkg/core/mempool/prometheus.go | 34 +++++++++++++++++++++++++ pkg/core/prometheus.go | 23 ----------------- pkg/network/helper_test.go | 3 ++- pkg/network/server.go | 3 ++- 10 files changed, 60 insertions(+), 37 deletions(-) rename pkg/core/{ => mempool}/feer.go (96%) rename pkg/core/{ => mempool}/mem_pool.go (99%) rename pkg/core/{ => mempool}/mem_pool_test.go (94%) create mode 100644 pkg/core/mempool/prometheus.go diff --git a/pkg/consensus/consensus_test.go b/pkg/consensus/consensus_test.go index 1cd450f77..08de02dd9 100644 --- a/pkg/consensus/consensus_test.go +++ b/pkg/consensus/consensus_test.go @@ -5,6 +5,7 @@ import ( "github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/pkg/core" + "github.com/CityOfZion/neo-go/pkg/core/mempool" "github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/util" @@ -20,7 +21,7 @@ func TestNewService(t *testing.T) { Type: transaction.MinerType, Data: &transaction.MinerTX{Nonce: 12345}, } - item := core.NewPoolItem(tx, new(feer)) + item := mempool.NewPoolItem(tx, new(feer)) srv.Chain.GetMemPool().TryAdd(tx.Hash(), item) var txx []block.Transaction @@ -38,7 +39,7 @@ func TestService_GetVerified(t *testing.T) { newMinerTx(4), } pool := srv.Chain.GetMemPool() - item := core.NewPoolItem(txs[3], new(feer)) + item := mempool.NewPoolItem(txs[3], new(feer)) require.True(t, pool.TryAdd(txs[3].Hash(), item)) @@ -65,7 +66,7 @@ func TestService_GetVerified(t *testing.T) { t.Run("more than half of the last proposal will be reused", func(t *testing.T) { for _, tx := range txs[:2] { - item := core.NewPoolItem(tx, new(feer)) + item := mempool.NewPoolItem(tx, new(feer)) require.True(t, pool.TryAdd(tx.Hash(), item)) } @@ -114,7 +115,7 @@ func TestService_getTx(t *testing.T) { require.Equal(t, nil, srv.getTx(h)) - item := core.NewPoolItem(tx, new(feer)) + item := mempool.NewPoolItem(tx, new(feer)) srv.Chain.GetMemPool().TryAdd(h, item) got := srv.getTx(h) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index cd0da4930..0bf36e1dc 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -12,6 +12,7 @@ import ( "github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/pkg/core/block" + "github.com/CityOfZion/neo-go/pkg/core/mempool" "github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/transaction" @@ -77,7 +78,7 @@ type Blockchain struct { stopCh chan struct{} runToExitCh chan struct{} - memPool MemPool + memPool mempool.MemPool // cache for block verification keys. keyCache map[util.Uint160]map[string]*keys.PublicKey @@ -101,7 +102,7 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L headersOpDone: make(chan struct{}), stopCh: make(chan struct{}), runToExitCh: make(chan struct{}), - memPool: NewMemPool(50000), + memPool: mempool.NewMemPool(50000), keyCache: make(map[util.Uint160]map[string]*keys.PublicKey), log: log, } @@ -951,7 +952,7 @@ func (bc *Blockchain) IsLowPriority(t *transaction.Transaction) bool { } // GetMemPool returns the memory pool of the blockchain. -func (bc *Blockchain) GetMemPool() MemPool { +func (bc *Blockchain) GetMemPool() mempool.MemPool { return bc.memPool } diff --git a/pkg/core/blockchainer.go b/pkg/core/blockchainer.go index fdeaa6072..15ade1c18 100644 --- a/pkg/core/blockchainer.go +++ b/pkg/core/blockchainer.go @@ -3,6 +3,7 @@ package core import ( "github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/pkg/core/block" + "github.com/CityOfZion/neo-go/pkg/core/mempool" "github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/transaction" @@ -38,7 +39,7 @@ type Blockchainer interface { GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error) GetUnspentCoinState(util.Uint256) *UnspentCoinState References(t *transaction.Transaction) map[transaction.Input]*transaction.Output - Feer // fee interface + mempool.Feer // fee interface VerifyTx(*transaction.Transaction, *block.Block) error - GetMemPool() MemPool + GetMemPool() mempool.MemPool } diff --git a/pkg/core/feer.go b/pkg/core/mempool/feer.go similarity index 96% rename from pkg/core/feer.go rename to pkg/core/mempool/feer.go index 61adbe3ca..f92c0e1d1 100644 --- a/pkg/core/feer.go +++ b/pkg/core/mempool/feer.go @@ -1,4 +1,4 @@ -package core +package mempool import ( "github.com/CityOfZion/neo-go/pkg/core/transaction" diff --git a/pkg/core/mem_pool.go b/pkg/core/mempool/mem_pool.go similarity index 99% rename from pkg/core/mem_pool.go rename to pkg/core/mempool/mem_pool.go index 2a629d403..404149a2a 100644 --- a/pkg/core/mem_pool.go +++ b/pkg/core/mempool/mem_pool.go @@ -1,4 +1,4 @@ -package core +package mempool import ( "sort" diff --git a/pkg/core/mem_pool_test.go b/pkg/core/mempool/mem_pool_test.go similarity index 94% rename from pkg/core/mem_pool_test.go rename to pkg/core/mempool/mem_pool_test.go index b75419d17..6da40043b 100644 --- a/pkg/core/mem_pool_test.go +++ b/pkg/core/mempool/mem_pool_test.go @@ -1,4 +1,4 @@ -package core +package mempool import ( "testing" @@ -88,3 +88,10 @@ func TestMemPoolVerify(t *testing.T) { tx3.Inputs = append(tx3.Inputs, transaction.Input{PrevHash: inhash2, PrevIndex: 0}) require.Equal(t, false, mp.Verify(tx3)) } + +func newMinerTX() *transaction.Transaction { + return &transaction.Transaction{ + Type: transaction.MinerType, + Data: &transaction.MinerTX{}, + } +} diff --git a/pkg/core/mempool/prometheus.go b/pkg/core/mempool/prometheus.go new file mode 100644 index 000000000..3d8a6a585 --- /dev/null +++ b/pkg/core/mempool/prometheus.go @@ -0,0 +1,34 @@ +package mempool + +import "github.com/prometheus/client_golang/prometheus" + +var ( + //mempoolUnsortedTx prometheus metric. + mempoolUnsortedTx = prometheus.NewGauge( + prometheus.GaugeOpts{ + Help: "Mempool Unsorted TXs", + Name: "mempool_unsorted_tx", + Namespace: "neogo", + }, + ) + //mempoolUnverifiedTx prometheus metric. + mempoolUnverifiedTx = prometheus.NewGauge( + prometheus.GaugeOpts{ + Help: "Mempool Unverified TXs", + Name: "mempool_unverified_tx", + Namespace: "neogo", + }, + ) +) + +func init() { + prometheus.MustRegister( + mempoolUnsortedTx, + mempoolUnverifiedTx, + ) +} + +func updateMempoolMetrics(unsortedTxnLen int, unverifiedTxnLen int) { + mempoolUnsortedTx.Set(float64(unsortedTxnLen)) + mempoolUnverifiedTx.Set(float64(unverifiedTxnLen)) +} diff --git a/pkg/core/prometheus.go b/pkg/core/prometheus.go index b4bec8ca9..b81fb847d 100644 --- a/pkg/core/prometheus.go +++ b/pkg/core/prometheus.go @@ -30,22 +30,6 @@ var ( Namespace: "neogo", }, ) - //mempoolUnsortedTx prometheus metric. - mempoolUnsortedTx = prometheus.NewGauge( - prometheus.GaugeOpts{ - Help: "Mempool Unsorted TXs", - Name: "mempool_unsorted_tx", - Namespace: "neogo", - }, - ) - //mempoolUnverifiedTx prometheus metric. - mempoolUnverifiedTx = prometheus.NewGauge( - prometheus.GaugeOpts{ - Help: "Mempool Unverified TXs", - Name: "mempool_unverified_tx", - Namespace: "neogo", - }, - ) ) func init() { @@ -53,8 +37,6 @@ func init() { blockHeight, persistedHeight, headerHeight, - mempoolUnsortedTx, - mempoolUnverifiedTx, ) } @@ -69,8 +51,3 @@ func updateHeaderHeightMetric(hHeight int) { func updateBlockHeightMetric(bHeight uint32) { blockHeight.Set(float64(bHeight)) } - -func updateMempoolMetrics(unsortedTxnLen int, unverifiedTxnLen int) { - mempoolUnsortedTx.Set(float64(unsortedTxnLen)) - mempoolUnverifiedTx.Set(float64(unverifiedTxnLen)) -} diff --git a/pkg/network/helper_test.go b/pkg/network/helper_test.go index e71fde016..e78b8c20f 100644 --- a/pkg/network/helper_test.go +++ b/pkg/network/helper_test.go @@ -10,6 +10,7 @@ import ( "github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/block" + "github.com/CityOfZion/neo-go/pkg/core/mempool" "github.com/CityOfZion/neo-go/pkg/core/state" "github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/transaction" @@ -116,7 +117,7 @@ func (chain testChain) GetUnspentCoinState(util.Uint256) *core.UnspentCoinState panic("TODO") } -func (chain testChain) GetMemPool() core.MemPool { +func (chain testChain) GetMemPool() mempool.MemPool { panic("TODO") } diff --git a/pkg/network/server.go b/pkg/network/server.go index dbda99c9f..82eed386b 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -13,6 +13,7 @@ import ( "github.com/CityOfZion/neo-go/pkg/consensus" "github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/block" + "github.com/CityOfZion/neo-go/pkg/core/mempool" "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/network/payload" "github.com/CityOfZion/neo-go/pkg/util" @@ -709,7 +710,7 @@ func (s *Server) RelayTxn(t *transaction.Transaction) RelayReason { // TODO: Implement Plugin.CheckPolicy? //if (!Plugin.CheckPolicy(transaction)) // return RelayResultReason.PolicyFail; - if ok := s.chain.GetMemPool().TryAdd(t.Hash(), core.NewPoolItem(t, s.chain)); !ok { + if ok := s.chain.GetMemPool().TryAdd(t.Hash(), mempool.NewPoolItem(t, s.chain)); !ok { return RelayOutOfMemory }