-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
bhs.go
207 lines (181 loc) · 6.34 KB
/
bhs.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
// The blockhash store package provides a service that stores blockhashes such that they are available
// for on-chain proofs beyond the EVM 256 block limit.
package blockhashstore
import (
"context"
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/pkg/errors"
txmgrcommon "github.com/smartcontractkit/chainlink/v2/common/txmgr"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/blockhash_store"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/trusted_blockhash_store"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)
var _ BHS = &BulletproofBHS{}
type bpBHSConfig interface {
LimitDefault() uint32
}
type bpBHSDatabaseConfig interface {
DefaultQueryTimeout() time.Duration
}
// BulletproofBHS is an implementation of BHS that writes "store" transactions to a bulletproof
// transaction manager, and reads BlockhashStore state from the contract.
type BulletproofBHS struct {
config bpBHSConfig
dbConfig bpBHSDatabaseConfig
jobID uuid.UUID
fromAddresses []ethkey.EIP55Address
txm txmgr.TxManager
abi *abi.ABI
trustedAbi *abi.ABI
bhs blockhash_store.BlockhashStoreInterface
trustedBHS *trusted_blockhash_store.TrustedBlockhashStore
chainID *big.Int
gethks keystore.Eth
}
// NewBulletproofBHS creates a new instance with the given transaction manager and blockhash store.
func NewBulletproofBHS(
config bpBHSConfig,
dbConfig bpBHSDatabaseConfig,
fromAddresses []ethkey.EIP55Address,
txm txmgr.TxManager,
bhs blockhash_store.BlockhashStoreInterface,
trustedBHS *trusted_blockhash_store.TrustedBlockhashStore,
chainID *big.Int,
gethks keystore.Eth,
) (*BulletproofBHS, error) {
bhsABI, err := blockhash_store.BlockhashStoreMetaData.GetAbi()
if err != nil {
// blockhash_store.BlockhashStoreABI is generated code, this should never happen
return nil, errors.Wrap(err, "building ABI")
}
trustedBHSAbi, err := trusted_blockhash_store.TrustedBlockhashStoreMetaData.GetAbi()
if err != nil {
return nil, errors.Wrap(err, "building trusted BHS ABI")
}
return &BulletproofBHS{
config: config,
dbConfig: dbConfig,
fromAddresses: fromAddresses,
txm: txm,
abi: bhsABI,
trustedAbi: trustedBHSAbi,
bhs: bhs,
trustedBHS: trustedBHS,
chainID: chainID,
gethks: gethks,
}, nil
}
// Store satisfies the BHS interface.
func (c *BulletproofBHS) Store(ctx context.Context, blockNum uint64) error {
payload, err := c.abi.Pack("store", new(big.Int).SetUint64(blockNum))
if err != nil {
return errors.Wrap(err, "packing args")
}
fromAddress, err := c.gethks.GetRoundRobinAddress(c.chainID, SendingKeys(c.fromAddresses)...)
if err != nil {
return errors.Wrap(err, "getting next from address")
}
_, err = c.txm.CreateTransaction(txmgr.TxRequest{
FromAddress: fromAddress,
ToAddress: c.bhs.Address(),
EncodedPayload: payload,
FeeLimit: c.config.LimitDefault(),
// Set a queue size of 256. At most we store the blockhash of every block, and only the
// latest 256 can possibly be stored.
Strategy: txmgrcommon.NewQueueingTxStrategy(c.jobID, 256, c.dbConfig.DefaultQueryTimeout()),
}, pg.WithParentCtx(ctx))
if err != nil {
return errors.Wrap(err, "creating transaction")
}
return nil
}
func (c *BulletproofBHS) StoreTrusted(
ctx context.Context,
blockNums []uint64,
blockhashes []common.Hash,
recentBlock uint64,
recentBlockhash common.Hash,
) error {
// Convert and pack arguments for a "storeTrusted" function call to the trusted BHS.
var blockNumsBig []*big.Int
for _, b := range blockNums {
blockNumsBig = append(blockNumsBig, new(big.Int).SetUint64(b))
}
recentBlockBig := new(big.Int).SetUint64(recentBlock)
payload, err := c.trustedAbi.Pack("storeTrusted", blockNumsBig, blockhashes, recentBlockBig, recentBlockhash)
if err != nil {
return errors.Wrap(err, "packing args")
}
// Create a transaction from the given batch and send it to the TXM.
fromAddress, err := c.gethks.GetRoundRobinAddress(c.chainID, SendingKeys(c.fromAddresses)...)
if err != nil {
return errors.Wrap(err, "getting next from address")
}
_, err = c.txm.CreateTransaction(txmgr.TxRequest{
FromAddress: fromAddress,
ToAddress: c.trustedBHS.Address(),
EncodedPayload: payload,
FeeLimit: c.config.LimitDefault(),
Strategy: txmgrcommon.NewSendEveryStrategy(),
}, pg.WithParentCtx(ctx))
if err != nil {
return errors.Wrap(err, "creating transaction")
}
return nil
}
func (c *BulletproofBHS) IsTrusted() bool {
return c.trustedBHS != nil
}
// IsStored satisfies the BHS interface.
func (c *BulletproofBHS) IsStored(ctx context.Context, blockNum uint64) (bool, error) {
var err error
if c.IsTrusted() {
_, err = c.trustedBHS.GetBlockhash(&bind.CallOpts{Context: ctx}, big.NewInt(int64(blockNum)))
} else {
_, err = c.bhs.GetBlockhash(&bind.CallOpts{Context: ctx}, big.NewInt(int64(blockNum)))
}
if err != nil && strings.Contains(err.Error(), "reverted") {
// Transaction reverted because the blockhash is not stored
return false, nil
} else if err != nil {
return false, errors.Wrap(err, "getting blockhash")
}
return true, nil
}
func (c *BulletproofBHS) sendingKeys() []common.Address {
var keys []common.Address
for _, a := range c.fromAddresses {
keys = append(keys, a.Address())
}
return keys
}
func (c *BulletproofBHS) StoreEarliest(ctx context.Context) error {
payload, err := c.abi.Pack("storeEarliest")
if err != nil {
return errors.Wrap(err, "packing args")
}
fromAddress, err := c.gethks.GetRoundRobinAddress(c.chainID, c.sendingKeys()...)
if err != nil {
return errors.Wrap(err, "getting next from address")
}
_, err = c.txm.CreateTransaction(txmgr.TxRequest{
FromAddress: fromAddress,
ToAddress: c.bhs.Address(),
EncodedPayload: payload,
FeeLimit: c.config.LimitDefault(),
Strategy: txmgrcommon.NewSendEveryStrategy(),
}, pg.WithParentCtx(ctx))
if err != nil {
return errors.Wrap(err, "creating transaction")
}
return nil
}