Implement BlockChain.GetMempool in ethrpc

This commit is contained in:
Martin Boehm 2018-03-29 17:30:12 +02:00
parent 090bb8e4f1
commit 4e43f0d482
2 changed files with 54 additions and 8 deletions

View File

@ -37,7 +37,7 @@ type EthRPC struct {
Parser *EthParser Parser *EthParser
Testnet bool Testnet bool
Network string Network string
Mempool *bchain.UTXOMempool Mempool *bchain.NonUTXOMempool
bestHeaderMu sync.Mutex bestHeaderMu sync.Mutex
bestHeader *ethtypes.Header bestHeader *ethtypes.Header
chanNewBlock chan *ethtypes.Header chanNewBlock chan *ethtypes.Header
@ -126,7 +126,9 @@ func (b *EthRPC) Initialize() error {
return errors.Annotatef(err, "EthSubscribe newHeads") return errors.Annotatef(err, "EthSubscribe newHeads")
} }
b.newBlockSubscription = sub b.newBlockSubscription = sub
// b.Mempool = bchain.NewMempool(s, metrics)
b.Mempool = bchain.NewNonUTXOMempool(b)
return nil return nil
} }
@ -415,8 +417,26 @@ func (b *EthRPC) GetTransaction(txid string) (*bchain.Tx, error) {
return btx, nil return btx, nil
} }
type rpcMempoolBlock struct {
Transactions []string `json:"transactions"`
}
func (b *EthRPC) GetMempool() ([]string, error) { 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. // 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 { func (b *EthRPC) ResyncMempool(onNewTxAddr func(txid string, addr string)) error {
return nil return b.Mempool.Resync(onNewTxAddr)
return errors.New("ResyncMempool: not implemented")
} }
func (b *EthRPC) GetMempoolTransactions(address string) ([]string, error) { 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 { func (b *EthRPC) GetMempoolSpentOutput(outputTxid string, vout uint32) string {

View File

@ -378,7 +378,6 @@ func TestEthRPC_GetTransaction(t *testing.T) {
}, },
Vout: []bchain.Vout{ Vout: []bchain.Vout{
{ {
N: uint32(1),
ScriptPubKey: bchain.ScriptPubKey{ ScriptPubKey: bchain.ScriptPubKey{
Addresses: []string{"682b7903a11098cf770c7aef4aa02a85b3f3601a"}, Addresses: []string{"682b7903a11098cf770c7aef4aa02a85b3f3601a"},
}, },
@ -407,7 +406,6 @@ func TestEthRPC_GetTransaction(t *testing.T) {
}, },
Vout: []bchain.Vout{ Vout: []bchain.Vout{
{ {
N: uint32(10),
ScriptPubKey: bchain.ScriptPubKey{ ScriptPubKey: bchain.ScriptPubKey{
Addresses: []string{"555ee11fbddc0e49a9bab358a8941ad95ffdb48f"}, 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))
})
}
}