-
Notifications
You must be signed in to change notification settings - Fork 84
/
eth.go
468 lines (422 loc) · 14.2 KB
/
eth.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
//go:build lgpl
// +build lgpl
package eth
import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"math/big"
"sync"
"time"
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/encode"
dexeth "decred.org/dcrdex/dex/networks/eth"
swapv0 "decred.org/dcrdex/dex/networks/eth/contracts/v0"
"decred.org/dcrdex/server/asset"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
func init() {
asset.Register(assetName, &Driver{})
}
const (
version = 0
assetName = "eth"
// The blockPollInterval is the delay between calls to bestBlockHash to
// check for new blocks.
blockPollInterval = time.Second
// TODO: Fill in with an addresses. Also consider upgrades where one
// contract will be good for current active swaps, but a new one is
// required for new swaps.
mainnetContractAddr = ""
testnetContractAddr = ""
simnetContractAddr = ""
)
var _ asset.Driver = (*Driver)(nil)
// Driver implements asset.Driver.
type Driver struct{}
// Version returns the Backend implementation's version number.
func (d *Driver) Version() uint32 {
return version
}
// Setup creates the ETH backend. Start the backend with its Run method.
func (d *Driver) Setup(configPath string, logger dex.Logger, network dex.Network) (asset.Backend, error) {
return NewBackend(configPath, logger, network)
}
// DecodeCoinID creates a human-readable representation of a coin ID for
// Ethereum. This must be a transaction hash.
func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
txHash, err := dexeth.DecodeCoinID(coinID)
if err != nil {
return "", err
}
return txHash.String(), nil
}
// UnitInfo returns the dex.UnitInfo for the asset.
func (d *Driver) UnitInfo() dex.UnitInfo {
return dexeth.UnitInfo
}
// ethFetcher represents a blockchain information fetcher. In practice, it is
// satisfied by rpcclient. For testing, it can be satisfied by a stub.
//
// TODO: At some point multiple contracts will need to be used, at least for
// transitory periods when updating the contract, and possibly a random
// contract setup, and so contract addresses may need to be an argument in some
// of these methods.
type ethFetcher interface {
bestHeader(ctx context.Context) (*types.Header, error)
blockNumber(ctx context.Context) (uint64, error)
headerByHeight(ctx context.Context, height uint64) (*types.Header, error)
connect(ctx context.Context, ipc string, contractAddr *common.Address) error
shutdown()
suggestGasPrice(ctx context.Context) (*big.Int, error)
syncProgress(ctx context.Context) (*ethereum.SyncProgress, error)
swap(ctx context.Context, secretHash [32]byte) (*swapv0.ETHSwapSwap, error)
transaction(ctx context.Context, hash common.Hash) (tx *types.Transaction, isMempool bool, err error)
accountBalance(ctx context.Context, addr common.Address) (*big.Int, error)
}
type hashN struct {
height uint64
hash common.Hash
}
// Backend is an asset backend for Ethereum. It has methods for fetching output
// information and subscribing to block updates. It maintains a cache of block
// data for quick lookups. Backend implements asset.Backend, so provides
// exported methods for DEX-related blockchain info.
type Backend struct {
// A connection-scoped Context is used to cancel active RPCs on
// connection shutdown.
rpcCtx context.Context
cancelRPCs context.CancelFunc
cfg *config
node ethFetcher
// bestHash caches the last know best block hash and height and is used
// to detect reorgs. Only accessed in Connect and poll which is
// syncronous so no locking is needed presently.
bestHash hashN
// The backend provides block notification channels through the BlockChannel
// method.
blockChansMtx sync.RWMutex
blockChans map[chan *asset.BlockUpdate]struct{}
// A logger will be provided by the DEX. All logging should use the provided
// logger.
log dex.Logger
// contractAddr is the address of the swap contract used for swaps.
//
// TODO: Allow supporting multiple addresses/contracts. Needed in the
// case of updating where two contracts may be valid for some time,
// possibly disallowing initialization for the deprecated one only.
contractAddr common.Address
// initTxSize is the gas used for an initiation transaction with one swap.
initTxSize uint32
}
// Check that Backend satisfies the Backend interface.
var _ asset.Backend = (*Backend)(nil)
// unconnectedETH returns a Backend without a node. The node should be set
// before use.
func unconnectedETH(logger dex.Logger, cfg *config) *Backend {
ctx, cancel := context.WithCancel(context.Background())
// TODO: At some point multiple contracts will need to be used, at
// least for transitory periods when updating the contract, and
// possibly a random contract setup, and so this section will need to
// change to support multiple contracts.
var contractAddr common.Address
switch cfg.network {
case dex.Simnet:
contractAddr = common.HexToAddress(simnetContractAddr)
case dex.Testnet:
contractAddr = common.HexToAddress(testnetContractAddr)
case dex.Mainnet:
contractAddr = common.HexToAddress(mainnetContractAddr)
}
return &Backend{
rpcCtx: ctx,
cancelRPCs: cancel,
cfg: cfg,
log: logger,
blockChans: make(map[chan *asset.BlockUpdate]struct{}),
contractAddr: contractAddr,
initTxSize: uint32(dexeth.InitGas(1, version)),
}
}
// NewBackend is the exported constructor by which the DEX will import the
// Backend.
func NewBackend(ipc string, logger dex.Logger, network dex.Network) (*Backend, error) {
cfg, err := load(ipc, network)
if err != nil {
return nil, err
}
return unconnectedETH(logger, cfg), nil
}
func (eth *Backend) shutdown() {
eth.node.shutdown()
}
// Connect connects to the node RPC server and initializes some variables.
func (eth *Backend) Connect(ctx context.Context) (*sync.WaitGroup, error) {
c := rpcclient{}
if err := c.connect(ctx, eth.cfg.ipc, ð.contractAddr); err != nil {
return nil, err
}
eth.node = &c
// Prime the best block hash and height.
hdr, err := c.bestHeader(ctx)
if err != nil {
return nil, fmt.Errorf("error getting best block header from geth: %w", err)
}
eth.bestHash = hashN{
height: hdr.Number.Uint64(),
hash: hdr.Hash(),
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
eth.run(ctx)
wg.Done()
}()
return &wg, nil
}
// TxData fetches the raw transaction data.
func (eth *Backend) TxData(coinID []byte) ([]byte, error) {
txHash, err := dexeth.DecodeCoinID(coinID)
if err != nil {
return nil, fmt.Errorf("coin ID decoding error: %v", err)
}
tx, _, err := eth.node.transaction(eth.rpcCtx, txHash)
if err != nil {
return nil, fmt.Errorf("error retrieving transaction: %w", err)
}
if tx == nil { // Possible?
return nil, fmt.Errorf("no transaction %s", txHash)
}
return tx.MarshalBinary()
}
// InitTxSize is an upper limit on the gas used for an initiation.
func (eth *Backend) InitTxSize() uint32 {
return eth.initTxSize
}
// InitTxSizeBase is the same as InitTxSize for ETH.
func (eth *Backend) InitTxSizeBase() uint32 {
return eth.initTxSize
}
// FeeRate returns the current optimal fee rate in gwei / gas.
func (eth *Backend) FeeRate(ctx context.Context) (uint64, error) {
bigGP, err := eth.node.suggestGasPrice(ctx)
if err != nil {
return 0, err
}
return dexeth.ToGwei(bigGP)
}
// BlockChannel creates and returns a new channel on which to receive block
// updates. If the returned channel is ever blocking, there will be no error
// logged from the eth package. Part of the asset.Backend interface.
func (eth *Backend) BlockChannel(size int) <-chan *asset.BlockUpdate {
c := make(chan *asset.BlockUpdate, size)
eth.blockChansMtx.Lock()
defer eth.blockChansMtx.Unlock()
eth.blockChans[c] = struct{}{}
return c
}
// ValidateContract ensures that contractData encodes both the expected contract
// version targeted and the secret hash.
func (eth *Backend) ValidateContract(contractData []byte) error {
ver, _, err := dexeth.DecodeContractData(contractData)
if err != nil { // ensures secretHash is proper length
return err
}
if ver != version {
return fmt.Errorf("incorrect contract version %d, wanted %d", ver, version)
}
return nil
}
// Contract is part of the asset.Backend interface. The contractData bytes
// encodes both the contract version targeted and the secret hash.
func (eth *Backend) Contract(coinID, contractData []byte) (*asset.Contract, error) {
// newSwapCoin validates the contractData, extracting version, secret hash,
// counterparty address, and locktime. The supported version is enforced.
sc, err := eth.newSwapCoin(coinID, contractData, sctInit)
if err != nil {
return nil, fmt.Errorf("unable to create coiner: %w", err)
}
// Confirmations performs some extra swap status checks if the the tx is
// mined. For init coins, this uses the contract account's state (if it is
// mined) to verify the value, counterparty, and lock time.
_, err = sc.Confirmations(eth.rpcCtx)
if err != nil {
return nil, fmt.Errorf("unable to get confirmations: %v", err)
}
return &asset.Contract{
Coin: sc,
SwapAddress: sc.counterParty.String(),
ContractData: contractData,
LockTime: encode.UnixTimeMilli(sc.locktime),
}, nil
}
// ValidateSecret checks that the secret satisfies the secret hash.
func (eth *Backend) ValidateSecret(secret, secretHash []byte) bool {
sh := sha256.Sum256(secret)
return bytes.Equal(sh[:], secretHash)
}
// Synced is true if the blockchain is ready for action.
func (eth *Backend) Synced() (bool, error) {
// node.SyncProgress will return nil both before syncing has begun and
// after it has finished. In order to discern when syncing has begun,
// check that the best header came in under MaxBlockInterval.
sp, err := eth.node.syncProgress(eth.rpcCtx)
if err != nil {
return false, err
}
if sp != nil {
return false, nil
}
bh, err := eth.node.bestHeader(eth.rpcCtx)
if err != nil {
return false, err
}
// Time in the header is in seconds.
nowInSecs := time.Now().Unix() / 1000
timeDiff := nowInSecs - int64(bh.Time)
return timeDiff < dexeth.MaxBlockInterval, nil
}
// Redemption returns a coin that represents a contract redemption. redeemCoinID
// should be the transaction that sent a redemption, while contractCoinID is the
// swap contract this redemption redeems.
func (eth *Backend) Redemption(redeemCoinID, _, contractData []byte) (asset.Coin, error) {
// newSwapCoin uses the contract account's state to validate the
// contractData, extracting version, secret, and secret hash. The supported
// version is enforced.
rc, err := eth.newSwapCoin(redeemCoinID, contractData, sctRedeem)
if err != nil {
return nil, fmt.Errorf("unable to create coiner: %w", err)
}
// Confirmations performs some extra swap status checks if the the tx
// is mined. For redeem coins, this is just a swap state check.
_, err = rc.Confirmations(eth.rpcCtx)
if err != nil {
return nil, fmt.Errorf("unable to get confirmations: %v", err)
}
return rc, nil
}
// ValidateCoinID attempts to decode the coinID.
func (eth *Backend) ValidateCoinID(coinID []byte) (string, error) {
txHash, err := dexeth.DecodeCoinID(coinID)
if err != nil {
return "", err
}
return txHash.String(), nil
}
// CheckAddress checks that the given address is parseable.
func (eth *Backend) CheckAddress(addr string) bool {
return common.IsHexAddress(addr)
}
// AccountBalance retrieves the current account balance, including the effects
// of known unmined transactions.
func (eth *Backend) AccountBalance(addrStr string) (uint64, error) {
bigBal, err := eth.node.accountBalance(eth.rpcCtx, common.HexToAddress(addrStr))
if err != nil {
return 0, fmt.Errorf("accountBalance error: %w", err)
}
return dexeth.ToGwei(bigBal)
}
// poll pulls the best hash from an eth node and compares that to a stored
// hash. If the same does nothing. If different, updates the stored hash and
// notifies listeners on block chans.
func (eth *Backend) poll(ctx context.Context) {
best := ð.bestHash
send := func(reorg bool, err error) {
if err != nil {
eth.log.Error(err)
}
eth.blockChansMtx.RLock()
for c := range eth.blockChans {
select {
case c <- &asset.BlockUpdate{
Reorg: reorg,
Err: err,
}:
default:
eth.log.Error("failed to send block update on blocking channel")
}
}
eth.blockChansMtx.RUnlock()
}
bhdr, err := eth.node.bestHeader(ctx)
if err != nil {
send(false, fmt.Errorf("error getting best block header from geth: %w", err))
return
}
if bhdr.Hash() == best.hash {
// Same hash, nothing to do.
return
}
update := func(reorg bool, fastBlocks bool) {
hash := bhdr.Hash()
height := bhdr.Number.Uint64()
str := fmt.Sprintf("Tip change from %s (%d) to %s (%d).",
best.hash, best.height, hash, height)
switch {
case reorg:
str += " Detected reorg."
case fastBlocks:
str += " Fast blocks."
}
eth.log.Debug(str)
best.hash = hash
best.height = height
}
if bhdr.ParentHash == best.hash {
// Sequential hash, report a block update.
update(false, false)
send(false, nil)
return
}
// Either a block was skipped or a reorg happened. We can only detect
// the reorg if our last best header's hash has changed. Otherwise,
// assume no reorg and report the new block change.
//
// headerByHeight will only return mainchain headers.
hdr, err := eth.node.headerByHeight(ctx, best.height)
if err != nil {
send(false, fmt.Errorf("error getting block header from geth: %w", err))
return
}
if hdr.Hash() == best.hash {
// Our recorded hash is still on main chain so there is no reorg
// that we know of. The chain has advanced more than one block.
update(false, true)
send(false, nil)
return
}
// The block for our recorded hash was forked off and the chain had a
// reorganization.
update(true, false)
send(true, nil)
}
// run processes the queue and monitors the application context.
func (eth *Backend) run(ctx context.Context) {
done := ctx.Done()
wait := make(chan struct{})
// Shut down the RPC client on ctx.Done().
go func() {
<-done
eth.cancelRPCs()
eth.shutdown()
close(wait)
}()
blockPoll := time.NewTicker(blockPollInterval)
defer blockPoll.Stop()
out:
for {
select {
case <-blockPoll.C:
eth.poll(ctx)
case <-done:
break out
}
}
// Wait for the RPC client to shut down.
<-wait
}