-
Notifications
You must be signed in to change notification settings - Fork 3
/
tx_iter_rpc.go
74 lines (66 loc) · 1.52 KB
/
tx_iter_rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"fmt"
"log"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
type TxIterRPC struct {
client Client
parentBlockHash *common.Hash
block *types.Block
nextTx int
}
func NewTxIterRPC(rpcEndpoint string, startBlock *big.Int) *TxIterRPC {
fmt.Printf("Starting block: %v, RPC endpoint: %v\n", startBlock, rpcEndpoint)
it := &TxIterRPC{}
var err error
if strings.HasPrefix(rpcEndpoint, "http://") || strings.HasPrefix(rpcEndpoint, "https://") || strings.HasSuffix(rpcEndpoint, ".ipc") {
it.client, err = ethclient.Dial(rpcEndpoint)
} else {
it.client, err = NewLocalClient(rpcEndpoint)
}
if err != nil {
log.Fatalln(err)
}
it.block, err = it.client.BlockByNumber(context.Background(), startBlock)
if err != nil {
log.Fatalln(err)
}
return it
}
func (it *TxIterRPC) Next() []byte {
if it.block == nil {
return nil
}
txs := it.block.Transactions()
var err error
if it.nextTx == len(txs) {
it.block, err = it.client.BlockByHash(context.Background(), it.block.ParentHash())
if err != nil {
log.Fatalln(err)
}
it.nextTx = 0
return it.Next()
}
tx := txs[it.nextTx]
it.nextTx++
if tx.Type() == types.DepositTxType {
return it.Next()
}
b, err := tx.MarshalBinary()
if err != nil {
log.Fatalln(err)
}
return b
}
func (it *TxIterRPC) Close() {
it.client.Close()
}
func (it *TxIterRPC) BlockNumber() *big.Int {
return it.block.Number()
}