blockbook/bchain/mempool_ethereum_type.go

111 lines
3.2 KiB
Go
Raw Normal View History

2018-04-03 07:51:38 -06:00
package bchain
import (
"time"
"github.com/golang/glog"
)
// MempoolEthereumType is mempool handle of EthereumType chains
type MempoolEthereumType struct {
BaseMempool
2018-04-03 07:51:38 -06:00
}
// NewMempoolEthereumType creates new mempool handler.
func NewMempoolEthereumType(chain BlockChain) *MempoolEthereumType {
return &MempoolEthereumType{BaseMempool: BaseMempool{chain: chain}}
2018-04-03 07:51:38 -06:00
}
func appendAddress(io []addrIndex, i int32, a string, parser BlockChainParser) []addrIndex {
if len(a) > 0 {
addrDesc, err := parser.GetAddrDescFromAddress(a)
if err != nil {
glog.Error("error in input addrDesc in ", a, ": ", err)
return io
}
io = append(io, addrIndex{string(addrDesc), i})
}
return io
}
2019-04-01 09:00:34 -06:00
func (m *MempoolEthereumType) createTxEntry(txid string, txTime uint32) (txEntry, bool) {
tx, err := m.chain.GetTransactionForMempool(txid)
if err != nil {
if err != ErrTxNotFound {
glog.Warning("cannot get transaction ", txid, ": ", err)
}
return txEntry{}, false
}
parser := m.chain.GetChainParser()
addrIndexes := make([]addrIndex, 0, len(tx.Vout)+len(tx.Vin))
for _, output := range tx.Vout {
addrDesc, err := parser.GetAddrDescFromVout(&output)
if err != nil {
if err != ErrAddressMissing {
glog.Error("error in output addrDesc in ", txid, " ", output.N, ": ", err)
}
continue
}
if len(addrDesc) > 0 {
addrIndexes = append(addrIndexes, addrIndex{string(addrDesc), int32(output.N)})
}
}
for _, input := range tx.Vin {
for i, a := range input.Addresses {
addrIndexes = appendAddress(addrIndexes, ^int32(i), a, parser)
}
}
t, err := parser.EthereumTypeGetErc20FromTx(tx)
if err != nil {
glog.Error("GetErc20FromTx for tx ", txid, ", ", err)
} else {
for i := range t {
addrIndexes = appendAddress(addrIndexes, ^int32(i+1), t[i].From, parser)
addrIndexes = appendAddress(addrIndexes, int32(i+1), t[i].To, parser)
}
}
if m.OnNewTxAddr != nil {
sent := make(map[string]struct{})
for _, si := range addrIndexes {
if _, found := sent[si.addrDesc]; !found {
m.OnNewTxAddr(tx, AddressDescriptor(si.addrDesc))
sent[si.addrDesc] = struct{}{}
}
}
}
return txEntry{addrIndexes: addrIndexes, time: txTime}, true
}
2018-04-03 07:51:38 -06:00
// Resync gets mempool transactions and maps outputs to transactions.
// Resync is not reentrant, it should be called from a single thread.
// Read operations (GetTransactions) are safe.
2019-04-01 09:00:34 -06:00
func (m *MempoolEthereumType) Resync() (int, error) {
2018-04-03 07:51:38 -06:00
start := time.Now()
glog.V(1).Info("Mempool: resync")
txs, err := m.chain.GetMempoolTransactions()
2018-04-03 07:51:38 -06:00
if err != nil {
2018-06-01 05:22:56 -06:00
return 0, err
2018-04-03 07:51:38 -06:00
}
2018-05-01 15:43:36 -06:00
// allocate slightly larger capacity of the maps
2019-04-01 09:00:34 -06:00
newTxEntries := make(map[string]txEntry, len(m.txEntries)+5)
newAddrDescToTx := make(map[string][]Outpoint, len(m.addrDescToTx)+5)
2019-04-01 09:00:34 -06:00
txTime := uint32(time.Now().Unix())
var ok bool
2018-04-03 07:51:38 -06:00
for _, txid := range txs {
2019-04-01 09:00:34 -06:00
entry, exists := m.txEntries[txid]
2018-04-03 07:51:38 -06:00
if !exists {
2019-04-01 09:00:34 -06:00
entry, ok = m.createTxEntry(txid, txTime)
if !ok {
2018-04-03 07:51:38 -06:00
continue
}
}
2019-04-01 09:00:34 -06:00
newTxEntries[txid] = entry
for _, si := range entry.addrIndexes {
newAddrDescToTx[si.addrDesc] = append(newAddrDescToTx[si.addrDesc], Outpoint{txid, si.n})
2018-04-03 07:51:38 -06:00
}
}
2019-04-01 09:00:34 -06:00
m.updateMappings(newTxEntries, newAddrDescToTx)
glog.Info("Mempool: resync finished in ", time.Since(start), ", ", len(m.txEntries), " transactions in mempool")
return len(m.txEntries), nil
2018-04-03 07:51:38 -06:00
}