Implement new ethereum mempool sync with tx timeout

This commit is contained in:
Martin Boehm 2019-04-04 23:35:38 +02:00
parent 4435dbbfb4
commit 3f973bf47d
3 changed files with 140 additions and 86 deletions

View File

@ -67,33 +67,33 @@ func (a MempoolTxidEntries) Less(i, j int) bool {
func (m *BaseMempool) updateMappings(newTxEntries map[string]txEntry, newAddrDescToTx map[string][]Outpoint) { func (m *BaseMempool) updateMappings(newTxEntries map[string]txEntry, newAddrDescToTx map[string][]Outpoint) {
m.mux.Lock() m.mux.Lock()
defer m.mux.Unlock()
m.txEntries = newTxEntries m.txEntries = newTxEntries
m.addrDescToTx = newAddrDescToTx m.addrDescToTx = newAddrDescToTx
m.mux.Unlock()
} }
func getAllEntries(txEntries map[string]txEntry) MempoolTxidEntries { // GetAllEntries returns all mempool entries sorted by fist seen time in descending order
a := make(MempoolTxidEntries, len(txEntries)) func (m *BaseMempool) GetAllEntries() MempoolTxidEntries {
entries := make(MempoolTxidEntries, len(m.txEntries))
i := 0 i := 0
for txid, entry := range txEntries { m.mux.Lock()
a[i] = MempoolTxidEntry{ for txid, entry := range m.txEntries {
entries[i] = MempoolTxidEntry{
Txid: txid, Txid: txid,
Time: entry.time, Time: entry.time,
} }
i++ i++
} }
sort.Sort(a) m.mux.Unlock()
return a sort.Sort(entries)
} return entries
// GetAllEntries returns all mempool entries sorted by fist seen time in descending order
func (m *BaseMempool) GetAllEntries() MempoolTxidEntries {
return getAllEntries(m.txEntries)
} }
// GetTransactionTime returns first seen time of a transaction // GetTransactionTime returns first seen time of a transaction
func (m *BaseMempool) GetTransactionTime(txid string) uint32 { func (m *BaseMempool) GetTransactionTime(txid string) uint32 {
m.mux.Lock()
e, found := m.txEntries[txid] e, found := m.txEntries[txid]
m.mux.Unlock()
if !found { if !found {
return 0 return 0
} }

View File

@ -41,22 +41,21 @@ type Configuration struct {
// EthereumRPC is an interface to JSON-RPC eth service. // EthereumRPC is an interface to JSON-RPC eth service.
type EthereumRPC struct { type EthereumRPC struct {
*bchain.BaseChain *bchain.BaseChain
client *ethclient.Client client *ethclient.Client
rpc *rpc.Client rpc *rpc.Client
timeout time.Duration timeout time.Duration
Parser *EthereumParser Parser *EthereumParser
Mempool *bchain.MempoolEthereumType Mempool *bchain.MempoolEthereumType
bestHeaderLock sync.Mutex mempoolInitialized bool
bestHeader *ethtypes.Header bestHeaderLock sync.Mutex
bestHeaderTime time.Time bestHeader *ethtypes.Header
chanNewBlock chan *ethtypes.Header bestHeaderTime time.Time
newBlockSubscription *rpc.ClientSubscription chanNewBlock chan *ethtypes.Header
chanNewTx chan ethcommon.Hash newBlockSubscription *rpc.ClientSubscription
newTxSubscription *rpc.ClientSubscription chanNewTx chan ethcommon.Hash
pendingTransactions map[string]struct{} newTxSubscription *rpc.ClientSubscription
pendingTransactionsLock sync.Mutex ChainConfig *Configuration
ChainConfig *Configuration isETC bool
isETC bool
} }
// NewEthereumRPC returns new EthRPC instance. // NewEthereumRPC returns new EthRPC instance.
@ -78,11 +77,10 @@ func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.Notification
ec := ethclient.NewClient(rc) ec := ethclient.NewClient(rc)
s := &EthereumRPC{ s := &EthereumRPC{
BaseChain: &bchain.BaseChain{}, BaseChain: &bchain.BaseChain{},
client: ec, client: ec,
rpc: rc, rpc: rc,
ChainConfig: &c, ChainConfig: &c,
pendingTransactions: make(map[string]struct{}),
} }
// always create parser // always create parser
@ -125,9 +123,7 @@ func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.Notification
if glog.V(2) { if glog.V(2) {
glog.Info("rpc: new tx ", hex) glog.Info("rpc: new tx ", hex)
} }
s.pendingTransactionsLock.Lock() s.Mempool.AddTransactionToMempool(hex)
s.pendingTransactions[hex] = struct{}{}
s.pendingTransactionsLock.Unlock()
pushHandler(bchain.NotificationNewTx) pushHandler(bchain.NotificationNewTx)
} }
}() }()
@ -176,7 +172,18 @@ func (b *EthereumRPC) InitializeMempool(addrDescForOutpoint bchain.AddrDescForOu
if b.Mempool == nil { if b.Mempool == nil {
return errors.New("Mempool not created") return errors.New("Mempool not created")
} }
// get initial mempool transactions
txs, err := b.GetMempoolTransactions()
if err != nil {
return err
}
for _, txid := range txs {
b.Mempool.AddTransactionToMempool(txid)
}
b.Mempool.OnNewTxAddr = onNewTxAddr b.Mempool.OnNewTxAddr = onNewTxAddr
if b.isETC { if b.isETC {
glog.Info(b.ChainConfig.CoinName, " does not support subscription to newHeads") glog.Info(b.ChainConfig.CoinName, " does not support subscription to newHeads")
} else { } else {
@ -213,6 +220,9 @@ func (b *EthereumRPC) InitializeMempool(addrDescForOutpoint bchain.AddrDescForOu
}); err != nil { }); err != nil {
return err return err
} }
b.mempoolInitialized = true
return nil return nil
} }
@ -495,9 +505,9 @@ func (b *EthereumRPC) GetBlock(hash string, height uint32) (*bchain.Block, error
return nil, errors.Annotatef(err, "hash %v, height %v, txid %v", hash, height, tx.Hash) return nil, errors.Annotatef(err, "hash %v, height %v, txid %v", hash, height, tx.Hash)
} }
btxs[i] = *btx btxs[i] = *btx
b.pendingTransactionsLock.Lock() if b.mempoolInitialized {
delete(b.pendingTransactions, tx.Hash) b.Mempool.RemoveTransactionFromMempool(tx.Hash)
b.pendingTransactionsLock.Unlock() }
} }
bbk := bchain.Block{ bbk := bchain.Block{
BlockHeader: *bbh, BlockHeader: *bbh,
@ -535,14 +545,7 @@ func (b *EthereumRPC) GetBlockInfo(hash string) (*bchain.BlockInfo, error) {
// GetTransactionForMempool returns a transaction by the transaction ID. // GetTransactionForMempool returns a transaction by the transaction ID.
// It could be optimized for mempool, i.e. without block time and confirmations // It could be optimized for mempool, i.e. without block time and confirmations
func (b *EthereumRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) { func (b *EthereumRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) {
tx, err := b.GetTransaction(txid) return b.GetTransaction(txid)
// if there is an error getting the tx or the tx is confirmed, remove it from pending transactions
if err == bchain.ErrTxNotFound || (tx != nil && tx.Confirmations > 0) {
b.pendingTransactionsLock.Lock()
delete(b.pendingTransactions, txid)
b.pendingTransactionsLock.Unlock()
}
return tx, err
} }
// GetTransaction returns a transaction by the transaction ID. // GetTransaction returns a transaction by the transaction ID.
@ -555,6 +558,9 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if tx == nil { } else if tx == nil {
if b.mempoolInitialized {
b.Mempool.RemoveTransactionFromMempool(txid)
}
return nil, bchain.ErrTxNotFound return nil, bchain.ErrTxNotFound
} }
var btx *bchain.Tx var btx *bchain.Tx
@ -621,6 +627,10 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) {
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid) return nil, errors.Annotatef(err, "txid %v", txid)
} }
// remove tx from mempool if it is there
if b.mempoolInitialized {
b.Mempool.RemoveTransactionFromMempool(txid)
}
} }
return btx, nil return btx, nil
} }
@ -654,19 +664,7 @@ func (b *EthereumRPC) GetMempoolTransactions() ([]string, error) {
return nil, err return nil, err
} }
} }
b.pendingTransactionsLock.Lock() return body.Transactions, nil
// join transactions returned by getBlockRaw with pendingTransactions from subscription
for _, txid := range body.Transactions {
b.pendingTransactions[txid] = struct{}{}
}
txids := make([]string, len(b.pendingTransactions))
i := 0
for txid := range b.pendingTransactions {
txids[i] = txid
i++
}
b.pendingTransactionsLock.Unlock()
return txids, nil
} }
// EstimateFee returns fee estimation // EstimateFee returns fee estimation

View File

@ -6,14 +6,25 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
) )
const mempoolTimeoutTime = 24 * time.Hour
const mempoolTimeoutRunPeriod = 10 * time.Minute
// MempoolEthereumType is mempool handle of EthereumType chains // MempoolEthereumType is mempool handle of EthereumType chains
type MempoolEthereumType struct { type MempoolEthereumType struct {
BaseMempool BaseMempool
nextTimeoutRun time.Time
} }
// NewMempoolEthereumType creates new mempool handler. // NewMempoolEthereumType creates new mempool handler.
func NewMempoolEthereumType(chain BlockChain) *MempoolEthereumType { func NewMempoolEthereumType(chain BlockChain) *MempoolEthereumType {
return &MempoolEthereumType{BaseMempool: BaseMempool{chain: chain}} return &MempoolEthereumType{
BaseMempool: BaseMempool{
chain: chain,
txEntries: make(map[string]txEntry),
addrDescToTx: make(map[string][]Outpoint),
},
nextTimeoutRun: time.Now().Add(mempoolTimeoutTime),
}
} }
func appendAddress(io []addrIndex, i int32, a string, parser BlockChainParser) []addrIndex { func appendAddress(io []addrIndex, i int32, a string, parser BlockChainParser) []addrIndex {
@ -76,35 +87,80 @@ func (m *MempoolEthereumType) createTxEntry(txid string, txTime uint32) (txEntry
return txEntry{addrIndexes: addrIndexes, time: txTime}, true return txEntry{addrIndexes: addrIndexes, time: txTime}, true
} }
// Resync gets mempool transactions and maps outputs to transactions. // Resync ethereum type removes timed out transactions and returns number of transactions in mempool.
// Resync is not reentrant, it should be called from a single thread. // Transactions are added/removed by AddTransactionToMempool/RemoveTransactionFromMempool methods
// Read operations (GetTransactions) are safe.
func (m *MempoolEthereumType) Resync() (int, error) { func (m *MempoolEthereumType) Resync() (int, error) {
start := time.Now() m.mux.Lock()
glog.V(1).Info("Mempool: resync") entries := len(m.txEntries)
txs, err := m.chain.GetMempoolTransactions() now := time.Now()
if err != nil { if m.nextTimeoutRun.Before(now) {
return 0, err threshold := now.Add(-mempoolTimeoutTime)
} for txid, entry := range m.txEntries {
// allocate slightly larger capacity of the maps if time.Unix(int64(entry.time), 0).Before(threshold) {
newTxEntries := make(map[string]txEntry, len(m.txEntries)+5) m.removeEntryFromMempool(txid, entry)
newAddrDescToTx := make(map[string][]Outpoint, len(m.addrDescToTx)+5)
txTime := uint32(time.Now().Unix())
var ok bool
for _, txid := range txs {
entry, exists := m.txEntries[txid]
if !exists {
entry, ok = m.createTxEntry(txid, txTime)
if !ok {
continue
} }
} }
newTxEntries[txid] = entry removed := entries - len(m.txEntries)
entries = len(m.txEntries)
glog.Info("Mempool: cleanup, removed ", removed, " transactions from mempool")
m.nextTimeoutRun = now.Add(mempoolTimeoutRunPeriod)
}
m.mux.Unlock()
glog.Info("Mempool: resync ", entries, " transactions in mempool")
return entries, nil
}
// AddTransactionToMempool adds transactions to mempool
func (m *MempoolEthereumType) AddTransactionToMempool(txid string) {
m.mux.Lock()
_, exists := m.txEntries[txid]
m.mux.Unlock()
if glog.V(1) {
glog.Info("AddTransactionToMempool ", txid, ", existed ", exists)
}
if !exists {
entry, ok := m.createTxEntry(txid, uint32(time.Now().Unix()))
if !ok {
return
}
m.mux.Lock()
m.txEntries[txid] = entry
for _, si := range entry.addrIndexes { for _, si := range entry.addrIndexes {
newAddrDescToTx[si.addrDesc] = append(newAddrDescToTx[si.addrDesc], Outpoint{txid, si.n}) m.addrDescToTx[si.addrDesc] = append(m.addrDescToTx[si.addrDesc], Outpoint{txid, si.n})
}
m.mux.Unlock()
}
}
func (m *MempoolEthereumType) removeEntryFromMempool(txid string, entry txEntry) {
delete(m.txEntries, txid)
for _, si := range entry.addrIndexes {
outpoints, found := m.addrDescToTx[si.addrDesc]
if found {
newOutpoints := make([]Outpoint, 0, len(outpoints)-1)
for _, o := range outpoints {
if o.Txid != txid {
newOutpoints = append(newOutpoints, o)
}
}
if len(newOutpoints) > 0 {
m.addrDescToTx[si.addrDesc] = newOutpoints
} else {
delete(m.addrDescToTx, si.addrDesc)
}
} }
} }
m.updateMappings(newTxEntries, newAddrDescToTx) }
glog.Info("Mempool: resync finished in ", time.Since(start), ", ", len(m.txEntries), " transactions in mempool")
return len(m.txEntries), nil // RemoveTransactionFromMempool removes transaction from mempool
func (m *MempoolEthereumType) RemoveTransactionFromMempool(txid string) {
m.mux.Lock()
entry, exists := m.txEntries[txid]
if glog.V(1) {
glog.Info("RemoveTransactionFromMempool ", txid, ", existed ", exists)
}
if exists {
m.removeEntryFromMempool(txid, entry)
}
m.mux.Unlock()
} }