mempool: simplify names of exported types

With the move to a separate package, naming can be simplified:
MemPool -> Pool, PoolItem -> Item, PoolItems -> Items.
This commit is contained in:
Evgenii Stratonikov 2020-01-15 15:10:05 +03:00
parent fed6fba9b6
commit 28183b81d6
4 changed files with 51 additions and 51 deletions

View file

@ -78,7 +78,7 @@ type Blockchain struct {
stopCh chan struct{} stopCh chan struct{}
runToExitCh chan struct{} runToExitCh chan struct{}
memPool mempool.MemPool memPool mempool.Pool
// cache for block verification keys. // cache for block verification keys.
keyCache map[util.Uint160]map[string]*keys.PublicKey keyCache map[util.Uint160]map[string]*keys.PublicKey
@ -952,7 +952,7 @@ func (bc *Blockchain) IsLowPriority(t *transaction.Transaction) bool {
} }
// GetMemPool returns the memory pool of the blockchain. // GetMemPool returns the memory pool of the blockchain.
func (bc *Blockchain) GetMemPool() mempool.MemPool { func (bc *Blockchain) GetMemPool() mempool.Pool {
return bc.memPool return bc.memPool
} }

View file

@ -41,5 +41,5 @@ type Blockchainer interface {
References(t *transaction.Transaction) map[transaction.Input]*transaction.Output References(t *transaction.Transaction) map[transaction.Input]*transaction.Output
mempool.Feer // fee interface mempool.Feer // fee interface
VerifyTx(*transaction.Transaction, *block.Block) error VerifyTx(*transaction.Transaction, *block.Block) error
GetMemPool() mempool.MemPool GetMemPool() mempool.Pool
} }

View file

@ -9,38 +9,38 @@ import (
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
) )
// PoolItem represents a transaction in the the Memory pool. // Item represents a transaction in the the Memory pool.
type PoolItem struct { type Item struct {
txn *transaction.Transaction txn *transaction.Transaction
timeStamp time.Time timeStamp time.Time
fee Feer fee Feer
} }
// PoolItems slice of PoolItem. // Items is a slice of Item.
type PoolItems []*PoolItem type Items []*Item
// MemPool stores the unconfirms transactions. // Pool stores the unconfirms transactions.
type MemPool struct { type Pool struct {
lock *sync.RWMutex lock *sync.RWMutex
unsortedTxn map[util.Uint256]*PoolItem unsortedTxn map[util.Uint256]*Item
unverifiedTxn map[util.Uint256]*PoolItem unverifiedTxn map[util.Uint256]*Item
sortedHighPrioTxn PoolItems sortedHighPrioTxn Items
sortedLowPrioTxn PoolItems sortedLowPrioTxn Items
unverifiedSortedHighPrioTxn PoolItems unverifiedSortedHighPrioTxn Items
unverifiedSortedLowPrioTxn PoolItems unverifiedSortedLowPrioTxn Items
capacity int capacity int
} }
func (p PoolItems) Len() int { return len(p) } func (p Items) Len() int { return len(p) }
func (p PoolItems) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p Items) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PoolItems) Less(i, j int) bool { return p[i].CompareTo(p[j]) < 0 } func (p Items) Less(i, j int) bool { return p[i].CompareTo(p[j]) < 0 }
// CompareTo returns the difference between two PoolItems. // CompareTo returns the difference between two Items.
// difference < 0 implies p < otherP. // difference < 0 implies p < otherP.
// difference = 0 implies p = otherP. // difference = 0 implies p = otherP.
// difference > 0 implies p > otherP. // difference > 0 implies p > otherP.
func (p PoolItem) CompareTo(otherP *PoolItem) int { func (p Item) CompareTo(otherP *Item) int {
if otherP == nil { if otherP == nil {
return 1 return 1
} }
@ -77,15 +77,15 @@ func (p PoolItem) CompareTo(otherP *PoolItem) int {
} }
// Count returns the total number of uncofirm transactions. // Count returns the total number of uncofirm transactions.
func (mp MemPool) Count() int { func (mp Pool) Count() int {
mp.lock.RLock() mp.lock.RLock()
defer mp.lock.RUnlock() defer mp.lock.RUnlock()
return len(mp.unsortedTxn) + len(mp.unverifiedTxn) return len(mp.unsortedTxn) + len(mp.unverifiedTxn)
} }
// ContainsKey checks if a transactions hash is in the MemPool. // ContainsKey checks if a transactions hash is in the Pool.
func (mp MemPool) ContainsKey(hash util.Uint256) bool { func (mp Pool) ContainsKey(hash util.Uint256) bool {
mp.lock.RLock() mp.lock.RLock()
defer mp.lock.RUnlock() defer mp.lock.RUnlock()
@ -100,9 +100,9 @@ func (mp MemPool) ContainsKey(hash util.Uint256) bool {
return false return false
} }
// TryAdd try to add the PoolItem to the MemPool. // TryAdd try to add the Item to the Pool.
func (mp MemPool) TryAdd(hash util.Uint256, pItem *PoolItem) bool { func (mp Pool) TryAdd(hash util.Uint256, pItem *Item) bool {
var pool PoolItems var pool Items
mp.lock.Lock() mp.lock.Lock()
if _, ok := mp.unsortedTxn[hash]; ok { if _, ok := mp.unsortedTxn[hash]; ok {
@ -135,13 +135,13 @@ func (mp MemPool) TryAdd(hash util.Uint256, pItem *PoolItem) bool {
// Remove removes an item from the mempool, if it exists there (and does // Remove removes an item from the mempool, if it exists there (and does
// nothing if it doesn't). // nothing if it doesn't).
func (mp *MemPool) Remove(hash util.Uint256) { func (mp *Pool) Remove(hash util.Uint256) {
var mapAndPools = []struct { var mapAndPools = []struct {
unsortedMap map[util.Uint256]*PoolItem unsortedMap map[util.Uint256]*Item
sortedPools []*PoolItems sortedPools []*Items
}{ }{
{unsortedMap: mp.unsortedTxn, sortedPools: []*PoolItems{&mp.sortedHighPrioTxn, &mp.sortedLowPrioTxn}}, {unsortedMap: mp.unsortedTxn, sortedPools: []*Items{&mp.sortedHighPrioTxn, &mp.sortedLowPrioTxn}},
{unsortedMap: mp.unverifiedTxn, sortedPools: []*PoolItems{&mp.unverifiedSortedHighPrioTxn, &mp.unverifiedSortedLowPrioTxn}}, {unsortedMap: mp.unverifiedTxn, sortedPools: []*Items{&mp.unverifiedSortedHighPrioTxn, &mp.unverifiedSortedLowPrioTxn}},
} }
mp.lock.Lock() mp.lock.Lock()
for _, mapAndPool := range mapAndPools { for _, mapAndPool := range mapAndPools {
@ -149,7 +149,7 @@ func (mp *MemPool) Remove(hash util.Uint256) {
delete(mapAndPool.unsortedMap, hash) delete(mapAndPool.unsortedMap, hash)
for _, pool := range mapAndPool.sortedPools { for _, pool := range mapAndPool.sortedPools {
var num int var num int
var item *PoolItem var item *Item
for num, item = range *pool { for num, item = range *pool {
if hash.Equals(item.txn.Hash()) { if hash.Equals(item.txn.Hash()) {
break break
@ -168,8 +168,8 @@ func (mp *MemPool) Remove(hash util.Uint256) {
} }
// RemoveOverCapacity removes transactions with lowest fees until the total number of transactions // RemoveOverCapacity removes transactions with lowest fees until the total number of transactions
// in the MemPool is within the capacity of the MemPool. // in the Pool is within the capacity of the Pool.
func (mp *MemPool) RemoveOverCapacity() { func (mp *Pool) RemoveOverCapacity() {
for mp.Count()-mp.capacity > 0 { for mp.Count()-mp.capacity > 0 {
mp.lock.Lock() mp.lock.Lock()
if minItem, argPosition := getLowestFeeTransaction(mp.sortedLowPrioTxn, mp.unverifiedSortedLowPrioTxn); minItem != nil { if minItem, argPosition := getLowestFeeTransaction(mp.sortedLowPrioTxn, mp.unverifiedSortedLowPrioTxn); minItem != nil {
@ -205,27 +205,27 @@ func (mp *MemPool) RemoveOverCapacity() {
} }
// NewPoolItem returns a new PoolItem. // NewPoolItem returns a new Item.
func NewPoolItem(t *transaction.Transaction, fee Feer) *PoolItem { func NewPoolItem(t *transaction.Transaction, fee Feer) *Item {
return &PoolItem{ return &Item{
txn: t, txn: t,
timeStamp: time.Now().UTC(), timeStamp: time.Now().UTC(),
fee: fee, fee: fee,
} }
} }
// NewMemPool returns a new MemPool struct. // NewMemPool returns a new Pool struct.
func NewMemPool(capacity int) MemPool { func NewMemPool(capacity int) Pool {
return MemPool{ return Pool{
lock: new(sync.RWMutex), lock: new(sync.RWMutex),
unsortedTxn: make(map[util.Uint256]*PoolItem), unsortedTxn: make(map[util.Uint256]*Item),
unverifiedTxn: make(map[util.Uint256]*PoolItem), unverifiedTxn: make(map[util.Uint256]*Item),
capacity: capacity, capacity: capacity,
} }
} }
// TryGetValue returns a transaction if it exists in the memory pool. // TryGetValue returns a transaction if it exists in the memory pool.
func (mp MemPool) TryGetValue(hash util.Uint256) (*transaction.Transaction, bool) { func (mp Pool) TryGetValue(hash util.Uint256) (*transaction.Transaction, bool) {
mp.lock.RLock() mp.lock.RLock()
defer mp.lock.RUnlock() defer mp.lock.RUnlock()
if pItem, ok := mp.unsortedTxn[hash]; ok { if pItem, ok := mp.unsortedTxn[hash]; ok {
@ -239,13 +239,13 @@ func (mp MemPool) TryGetValue(hash util.Uint256) (*transaction.Transaction, bool
return nil, false return nil, false
} }
// getLowestFeeTransaction returns the PoolItem with the lowest fee amongst the "verifiedTxnSorted" // getLowestFeeTransaction returns the Item with the lowest fee amongst the "verifiedTxnSorted"
// and "unverifiedTxnSorted" PoolItems along with a integer. The integer can assume two values, 1 and 2 which indicate // and "unverifiedTxnSorted" Items along with a integer. The integer can assume two values, 1 and 2 which indicate
// that the PoolItem with the lowest fee was found in "verifiedTxnSorted" respectively in "unverifiedTxnSorted". // that the Item with the lowest fee was found in "verifiedTxnSorted" respectively in "unverifiedTxnSorted".
// "verifiedTxnSorted" and "unverifiedTxnSorted" are sorted slice order by transaction fee ascending. This means that // "verifiedTxnSorted" and "unverifiedTxnSorted" are sorted slice order by transaction fee ascending. This means that
// the transaction with lowest fee start at index 0. // the transaction with lowest fee start at index 0.
// Reference: GetLowestFeeTransaction method in C# (https://github.com/neo-project/neo/blob/master/neo/Ledger/MemoryPool.cs) // Reference: GetLowestFeeTransaction method in C# (https://github.com/neo-project/neo/blob/master/neo/Ledger/MemoryPool.cs)
func getLowestFeeTransaction(verifiedTxnSorted PoolItems, unverifiedTxnSorted PoolItems) (*PoolItem, int) { func getLowestFeeTransaction(verifiedTxnSorted Items, unverifiedTxnSorted Items) (*Item, int) {
minItem := min(unverifiedTxnSorted) minItem := min(unverifiedTxnSorted)
verifiedMin := min(verifiedTxnSorted) verifiedMin := min(verifiedTxnSorted)
if verifiedMin == nil || (minItem != nil && verifiedMin.CompareTo(minItem) >= 0) { if verifiedMin == nil || (minItem != nil && verifiedMin.CompareTo(minItem) >= 0) {
@ -259,7 +259,7 @@ func getLowestFeeTransaction(verifiedTxnSorted PoolItems, unverifiedTxnSorted Po
// min returns the minimum item in a ascending sorted slice of pool items. // min returns the minimum item in a ascending sorted slice of pool items.
// The function can't be applied to unsorted slice! // The function can't be applied to unsorted slice!
func min(sortedPool PoolItems) *PoolItem { func min(sortedPool Items) *Item {
if len(sortedPool) == 0 { if len(sortedPool) == 0 {
return nil return nil
} }
@ -268,7 +268,7 @@ func min(sortedPool PoolItems) *PoolItem {
// GetVerifiedTransactions returns a slice of Input from all the transactions in the memory pool // GetVerifiedTransactions returns a slice of Input from all the transactions in the memory pool
// whose hash is not included in excludedHashes. // whose hash is not included in excludedHashes.
func (mp *MemPool) GetVerifiedTransactions() []*transaction.Transaction { func (mp *Pool) GetVerifiedTransactions() []*transaction.Transaction {
mp.lock.RLock() mp.lock.RLock()
defer mp.lock.RUnlock() defer mp.lock.RUnlock()
@ -286,7 +286,7 @@ func (mp *MemPool) GetVerifiedTransactions() []*transaction.Transaction {
// Verify verifies if the inputs of a transaction tx are already used in any other transaction in the memory pool. // Verify verifies if the inputs of a transaction tx are already used in any other transaction in the memory pool.
// If yes, the transaction tx is not a valid transaction and the function return false. // If yes, the transaction tx is not a valid transaction and the function return false.
// If no, the transaction tx is a valid transaction and the function return true. // If no, the transaction tx is a valid transaction and the function return true.
func (mp MemPool) Verify(tx *transaction.Transaction) bool { func (mp Pool) Verify(tx *transaction.Transaction) bool {
mp.lock.RLock() mp.lock.RLock()
defer mp.lock.RUnlock() defer mp.lock.RUnlock()
for _, item := range mp.unsortedTxn { for _, item := range mp.unsortedTxn {

View file

@ -117,7 +117,7 @@ func (chain testChain) GetUnspentCoinState(util.Uint256) *core.UnspentCoinState
panic("TODO") panic("TODO")
} }
func (chain testChain) GetMemPool() mempool.MemPool { func (chain testChain) GetMemPool() mempool.Pool {
panic("TODO") panic("TODO")
} }