-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathnode.go
566 lines (487 loc) · 15.7 KB
/
node.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package ethereummock
import (
"bytes"
"context"
"errors"
"fmt"
"math/big"
"sync"
"sync/atomic"
"time"
"github.com/ten-protocol/go-ten/go/enclave/storage"
"github.com/ten-protocol/go-ten/go/common/async"
"github.com/google/uuid"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/go/common/gethutil"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
ethclient_ethereum "github.com/ethereum/go-ethereum/ethclient"
"github.com/ten-protocol/go-ten/go/ethadapter"
"github.com/ten-protocol/go-ten/go/ethadapter/erc20contractlib"
"github.com/ten-protocol/go-ten/go/ethadapter/mgmtcontractlib"
)
type L1Network interface {
// BroadcastBlock - send the block and the parent to make sure there are no gaps
BroadcastBlock(b common.EncodedL1Block, p common.EncodedL1Block)
BroadcastTx(tx *types.Transaction)
}
type MiningConfig struct {
PowTime common.Latency
LogFile string
}
type TxDB interface {
Txs(block *types.Block) (map[common.TxHash]*types.Transaction, bool)
AddTxs(*types.Block, map[common.TxHash]*types.Transaction)
}
type StatsCollector interface {
// L1Reorg registers when a miner has to process a reorg (a winning block from a fork)
L1Reorg(id gethcommon.Address)
}
type Node struct {
l2ID gethcommon.Address // the address of the Obscuro node this client is dedicated to
cfg MiningConfig
Network L1Network
mining bool
stats StatsCollector
Resolver storage.BlockResolver
db TxDB
subs map[uuid.UUID]*mockSubscription // active subscription for mock blocks
subMu sync.Mutex
// Channels
exitCh chan bool // the Node stops
exitMiningCh chan bool // the mining loop is notified to stop
interrupt *int32
p2pCh chan *types.Block // this is where blocks received from peers are dropped
miningCh chan *types.Block // this is where blocks created by the mining setup of the current node are dropped
canonicalCh chan *types.Block // this is where the main processing routine drops blocks that are canonical
mempoolCh chan *types.Transaction // where l1 transactions to be published in the next block are added
// internal
headInCh chan bool
headOutCh chan *types.Block
erc20ContractLib erc20contractlib.ERC20ContractLib
mgmtContractLib mgmtcontractlib.MgmtContractLib
logger gethlog.Logger
}
func (m *Node) PrepareTransactionToSend(_ context.Context, txData types.TxData, _ gethcommon.Address) (types.TxData, error) {
tx := types.NewTx(txData)
return &types.LegacyTx{
Nonce: 123,
GasPrice: tx.GasPrice(),
Gas: tx.Gas(),
To: tx.To(),
Value: tx.Value(),
Data: tx.Data(),
}, nil
}
func (m *Node) PrepareTransactionToRetry(ctx context.Context, txData types.TxData, from gethcommon.Address, _ uint64, _ int) (types.TxData, error) {
return m.PrepareTransactionToSend(ctx, txData, from)
}
func (m *Node) SendTransaction(tx *types.Transaction) error {
m.Network.BroadcastTx(tx)
return nil
}
func (m *Node) TransactionReceipt(_ gethcommon.Hash) (*types.Receipt, error) {
// all transactions are immediately processed
return &types.Receipt{
Status: types.ReceiptStatusSuccessful,
}, nil
}
func (m *Node) Nonce(gethcommon.Address) (uint64, error) {
return 0, nil
}
func (m *Node) getRollupFromBlock(block *types.Block) *common.ExtRollup {
for _, tx := range block.Transactions() {
decodedTx := m.mgmtContractLib.DecodeTx(tx)
if decodedTx == nil {
continue
}
switch l1tx := decodedTx.(type) {
case *ethadapter.L1RollupTx:
r, err := common.DecodeRollup(l1tx.Rollup)
if err == nil {
return r
}
}
}
return nil
}
func (m *Node) FetchLastBatchSeqNo(gethcommon.Address) (*big.Int, error) {
startingBlock, err := m.FetchHeadBlock()
if err != nil {
return nil, err
}
for currentBlock := startingBlock; currentBlock.NumberU64() != 0; currentBlock, _ = m.BlockByHash(currentBlock.Header().ParentHash) {
rollup := m.getRollupFromBlock(currentBlock)
if rollup != nil {
return big.NewInt(int64(rollup.Header.LastBatchSeqNo)), nil
}
}
// the first batch is number 1
return big.NewInt(int64(common.L2GenesisSeqNo)), nil
}
// BlockListener provides stream of latest mock head headers as they are created
func (m *Node) BlockListener() (chan *types.Header, ethereum.Subscription) {
id := uuid.New()
mockSub := &mockSubscription{
node: m,
id: id,
headCh: make(chan *types.Header),
}
m.subMu.Lock()
defer m.subMu.Unlock()
m.subs[id] = mockSub
return mockSub.headCh, mockSub
}
func (m *Node) BlockNumber() (uint64, error) {
blk, err := m.Resolver.FetchHeadBlock(context.Background())
if err != nil {
if errors.Is(err, errutil.ErrNotFound) {
return 0, ethereum.NotFound
}
return 0, fmt.Errorf("could not retrieve head block. Cause: %w", err)
}
return blk.NumberU64(), nil
}
func (m *Node) BlockByNumber(n *big.Int) (*types.Block, error) {
if n.Int64() == 0 {
return MockGenesisBlock, nil
}
// TODO this should be a method in the resolver
blk, err := m.Resolver.FetchHeadBlock(context.Background())
if err != nil {
if errors.Is(err, errutil.ErrNotFound) {
return nil, ethereum.NotFound
}
return nil, fmt.Errorf("could not retrieve head block. Cause: %w", err)
}
for !bytes.Equal(blk.ParentHash().Bytes(), (common.L1BlockHash{}).Bytes()) {
if blk.NumberU64() == n.Uint64() {
return blk, nil
}
blk, err = m.Resolver.FetchBlock(context.Background(), blk.ParentHash())
if err != nil {
return nil, fmt.Errorf("could not retrieve parent for block in chain. Cause: %w", err)
}
}
return nil, ethereum.NotFound
}
func (m *Node) BlockByHash(id gethcommon.Hash) (*types.Block, error) {
blk, err := m.Resolver.FetchBlock(context.Background(), id)
if err != nil {
return nil, fmt.Errorf("block could not be retrieved. Cause: %w", err)
}
return blk, nil
}
func (m *Node) FetchHeadBlock() (*types.Block, error) {
block, err := m.Resolver.FetchHeadBlock(context.Background())
if err != nil {
return nil, fmt.Errorf("could not retrieve head block. Cause: %w", err)
}
return block, nil
}
func (m *Node) Info() ethadapter.Info {
return ethadapter.Info{
L2ID: m.l2ID,
}
}
func (m *Node) IsBlockAncestor(block *types.Block, proof common.L1BlockHash) bool {
return m.Resolver.IsBlockAncestor(context.Background(), block, proof)
}
func (m *Node) BalanceAt(gethcommon.Address, *big.Int) (*big.Int, error) {
panic("not implemented")
}
// GetLogs is a mock method - we don't really have logs on the mock transactions, so it returns a basic log for every tx
// so the host recognises them as relevant
func (m *Node) GetLogs(fq ethereum.FilterQuery) ([]types.Log, error) {
logs := make([]types.Log, 0)
if fq.BlockHash == nil {
return logs, nil
}
blk, err := m.BlockByHash(*fq.BlockHash)
if err != nil {
return nil, fmt.Errorf("could not retrieve block. Cause: %w", err)
}
for _, tx := range blk.Transactions() {
dummyLog := types.Log{
BlockHash: blk.Hash(),
TxHash: tx.Hash(),
}
logs = append(logs, dummyLog)
}
return logs, nil
}
// Start runs an infinite loop that listens to the two block producing channels and processes them.
func (m *Node) Start() {
if m.mining {
// This starts the mining
go m.startMining()
}
err := m.Resolver.StoreBlock(context.Background(), MockGenesisBlock, nil)
if err != nil {
m.logger.Crit("Failed to store block")
}
head := m.setHead(MockGenesisBlock)
for {
select {
case p2pb := <-m.p2pCh: // Received from peers
_, err := m.Resolver.FetchBlock(context.Background(), p2pb.Hash())
// only process blocks if they haven't been processed before
if err != nil {
if errors.Is(err, errutil.ErrNotFound) {
head = m.processBlock(p2pb, head)
} else {
panic(fmt.Errorf("could not retrieve parent block. Cause: %w", err))
}
}
case mb := <-m.miningCh: // Received from the local mining
head = m.processBlock(mb, head)
if bytes.Equal(head.Hash().Bytes(), mb.Hash().Bytes()) { // Ignore the locally produced block if someone else found one already
p, err := m.Resolver.FetchBlock(context.Background(), mb.ParentHash())
if err != nil {
panic(fmt.Errorf("could not retrieve parent. Cause: %w", err))
}
encodedBlock, err := common.EncodeBlock(mb)
if err != nil {
panic(fmt.Errorf("could not encode block. Cause: %w", err))
}
encodedParentBlock, err := common.EncodeBlock(p)
if err != nil {
panic(fmt.Errorf("could not encode parent block. Cause: %w", err))
}
m.Network.BroadcastBlock(encodedBlock, encodedParentBlock)
}
case <-m.headInCh:
m.headOutCh <- head
case <-m.exitCh:
return
}
}
}
func (m *Node) processBlock(b *types.Block, head *types.Block) *types.Block {
err := m.Resolver.StoreBlock(context.Background(), b, nil)
if err != nil {
m.logger.Crit("Failed to store block. Cause: %w", err)
}
_, err = m.Resolver.FetchBlock(context.Background(), b.Header().ParentHash)
// only proceed if the parent is available
if err != nil {
if errors.Is(err, errutil.ErrNotFound) {
m.logger.Info(fmt.Sprintf("Parent block not found=b_%d", common.ShortHash(b.Header().ParentHash)))
return head
}
m.logger.Crit("Could not fetch block parent. Cause: %w", err)
}
// Ignore superseded blocks
if b.NumberU64() <= head.NumberU64() {
return head
}
// Check for Reorgs
if !m.Resolver.IsAncestor(context.Background(), b, head) {
m.stats.L1Reorg(m.l2ID)
fork, err := gethutil.LCA(context.Background(), head, b, m.Resolver)
if err != nil {
panic(err)
}
m.logger.Info(
fmt.Sprintf("L1Reorg new=b_%d(%d), old=b_%d(%d), fork=b_%d(%d)", common.ShortHash(b.Hash()), b.NumberU64(), common.ShortHash(head.Hash()), head.NumberU64(), common.ShortHash(fork.CommonAncestor.Hash()), fork.CommonAncestor.NumberU64()))
return m.setFork(m.BlocksBetween(fork.CommonAncestor, b))
}
if b.NumberU64() > (head.NumberU64() + 1) {
m.logger.Crit("Should not happen")
}
return m.setHead(b)
}
// Notifies the Miner to start mining on the new block and the aggregator to produce rollups
func (m *Node) setHead(b *types.Block) *types.Block {
if atomic.LoadInt32(m.interrupt) == 1 {
return b
}
// notify the client subscriptions
m.subMu.Lock()
for _, s := range m.subs {
sub := s
go sub.publish(b)
}
m.subMu.Unlock()
m.canonicalCh <- b
return b
}
func (m *Node) setFork(blocks []*types.Block) *types.Block {
head := blocks[len(blocks)-1]
if atomic.LoadInt32(m.interrupt) == 1 {
return head
}
// notify the client subs
m.subMu.Lock()
for _, s := range m.subs {
sub := s
go sub.publishAll(blocks)
}
m.subMu.Unlock()
m.canonicalCh <- head
return head
}
// P2PReceiveBlock is called by counterparties when there is a block to broadcast
// All it does is drop the blocks in a channel for processing.
func (m *Node) P2PReceiveBlock(b common.EncodedL1Block, p common.EncodedL1Block) {
if atomic.LoadInt32(m.interrupt) == 1 {
return
}
decodedBlock, err := b.DecodeBlock()
if err != nil {
panic(fmt.Errorf("could not decode block. Cause: %w", err))
}
decodedParentBlock, err := p.DecodeBlock()
if err != nil {
panic(fmt.Errorf("could not decode parent block. Cause: %w", err))
}
m.p2pCh <- decodedParentBlock
m.p2pCh <- decodedBlock
}
// startMining - listens on the canonicalCh and schedule a go routine that produces a block after a PowTime and drop it
// on the miningCh channel
func (m *Node) startMining() {
m.logger.Info(" starting miner...")
// stores all transactions seen from the beginning of time.
mempool := make([]*types.Transaction, 0)
z := int32(0)
interrupt := &z
for {
select {
case <-m.exitMiningCh:
return
case tx := <-m.mempoolCh:
mempool = append(mempool, tx)
case canonicalBlock := <-m.canonicalCh:
// A new canonical block was found. Start a new round based on that block.
// remove transactions that are already considered committed
mempool = m.removeCommittedTransactions(context.Background(), canonicalBlock, mempool, m.Resolver, m.db)
// notify the existing mining go routine to stop mining
atomic.StoreInt32(interrupt, 1)
c := int32(0)
interrupt = &c
// Generate a random number, and wait for that number of ms. Equivalent to PoW
// Include all rollups received during this period.
async.Schedule(m.cfg.PowTime(), func() {
toInclude := findNotIncludedTxs(canonicalBlock, mempool, m.Resolver, m.db)
// todo - iterate through the rollup transactions and include only the ones with the proof on the canonical chain
if atomic.LoadInt32(m.interrupt) == 1 {
return
}
m.miningCh <- NewBlock(canonicalBlock, m.l2ID, toInclude)
})
}
}
}
// P2PGossipTx receive rollups to publish from the linked aggregators
func (m *Node) P2PGossipTx(tx *types.Transaction) {
if atomic.LoadInt32(m.interrupt) == 1 {
return
}
m.mempoolCh <- tx
}
func (m *Node) BroadcastTx(tx types.TxData) {
m.Network.BroadcastTx(types.NewTx(tx))
}
func (m *Node) Stop() {
// block all requests
atomic.StoreInt32(m.interrupt, 1)
time.Sleep(time.Millisecond * 100)
m.exitMiningCh <- true
m.exitCh <- true
}
func (m *Node) BlocksBetween(blockA *types.Block, blockB *types.Block) []*types.Block {
if bytes.Equal(blockA.Hash().Bytes(), blockB.Hash().Bytes()) {
return []*types.Block{blockA}
}
blocks := make([]*types.Block, 0)
tempBlock := blockB
var err error
for {
blocks = append(blocks, tempBlock)
if bytes.Equal(tempBlock.Hash().Bytes(), blockA.Hash().Bytes()) {
break
}
tempBlock, err = m.Resolver.FetchBlock(context.Background(), tempBlock.ParentHash())
if err != nil {
panic(fmt.Errorf("could not retrieve parent block. Cause: %w", err))
}
}
n := len(blocks)
result := make([]*types.Block, n)
for i, block := range blocks {
result[n-i-1] = block
}
return result
}
func (m *Node) CallContract(ethereum.CallMsg) ([]byte, error) {
return nil, nil
}
func (m *Node) EthClient() *ethclient_ethereum.Client {
return nil
}
func (m *Node) RemoveSubscription(id uuid.UUID) {
m.subMu.Lock()
defer m.subMu.Unlock()
delete(m.subs, id)
}
func (m *Node) ReconnectIfClosed() error {
return nil
}
func (m *Node) Alive() bool {
return true
}
func NewMiner(
id gethcommon.Address,
cfg MiningConfig,
network L1Network,
statsCollector StatsCollector,
) *Node {
return &Node{
l2ID: id,
mining: true,
cfg: cfg,
stats: statsCollector,
Resolver: NewResolver(),
db: NewTxDB(),
Network: network,
exitCh: make(chan bool),
exitMiningCh: make(chan bool),
interrupt: new(int32),
p2pCh: make(chan *types.Block),
miningCh: make(chan *types.Block),
canonicalCh: make(chan *types.Block),
mempoolCh: make(chan *types.Transaction),
headInCh: make(chan bool),
headOutCh: make(chan *types.Block),
erc20ContractLib: NewERC20ContractLibMock(),
mgmtContractLib: NewMgmtContractLibMock(),
logger: log.New(log.EthereumL1Cmp, int(gethlog.LvlInfo), cfg.LogFile, log.NodeIDKey, id),
subs: map[uuid.UUID]*mockSubscription{},
subMu: sync.Mutex{},
}
}
// implements the ethereum.Subscription
type mockSubscription struct {
id uuid.UUID
headCh chan *types.Header
node *Node // we hold a reference to the node to unsubscribe ourselves - not ideal but this is just a mock
}
func (sub *mockSubscription) Err() <-chan error {
return make(chan error)
}
func (sub *mockSubscription) Unsubscribe() {
sub.node.RemoveSubscription(sub.id)
}
func (sub *mockSubscription) publish(b *types.Block) {
sub.headCh <- b.Header()
}
func (sub *mockSubscription) publishAll(blocks []*types.Block) {
for _, b := range blocks {
sub.publish(b)
}
}