Implement GetAllTxs function in mempool
This commit is contained in:
parent
00b041fbb0
commit
c0e69ff1d0
@ -3,7 +3,6 @@ package pool
|
||||
import (
|
||||
"encoding/hex"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
types2 "github.com/Secured-Finance/dione/blockchain/types"
|
||||
@ -19,58 +18,26 @@ const (
|
||||
)
|
||||
|
||||
type Mempool struct {
|
||||
m sync.RWMutex
|
||||
cache cache.Cache
|
||||
txDescriptors []string // list of txs in cache
|
||||
}
|
||||
|
||||
func NewMempool(c cache.Cache) (*Mempool, error) {
|
||||
func NewMempool() (*Mempool, error) {
|
||||
mp := &Mempool{
|
||||
cache: c,
|
||||
cache: cache.NewInMemoryCache(), // here we need to use separate cache
|
||||
}
|
||||
|
||||
var txDesc []string
|
||||
err := c.Get("tx_list", &txDesc)
|
||||
if err != nil || err != cache.ErrNilValue {
|
||||
return nil, err
|
||||
}
|
||||
mp.txDescriptors = txDesc
|
||||
|
||||
return mp, nil
|
||||
}
|
||||
|
||||
func (mp *Mempool) StoreTx(tx *types2.Transaction) error {
|
||||
mp.m.Lock()
|
||||
defer mp.m.Unlock()
|
||||
|
||||
hashStr := hex.EncodeToString(tx.Hash)
|
||||
err := mp.cache.StoreWithTTL(DefaultTxPrefix+hashStr, tx, DefaultTxTTL)
|
||||
mp.txDescriptors = append(mp.txDescriptors, hashStr)
|
||||
mp.cache.Store("tx_list", mp.txDescriptors) // update tx list in cache
|
||||
return err
|
||||
}
|
||||
|
||||
func (mp *Mempool) GetTxsForNewBlock() []*types2.Transaction {
|
||||
mp.m.Lock()
|
||||
defer mp.m.Unlock()
|
||||
|
||||
var txForBlock []*types2.Transaction
|
||||
var allTxs []*types2.Transaction
|
||||
|
||||
for i, v := range mp.txDescriptors {
|
||||
var tx types2.Transaction
|
||||
err := mp.cache.Get(DefaultTxPrefix+v, &tx)
|
||||
if err != nil {
|
||||
if err == cache.ErrNilValue {
|
||||
// descriptor is broken
|
||||
// delete it and update list
|
||||
mp.txDescriptors = removeItemFromStringSlice(mp.txDescriptors, i)
|
||||
mp.cache.Store("tx_list", mp.txDescriptors) // update tx list in cache
|
||||
}
|
||||
continue
|
||||
}
|
||||
allTxs = append(allTxs, &tx)
|
||||
}
|
||||
allTxs := mp.GetAllTxs()
|
||||
sort.Slice(allTxs, func(i, j int) bool {
|
||||
return allTxs[i].Timestamp.Before(allTxs[j].Timestamp)
|
||||
})
|
||||
@ -87,6 +54,16 @@ func (mp *Mempool) GetTxsForNewBlock() []*types2.Transaction {
|
||||
return txForBlock
|
||||
}
|
||||
|
||||
func (mp *Mempool) GetAllTxs() []*types2.Transaction {
|
||||
var allTxs []*types2.Transaction
|
||||
|
||||
for _, v := range mp.cache.Items() {
|
||||
tx := v.(types2.Transaction)
|
||||
allTxs = append(allTxs, &tx)
|
||||
}
|
||||
return allTxs
|
||||
}
|
||||
|
||||
func removeItemFromStringSlice(s []string, i int) []string {
|
||||
s[len(s)-1], s[i] = s[i], s[len(s)-1]
|
||||
return s[:len(s)-1]
|
||||
|
1
cache/cache.go
vendored
1
cache/cache.go
vendored
@ -12,4 +12,5 @@ type Cache interface {
|
||||
StoreWithTTL(key string, value interface{}, ttl time.Duration) error
|
||||
Get(key string, value interface{}) error
|
||||
Delete(key string)
|
||||
Items() map[string]interface{}
|
||||
}
|
||||
|
10
cache/inmemory_cache.go
vendored
10
cache/inmemory_cache.go
vendored
@ -15,7 +15,7 @@ type InMemoryCache struct {
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
func NewInMemoryCache() *InMemoryCache {
|
||||
func NewInMemoryCache() Cache {
|
||||
return &InMemoryCache{
|
||||
cache: cache.New(DefaultCacheExpiration, DefaultGCInterval),
|
||||
}
|
||||
@ -45,3 +45,11 @@ func (imc *InMemoryCache) Get(key string, value interface{}) error {
|
||||
func (imc *InMemoryCache) Delete(key string) {
|
||||
imc.cache.Delete(key)
|
||||
}
|
||||
|
||||
func (imc *InMemoryCache) Items() map[string]interface{} {
|
||||
m := make(map[string]interface{})
|
||||
for k, v := range imc.cache.Items() {
|
||||
m[k] = v.Object
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
4
cache/redis_cache.go
vendored
4
cache/redis_cache.go
vendored
@ -81,3 +81,7 @@ func (rc *RedisCache) Get(key string, value interface{}) error {
|
||||
func (rc *RedisCache) Delete(key string) {
|
||||
rc.Client.Del(rc.ctx, key)
|
||||
}
|
||||
|
||||
func (rc *RedisCache) Items() map[string]interface{} {
|
||||
return nil // TODO
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user