/
ethereum_vrf_contracts.go
314 lines (286 loc) · 9.91 KB
/
ethereum_vrf_contracts.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
package contracts
import (
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/rs/zerolog/log"
"github.com/smartcontractkit/chainlink-testing-framework/blockchain"
"github.com/smartcontractkit/chainlink/integration-tests/wrappers"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/batch_blockhash_store"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/blockhash_store"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/solidity_vrf_consumer_interface"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/solidity_vrf_coordinator_interface"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/solidity_vrf_wrapper"
)
// LegacyEthereumBatchBlockhashStore represents BatchBlockhashStore contract
type LegacyEthereumBatchBlockhashStore struct {
address *common.Address
client blockchain.EVMClient
batchBlockhashStore *batch_blockhash_store.BatchBlockhashStore
}
// LegacyEthereumBlockhashStore represents a blockhash store for VRF contract
type LegacyEthereumBlockhashStore struct {
address *common.Address
client blockchain.EVMClient
blockHashStore *blockhash_store.BlockhashStore
}
// LegacyEthereumVRFConsumer represents VRF consumer contract
type LegacyEthereumVRFConsumer struct {
address *common.Address
client blockchain.EVMClient
consumer *solidity_vrf_consumer_interface.VRFConsumer
}
// VRFConsumerRoundConfirmer is a header subscription that awaits for a certain VRF round to be completed
type VRFConsumerRoundConfirmer struct {
consumer VRFConsumer
roundID *big.Int
doneChan chan struct{}
context context.Context
cancel context.CancelFunc
done bool
}
// LegacyEthereumVRF represents a VRF contract
type LegacyEthereumVRF struct {
client blockchain.EVMClient
vrf *solidity_vrf_wrapper.VRF
address *common.Address
}
// DeployVRFContract deploy VRF contract
func (e *EthereumContractDeployer) DeployVRFContract() (VRF, error) {
address, _, instance, err := e.client.DeployContract("VRF", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
return solidity_vrf_wrapper.DeployVRF(auth, wrappers.MustNewWrappedContractBackend(e.client, nil))
})
if err != nil {
return nil, err
}
return &LegacyEthereumVRF{
client: e.client,
vrf: instance.(*solidity_vrf_wrapper.VRF),
address: address,
}, err
}
// DeployBlockhashStore deploys blockhash store used with VRF contract
func (e *EthereumContractDeployer) DeployBlockhashStore() (BlockHashStore, error) {
address, _, instance, err := e.client.DeployContract("BlockhashStore", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
return blockhash_store.DeployBlockhashStore(auth, wrappers.MustNewWrappedContractBackend(e.client, nil))
})
if err != nil {
return nil, err
}
return &LegacyEthereumBlockhashStore{
client: e.client,
blockHashStore: instance.(*blockhash_store.BlockhashStore),
address: address,
}, err
}
// DeployVRFCoordinator deploys VRF coordinator contract
func (e *EthereumContractDeployer) DeployVRFCoordinator(linkAddr string, bhsAddr string) (VRFCoordinator, error) {
address, _, instance, err := e.client.DeployContract("VRFCoordinator", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
return solidity_vrf_coordinator_interface.DeployVRFCoordinator(auth, wrappers.MustNewWrappedContractBackend(e.client, nil), common.HexToAddress(linkAddr), common.HexToAddress(bhsAddr))
})
if err != nil {
return nil, err
}
return &LegacyEthereumVRFCoordinator{
client: e.client,
coordinator: instance.(*solidity_vrf_coordinator_interface.VRFCoordinator),
address: address,
}, err
}
// DeployVRFConsumer deploys VRF consumer contract
func (e *EthereumContractDeployer) DeployVRFConsumer(linkAddr string, coordinatorAddr string) (VRFConsumer, error) {
address, _, instance, err := e.client.DeployContract("VRFConsumer", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
return solidity_vrf_consumer_interface.DeployVRFConsumer(auth, wrappers.MustNewWrappedContractBackend(e.client, nil), common.HexToAddress(coordinatorAddr), common.HexToAddress(linkAddr))
})
if err != nil {
return nil, err
}
return &LegacyEthereumVRFConsumer{
client: e.client,
consumer: instance.(*solidity_vrf_consumer_interface.VRFConsumer),
address: address,
}, err
}
func (v *LegacyEthereumBlockhashStore) Address() string {
return v.address.Hex()
}
func (v *LegacyEthereumBlockhashStore) GetBlockHash(ctx context.Context, blockNumber *big.Int) ([32]byte, error) {
opts := &bind.CallOpts{
From: common.HexToAddress(v.client.GetDefaultWallet().Address()),
Context: ctx,
}
blockHash, err := v.blockHashStore.GetBlockhash(opts, blockNumber)
if err != nil {
return [32]byte{}, err
}
return blockHash, nil
}
func (v *LegacyEthereumVRFCoordinator) Address() string {
return v.address.Hex()
}
// HashOfKey get a hash of proving key to use it as a request ID part for VRF
func (v *LegacyEthereumVRFCoordinator) HashOfKey(ctx context.Context, pubKey [2]*big.Int) ([32]byte, error) {
opts := &bind.CallOpts{
From: common.HexToAddress(v.client.GetDefaultWallet().Address()),
Context: ctx,
}
hash, err := v.coordinator.HashOfKey(opts, pubKey)
if err != nil {
return [32]byte{}, err
}
return hash, nil
}
// RegisterProvingKey register VRF proving key
func (v *LegacyEthereumVRFCoordinator) RegisterProvingKey(
fee *big.Int,
oracleAddr string,
publicProvingKey [2]*big.Int,
jobID [32]byte,
) error {
opts, err := v.client.TransactionOpts(v.client.GetDefaultWallet())
if err != nil {
return err
}
tx, err := v.coordinator.RegisterProvingKey(opts, fee, common.HexToAddress(oracleAddr), publicProvingKey, jobID)
if err != nil {
return err
}
return v.client.ProcessTransaction(tx)
}
func (v *LegacyEthereumVRFConsumer) Address() string {
return v.address.Hex()
}
func (v *LegacyEthereumVRFConsumer) Fund(ethAmount *big.Float) error {
gasEstimates, err := v.client.EstimateGas(ethereum.CallMsg{
To: v.address,
})
if err != nil {
return err
}
return v.client.Fund(v.address.Hex(), ethAmount, gasEstimates)
}
// RequestRandomness requests VRF randomness
func (v *LegacyEthereumVRFConsumer) RequestRandomness(hash [32]byte, fee *big.Int) error {
opts, err := v.client.TransactionOpts(v.client.GetDefaultWallet())
if err != nil {
return err
}
tx, err := v.consumer.TestRequestRandomness(opts, hash, fee)
if err != nil {
return err
}
return v.client.ProcessTransaction(tx)
}
// CurrentRoundID helper roundID counter in consumer to check when all randomness requests are finished
func (v *LegacyEthereumVRFConsumer) CurrentRoundID(ctx context.Context) (*big.Int, error) {
opts := &bind.CallOpts{
From: common.HexToAddress(v.client.GetDefaultWallet().Address()),
Context: ctx,
}
return v.consumer.CurrentRoundID(opts)
}
// RandomnessOutput get VRF randomness output
func (v *LegacyEthereumVRFConsumer) RandomnessOutput(ctx context.Context) (*big.Int, error) {
opts := &bind.CallOpts{
From: common.HexToAddress(v.client.GetDefaultWallet().Address()),
Context: ctx,
}
out, err := v.consumer.RandomnessOutput(opts)
if err != nil {
return nil, err
}
return out, nil
}
// NewVRFConsumerRoundConfirmer provides a new instance of a NewVRFConsumerRoundConfirmer
func NewVRFConsumerRoundConfirmer(
contract VRFConsumer,
roundID *big.Int,
timeout time.Duration,
) *VRFConsumerRoundConfirmer {
ctx, ctxCancel := context.WithTimeout(context.Background(), timeout)
return &VRFConsumerRoundConfirmer{
consumer: contract,
roundID: roundID,
doneChan: make(chan struct{}),
context: ctx,
cancel: ctxCancel,
}
}
// ReceiveHeader will query the latest VRFConsumer round and check to see whether the round has confirmed
func (f *VRFConsumerRoundConfirmer) ReceiveHeader(header blockchain.NodeHeader) error {
if f.done {
return nil
}
roundID, err := f.consumer.CurrentRoundID(context.Background())
if err != nil {
return err
}
logFields := map[string]any{
"Contract Address": f.consumer.Address(),
"Waiting for Round": f.roundID.Int64(),
"Current Round ID": roundID.Int64(),
"Header Number": header.Number.Uint64(),
}
if roundID.Int64() == f.roundID.Int64() {
randomness, err := f.consumer.RandomnessOutput(context.Background())
if err != nil {
return err
}
log.Info().Fields(logFields).Uint64("Randomness", randomness.Uint64()).Msg("VRFConsumer round completed")
f.done = true
f.doneChan <- struct{}{}
} else {
log.Debug().Fields(logFields).Msg("Waiting for VRFConsumer round")
}
return nil
}
// Wait is a blocking function that will wait until the round has confirmed, and timeout if the deadline has passed
func (f *VRFConsumerRoundConfirmer) Wait() error {
for {
select {
case <-f.doneChan:
f.cancel()
return nil
case <-f.context.Done():
return fmt.Errorf("timeout waiting for VRFConsumer round to confirm: %d", f.roundID)
}
}
}
// Fund sends specified currencies to the contract
func (v *LegacyEthereumVRF) Fund(ethAmount *big.Float) error {
gasEstimates, err := v.client.EstimateGas(ethereum.CallMsg{
To: v.address,
})
if err != nil {
return err
}
return v.client.Fund(v.address.Hex(), ethAmount, gasEstimates)
}
// ProofLength returns the PROOFLENGTH call from the VRF contract
func (v *LegacyEthereumVRF) ProofLength(ctxt context.Context) (*big.Int, error) {
opts := &bind.CallOpts{
From: common.HexToAddress(v.client.GetDefaultWallet().Address()),
Context: ctxt,
}
return v.vrf.PROOFLENGTH(opts)
}
func (v *LegacyEthereumBatchBlockhashStore) Address() string {
return v.address.Hex()
}