forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_fetch_das.go
105 lines (89 loc) · 2.66 KB
/
chain_fetch_das.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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"context"
"errors"
"sync"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/das/dastree"
"github.com/offchainlabs/nitro/solgen/go/bridgegen"
"github.com/offchainlabs/nitro/util/pretty"
)
type syncedKeysetCache struct {
cache map[[32]byte][]byte
sync.RWMutex
}
func (c *syncedKeysetCache) get(key [32]byte) ([]byte, bool) {
c.RLock()
defer c.RUnlock()
res, ok := c.cache[key]
return res, ok
}
func (c *syncedKeysetCache) put(key [32]byte, value []byte) {
c.Lock()
defer c.Unlock()
c.cache[key] = value
}
type KeysetFetcher struct {
seqInboxCaller *bridgegen.SequencerInboxCaller
seqInboxFilterer *bridgegen.SequencerInboxFilterer
keysetCache syncedKeysetCache
}
func NewKeysetFetcher(l1client *ethclient.Client, seqInboxAddr common.Address) (*KeysetFetcher, error) {
seqInbox, err := bridgegen.NewSequencerInbox(seqInboxAddr, l1client)
if err != nil {
return nil, err
}
return NewKeysetFetcherWithSeqInbox(seqInbox)
}
func NewKeysetFetcherWithSeqInbox(seqInbox *bridgegen.SequencerInbox) (*KeysetFetcher, error) {
return &KeysetFetcher{
seqInboxCaller: &seqInbox.SequencerInboxCaller,
seqInboxFilterer: &seqInbox.SequencerInboxFilterer,
keysetCache: syncedKeysetCache{cache: make(map[[32]byte][]byte)},
}, nil
}
func (c *KeysetFetcher) GetKeysetByHash(ctx context.Context, hash common.Hash) ([]byte, error) {
log.Trace("das.KeysetFetcher.GetKeysetByHash", "hash", pretty.PrettyHash(hash))
cache := &c.keysetCache
seqInboxCaller := c.seqInboxCaller
seqInboxFilterer := c.seqInboxFilterer
// try to fetch from the cache
res, ok := cache.get(hash)
if ok {
return res, nil
}
// try to fetch from the L1 chain
blockNumBig, err := seqInboxCaller.GetKeysetCreationBlock(&bind.CallOpts{Context: ctx}, hash)
if err != nil {
return nil, err
}
if !blockNumBig.IsUint64() {
return nil, errors.New("block number too large")
}
blockNum := blockNumBig.Uint64()
blockNumPlus1 := blockNum + 1
filterOpts := &bind.FilterOpts{
Start: blockNum,
End: &blockNumPlus1,
Context: ctx,
}
iter, err := seqInboxFilterer.FilterSetValidKeyset(filterOpts, [][32]byte{hash})
if err != nil {
return nil, err
}
for iter.Next() {
if dastree.ValidHash(hash, iter.Event.KeysetBytes) {
cache.put(hash, iter.Event.KeysetBytes)
return iter.Event.KeysetBytes, nil
}
}
if iter.Error() != nil {
return nil, iter.Error()
}
return nil, ErrNotFound
}