From 4e43f0d4820ba341a4fcad36f5146091d0a41f5e Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Thu, 29 Mar 2018 17:30:12 +0200 Subject: [PATCH] Implement BlockChain.GetMempool in ethrpc --- bchain/coins/eth/ethrpc.go | 31 +++++++++++++++++++++++++------ bchain/coins/eth/ethrpc_test.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index 4c9fb8f6..67207589 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -37,7 +37,7 @@ type EthRPC struct { Parser *EthParser Testnet bool Network string - Mempool *bchain.UTXOMempool + Mempool *bchain.NonUTXOMempool bestHeaderMu sync.Mutex bestHeader *ethtypes.Header chanNewBlock chan *ethtypes.Header @@ -126,7 +126,9 @@ func (b *EthRPC) Initialize() error { return errors.Annotatef(err, "EthSubscribe newHeads") } b.newBlockSubscription = sub - // b.Mempool = bchain.NewMempool(s, metrics) + + b.Mempool = bchain.NewNonUTXOMempool(b) + return nil } @@ -415,8 +417,26 @@ func (b *EthRPC) GetTransaction(txid string) (*bchain.Tx, error) { return btx, nil } +type rpcMempoolBlock struct { + Transactions []string `json:"transactions"` +} + func (b *EthRPC) GetMempool() ([]string, error) { - panic("not implemented") + ctx, cancel := context.WithTimeout(context.Background(), b.timeout) + defer cancel() + var raw json.RawMessage + var err error + err = b.rpc.CallContext(ctx, &raw, "eth_getBlockByNumber", "pending", false) + if err != nil { + return nil, err + } else if len(raw) == 0 { + return nil, bchain.ErrBlockNotFound + } + var body rpcMempoolBlock + if err := json.Unmarshal(raw, &body); err != nil { + return nil, err + } + return body.Transactions, nil } // EstimateFee returns fee estimation. @@ -446,12 +466,11 @@ func (b *EthRPC) SendRawTransaction(tx string) (string, error) { } func (b *EthRPC) ResyncMempool(onNewTxAddr func(txid string, addr string)) error { - return nil - return errors.New("ResyncMempool: not implemented") + return b.Mempool.Resync(onNewTxAddr) } func (b *EthRPC) GetMempoolTransactions(address string) ([]string, error) { - return nil, errors.New("ResyncMempool: not implemented") + return b.Mempool.GetTransactions(address) } func (b *EthRPC) GetMempoolSpentOutput(outputTxid string, vout uint32) string { diff --git a/bchain/coins/eth/ethrpc_test.go b/bchain/coins/eth/ethrpc_test.go index 3251b716..204b2910 100644 --- a/bchain/coins/eth/ethrpc_test.go +++ b/bchain/coins/eth/ethrpc_test.go @@ -378,7 +378,6 @@ func TestEthRPC_GetTransaction(t *testing.T) { }, Vout: []bchain.Vout{ { - N: uint32(1), ScriptPubKey: bchain.ScriptPubKey{ Addresses: []string{"682b7903a11098cf770c7aef4aa02a85b3f3601a"}, }, @@ -407,7 +406,6 @@ func TestEthRPC_GetTransaction(t *testing.T) { }, Vout: []bchain.Vout{ { - N: uint32(10), ScriptPubKey: bchain.ScriptPubKey{ Addresses: []string{"555ee11fbddc0e49a9bab358a8941ad95ffdb48f"}, }, @@ -468,3 +466,32 @@ func TestEthRPC_EstimateFee(t *testing.T) { }) } } + +func TestEthRPC_GetMempool(t *testing.T) { + type fields struct { + b *EthRPC + } + tests := []struct { + name string + fields fields + want []string + wantErr bool + }{ + { + name: "1", + fields: fields{ + b: setupEthRPC(), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.fields.b.GetMempool() + if (err != nil) != tt.wantErr { + t.Errorf("EthRPC.GetMempool() error = %v, wantErr %v", err, tt.wantErr) + return + } + t.Logf("EthRPC.GetMempool() returned %v transactions", len(got)) + }) + } +}