forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethereum.go
276 lines (236 loc) · 6.78 KB
/
ethereum.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package ethereum
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/encoding/web3hex"
"github.com/hyperledger/burrow/event"
"github.com/hyperledger/burrow/event/query"
"github.com/hyperledger/burrow/execution/errors"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/rpc/rpcevents"
"github.com/hyperledger/burrow/rpc/web3/ethclient"
"github.com/hyperledger/burrow/vent/chain"
"github.com/hyperledger/burrow/vent/types"
"google.golang.org/grpc/connectivity"
)
const Scope = "Ethereum"
type Chain struct {
client ThrottleClient
filter *chain.Filter
chainID string
version string
consumerConfig *chain.BlockConsumerConfig
logger *logging.Logger
}
var _ chain.Chain = (*Chain)(nil)
type EthClient interface {
GetLogs(filter *ethclient.Filter) ([]*ethclient.EthLog, error)
BlockNumber() (uint64, error)
GetBlockByNumber(height string) (*ethclient.Block, error)
NetVersion() (string, error)
Web3ClientVersion() (string, error)
Syncing() (bool, error)
}
// We rely on this failing if the chain is not an Ethereum Chain
func New(client EthClient, filter *chain.Filter, consumerConfig *chain.BlockConsumerConfig,
logger *logging.Logger) (*Chain, error) {
logger = logger.WithScope(Scope)
throttleClient := NewThrottleClient(client, consumerConfig.MaxRequests, consumerConfig.TimeBase, logger)
chainID, err := throttleClient.NetVersion()
if err != nil {
return nil, fmt.Errorf("could not get Ethereum ChainID: %w", err)
}
version, err := throttleClient.Web3ClientVersion()
if err != nil {
return nil, fmt.Errorf("could not get Ethereum node version: %w", err)
}
return &Chain{
client: throttleClient,
filter: filter,
chainID: chainID,
version: version,
consumerConfig: consumerConfig,
logger: logger,
}, nil
}
func (c *Chain) StatusMessage(ctx context.Context, lastProcessedHeight uint64) []interface{} {
// TODO: more info is available from web3
return []interface{}{
"msg", "status",
"chain_type", "Ethereum",
"last_processed_height", lastProcessedHeight,
}
}
func (c *Chain) GetABI(ctx context.Context, address crypto.Address) (string, error) {
// Unsupported by Ethereum
return "", nil
}
func (c *Chain) GetVersion() string {
return c.version
}
func (c *Chain) GetChainID() string {
return c.chainID
}
func (c *Chain) ConsumeBlocks(ctx context.Context, in *rpcevents.BlockRange, consumer func(chain.Block) error) error {
return Consume(c.client, c.filter, in, c.consumerConfig, c.logger, consumer)
}
func (c *Chain) Connectivity() connectivity.State {
// TODO: better connectivity information
_, err := c.client.Syncing()
if err != nil {
return connectivity.TransientFailure
}
return connectivity.Ready
}
func (c *Chain) Close() error {
// just a http.Client - nothing to free
return nil
}
type Block struct {
client EthClient
Height uint64
Transactions []chain.Transaction
}
func newBlock(client EthClient, log *Event) *Block {
return &Block{
client: client,
Height: log.Height,
Transactions: []chain.Transaction{NewEthereumTransaction(log)},
}
}
var _ chain.Block = (*Block)(nil)
func (b *Block) GetHeight() uint64 {
return b.Height
}
func (b *Block) GetTxs() []chain.Transaction {
return b.Transactions
}
func (b *Block) GetMetadata(columns types.SQLColumnNames) (map[string]interface{}, error) {
block, err := b.client.GetBlockByNumber(web3hex.Encoder.Uint64(b.Height))
if err != nil {
return nil, err
}
d := new(web3hex.Decoder)
blockHeader, err := json.Marshal(block)
if err != nil {
return nil, fmt.Errorf("could not serialise block header: %w", err)
}
return map[string]interface{}{
columns.Height: strconv.FormatUint(b.Height, 10),
columns.TimeStamp: time.Unix(d.Int64(block.Timestamp), 0),
columns.BlockHeader: string(blockHeader),
}, d.Err()
}
func (b *Block) appendTransaction(log *Event) {
b.Transactions = append(b.Transactions, &Transaction{
Index: uint64(len(b.Transactions)),
Hash: log.TransactionHash,
Events: []chain.Event{log},
})
}
func (b *Block) appendEvent(log *Event) {
tx := b.Transactions[len(b.Transactions)-1].(*Transaction)
log.Index = uint64(len(tx.Events))
tx.Events = append(tx.Events, log)
}
type Transaction struct {
Height uint64
Index uint64
Hash binary.HexBytes
Events []chain.Event
}
func NewEthereumTransaction(log *Event) *Transaction {
return &Transaction{
Height: log.Height,
Index: 0,
Hash: log.TransactionHash,
Events: []chain.Event{log},
}
}
func (tx *Transaction) GetHash() binary.HexBytes {
return tx.Hash
}
func (tx *Transaction) GetIndex() uint64 {
return tx.Index
}
func (tx *Transaction) GetEvents() []chain.Event {
return tx.Events
}
func (tx *Transaction) GetException() *errors.Exception {
// Ethereum does not retain an log from reverted transactions
return nil
}
func (tx *Transaction) GetOrigin() *chain.Origin {
// Origin refers to a previous dumped chain which is not a concept in Ethereum
return nil
}
func (tx *Transaction) GetMetadata(columns types.SQLColumnNames) (map[string]interface{}, error) {
return map[string]interface{}{
columns.Height: tx.Height,
columns.TxHash: tx.Hash.String(),
columns.TxIndex: tx.Index,
columns.TxType: exec.TypeLog.String(),
}, nil
}
var _ chain.Transaction = (*Transaction)(nil)
type Event struct {
exec.LogEvent
Height uint64
// Index of event in entire block (what ethereum provides us with
IndexInBlock uint64
// Index of event in transaction
Index uint64
TransactionHash binary.HexBytes
}
var _ chain.Event = (*Event)(nil)
func newEvent(log *ethclient.EthLog) (*Event, error) {
d := new(web3hex.Decoder)
topics := make([]binary.Word256, len(log.Topics))
for i, t := range log.Topics {
topics[i] = binary.LeftPadWord256(d.Bytes(t))
}
txHash := d.Bytes(log.TransactionHash)
return &Event{
LogEvent: exec.LogEvent{
Topics: topics,
Address: d.Address(log.Address),
Data: d.Bytes(log.Data),
},
Height: d.Uint64(log.BlockNumber),
IndexInBlock: d.Uint64(log.LogIndex),
TransactionHash: txHash,
}, d.Err()
}
func (ev *Event) GetIndex() uint64 {
return ev.Index
}
func (ev *Event) GetTransactionHash() binary.HexBytes {
return ev.TransactionHash
}
func (ev *Event) GetAddress() crypto.Address {
return ev.Address
}
func (ev *Event) GetTopics() []binary.Word256 {
return ev.Topics
}
func (ev *Event) GetData() []byte {
return ev.Data
}
func (ev *Event) Get(key string) (value interface{}, ok bool) {
switch key {
case event.EventTypeKey:
return exec.TypeLog, true
}
v, ok := ev.LogEvent.Get(key)
if ok {
return v, ok
}
return query.GetReflect(reflect.ValueOf(ev), key)
}