core: refactor out MemPool

This commit is contained in:
Evgenii Stratonikov 2020-01-15 10:52:59 +03:00
parent 79bceb3e40
commit fed6fba9b6
10 changed files with 60 additions and 37 deletions

View file

@ -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)

View file

@ -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
}

View file

@ -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
}

View file

@ -1,4 +1,4 @@
package core
package mempool
import (
"github.com/CityOfZion/neo-go/pkg/core/transaction"

View file

@ -1,4 +1,4 @@
package core
package mempool
import (
"sort"

View file

@ -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{},
}
}

View file

@ -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))
}

View file

@ -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))
}

View file

@ -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")
}

View file

@ -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
}