-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhelpers.go
More file actions
488 lines (412 loc) · 12.3 KB
/
helpers.go
File metadata and controls
488 lines (412 loc) · 12.3 KB
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
package tests
import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"os"
"os/exec"
"strings"
"testing"
"time"
evmTypes "github.com/onflow/flow-go/fvm/evm/types"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-evm-gateway/bootstrap"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/goccy/go-json"
"github.com/onflow/cadence"
"github.com/onflow/flow-emulator/adapters"
"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/flow-emulator/server"
sdk "github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go-sdk/crypto"
evmEmulator "github.com/onflow/flow-go/fvm/evm/emulator"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"
"github.com/rs/zerolog"
"github.com/onflow/flow-evm-gateway/config"
)
var (
logger = zerolog.New(zerolog.NewConsoleWriter())
sc = systemcontracts.SystemContractsForChain(flow.Emulator)
logOutput = os.Getenv("LOG_OUTPUT")
eoaTestAccount = common.HexToAddress(eoaTestAddress)
)
const (
sigAlgo = crypto.SignatureAlgorithm(crypto.ECDSA_P256)
hashAlgo = crypto.HashAlgorithm(crypto.SHA3_256)
servicePrivateKey = "61ceacbdce419e25ee8e7c2beceee170a05c9cab1e725a955b15ba94dcd747d2"
// this is a test eoa account created on account setup
eoaTestAddress = "0xFACF71692421039876a5BB4F10EF7A439D8ef61E"
eoaTestPrivateKey = "f6d5333177711e562cabf1f311916196ee6ffc2a07966d9d4628094073bd5442"
coinbaseAddress = "0x658Bdf435d810C91414eC09147DAA6DB62406379"
eoaFundAmount = 5.0
coaFundAmount = 10.0
)
func testLogWriter() io.Writer {
if logOutput == "false" {
return zerolog.Nop()
}
return zerolog.NewConsoleWriter()
}
func defaultServerConfig() *server.Config {
pkey, err := crypto.DecodePrivateKeyHex(sigAlgo, servicePrivateKey)
if err != nil {
panic(err)
}
genesisToken, err := cadence.NewUFix64("10000.0")
if err != nil {
panic(err)
}
return &server.Config{
ServicePrivateKey: pkey,
ServiceKeySigAlgo: sigAlgo,
ServiceKeyHashAlgo: hashAlgo,
GenesisTokenSupply: genesisToken,
WithContracts: true,
Host: "localhost",
TransactionExpiry: flow.DefaultTransactionExpiry,
TransactionMaxGasLimit: flow.DefaultMaxTransactionGasLimit,
SetupEVMEnabled: true,
SetupVMBridgeEnabled: true,
}
}
func startEmulator(createTestAccounts bool, conf *server.Config) (
*server.EmulatorServer,
error,
) {
log := logger.With().Timestamp().Str("component", "emulator").Logger().Level(zerolog.DebugLevel)
if logOutput == "false" {
log = zerolog.Nop()
}
srv := server.NewEmulatorServer(&log, conf)
go func() {
srv.Start()
}()
time.Sleep(1000 * time.Millisecond) // give it some time to start, dummy but ok for test
if createTestAccounts {
if err := setupTestAccounts(srv.Emulator()); err != nil {
return nil, err
}
}
return srv, nil
}
// runWeb3Test will run the test by name, the name
// must match an existing js test file (without the extension)
func runWeb3Test(t *testing.T, name string) {
_, stop := servicesSetup(t)
executeTest(t, name)
stop()
}
func runWeb3TestWithSetup(
t *testing.T,
name string,
setupFunc func(emu emulator.Emulator),
) {
emu, stop := servicesSetup(t)
setupFunc(emu)
executeTest(t, name)
stop()
}
// servicesSetup starts up an emulator and the gateway
// engines required for operation of the evm gateway.
func servicesSetup(t *testing.T) (emulator.Emulator, func()) {
srv, err := startEmulator(true, defaultServerConfig())
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
emu := srv.Emulator()
service := emu.ServiceKey()
grpcHost := "localhost:3569"
client, err := grpc.NewClient(grpcHost)
require.NoError(t, err)
// create new account with keys used for key-rotation
keyCount := 5
coaAddress, privateKey, err := bootstrap.CreateMultiKeyAccount(
client,
keyCount,
service.Address,
sc.FungibleToken.Address.HexWithPrefix(),
sc.FlowToken.Address.HexWithPrefix(),
service.PrivateKey,
)
require.NoError(t, err)
// default config
cfg := config.Config{
DatabaseDir: t.TempDir(),
AccessNodeHost: "localhost:3569", // emulator
RPCPort: 8545,
RPCHost: "127.0.0.1",
FlowNetworkID: "flow-emulator",
EVMNetworkID: evmTypes.FlowEVMPreviewNetChainID,
Coinbase: common.HexToAddress(coinbaseAddress),
COAAddress: *coaAddress,
COAKey: privateKey,
GasPrice: new(big.Int).SetUint64(150),
EnforceGasPrice: true,
LogLevel: zerolog.DebugLevel,
LogWriter: testLogWriter(),
RateLimit: 500,
WSEnabled: true,
MetricsPort: 8443,
FilterExpiry: time.Second * 5,
TxStateValidation: config.LocalIndexValidation,
}
bootstrapDone := make(chan struct{})
go func() {
err = bootstrap.Run(ctx, cfg, func() {
close(bootstrapDone)
})
require.NoError(t, err)
}()
<-bootstrapDone
return emu, func() {
cancel()
srv.Stop()
}
}
// executeTest will run the provided JS test file using mocha
// and will report failure or success of the test.
func executeTest(t *testing.T, testFile string) {
command := fmt.Sprintf(
"./web3js/node_modules/.bin/mocha ./web3js/%s.js --timeout 150s --exit",
testFile,
)
parts := strings.Fields(command)
t.Run(testFile, func(t *testing.T) {
cmd := exec.Command(parts[0], parts[1:]...)
if cmd.Err != nil {
t.Log(cmd.Err.Error())
panic(cmd.Err)
}
out, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(out))
var exitError *exec.ExitError
if errors.As(err, &exitError) {
if exitError.ExitCode() >= 1 {
require.Fail(t, err.Error())
}
t.Fatalf("unknown test issue: %s, output: %s", err.Error(), string(out))
}
require.Fail(t, err.Error())
}
})
}
// setupTestAccounts creates a service COA and stores it in storage as well as fund it,
// it also funds an EOA test account.
func setupTestAccounts(emu emulator.Emulator) error {
code := `
transaction(eoaAddress: [UInt8; 20]) {
let fundVault: @FlowToken.Vault
let auth: auth(Capabilities, Storage) &Account
let coa: auth(EVM.Call) &EVM.CadenceOwnedAccount
prepare(signer: auth(Capabilities, Storage) &Account) {
let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(
from: /storage/flowTokenVault
) ?? panic("Could not borrow reference to the owner's Vault!")
self.fundVault <- vaultRef.withdraw(amount: 10.0) as! @FlowToken.Vault
self.auth = signer
if !signer.storage.check<@EVM.CadenceOwnedAccount>(from: /storage/evm_coa) {
signer.storage.save<@EVM.CadenceOwnedAccount>(
<- EVM.createCadenceOwnedAccount(),
to: /storage/evm_coa
)
}
if !signer.capabilities.exists(/public/evm) {
let cap = signer.capabilities.storage.issue<&EVM.CadenceOwnedAccount>(/storage/evm_coa)
signer.capabilities.publish(cap, at: /public/evm)
}
self.coa = signer.storage.borrow<auth(EVM.Call) &EVM.CadenceOwnedAccount>(from: /storage/evm_coa)!
}
execute {
self.coa.deposit(from: <-self.fundVault)
let weiAmount: UInt = 5000000000000000000 // 5 Flow
let result = self.coa.call(
to: EVM.EVMAddress(bytes: eoaAddress),
data: [],
gasLimit: 300000,
value: EVM.Balance(attoflow: weiAmount)
)
}
}`
eoaBytes, err := evmHexToCadenceBytes(eoaTestAddress)
if err != nil {
return err
}
res, err := flowSendTransaction(emu, code, eoaBytes)
if err != nil {
return err
}
if res.Error != nil {
return res.Error
}
return nil
}
// flowSendTransaction sends an evm transaction to the emulator, the code provided doesn't
// have to import EVM, this will be handled by this helper function.
func flowSendTransaction(
emu emulator.Emulator,
code string,
args ...cadence.Value,
) (*sdk.TransactionResult, error) {
key := emu.ServiceKey()
codeWrapper := []byte(fmt.Sprintf(`
import EVM from %s
import FungibleToken from %s
import FlowToken from %s
%s`,
sc.EVMContract.Address.HexWithPrefix(),
sc.FungibleToken.Address.HexWithPrefix(),
sc.FlowToken.Address.HexWithPrefix(),
code,
))
log := logger.With().Timestamp().Str("component", "adapter").Logger().Level(zerolog.DebugLevel)
adapter := adapters.NewSDKAdapter(&log, emu)
blk, _, err := adapter.GetLatestBlock(context.Background(), true)
if err != nil {
return nil, err
}
tx := sdk.NewTransaction().
SetScript(codeWrapper).
SetComputeLimit(flow.DefaultMaxTransactionGasLimit).
SetProposalKey(key.Address, key.Index, key.SequenceNumber).
SetPayer(key.Address).
SetReferenceBlockID(blk.ID).
AddAuthorizer(key.Address)
for _, arg := range args {
err := tx.AddArgument(arg)
if err != nil {
return nil, err
}
}
signer, err := key.Signer()
if err != nil {
return nil, err
}
err = tx.SignEnvelope(key.Address, key.Index, signer)
if err != nil {
return nil, err
}
err = adapter.SendTransaction(context.Background(), *tx)
if err != nil {
return nil, err
}
res, err := adapter.GetTransactionResult(context.Background(), tx.ID())
if err != nil {
return nil, err
}
return res, nil
}
func evmSign(
weiValue *big.Int,
gasLimit uint64,
signer *ecdsa.PrivateKey,
nonce uint64,
to *common.Address,
data []byte) ([]byte, common.Hash, error) {
gasPrice := big.NewInt(0)
evmTx := types.NewTx(&types.LegacyTx{Nonce: nonce, To: to, Value: weiValue, Gas: gasLimit, GasPrice: gasPrice, Data: data})
signed, err := types.SignTx(evmTx, evmEmulator.GetDefaultSigner(), signer)
if err != nil {
return nil, common.Hash{}, fmt.Errorf("error signing EVM transaction: %w", err)
}
var encoded bytes.Buffer
err = signed.EncodeRLP(&encoded)
if err != nil {
return nil, common.Hash{}, fmt.Errorf("error encoding EVM transaction: %w", err)
}
return encoded.Bytes(), signed.Hash(), nil
}
// evmHexToString takes an evm address as string and convert it to cadence byte array
func evmHexToCadenceBytes(address string) (cadence.Array, error) {
address = strings.ReplaceAll(address, "0x", "")
data, err := hex.DecodeString(address)
if err != nil {
return cadence.NewArray(nil), err
}
res := make([]cadence.Value, 0)
for _, d := range data {
res = append(res, cadence.UInt8(d))
}
return cadence.NewArray(res), nil
}
// todo remove this after integration test is refactored
type rpcTest struct {
url string
}
// rpcRequest takes url, method (eg. "eth_getBlockByNumber") and params (eg. `["0x03"]` or `[]` if empty)
func (r *rpcTest) request(method string, params string) (json.RawMessage, error) {
reqURL := fmt.Sprintf(`{"jsonrpc":"2.0","id":0,"method":"%s","params":%s}`, method, params)
body := bytes.NewReader([]byte(reqURL))
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s", r.url), body)
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/json")
req.Header.Set("accept-encoding", "identity")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
content, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
type rpcResult struct {
Result json.RawMessage `json:"result"`
Error any `json:"error"`
}
var resp rpcResult
err = json.Unmarshal(content, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, fmt.Errorf("%s", resp.Error)
}
return resp.Result, nil
}
func (r *rpcTest) getReceipt(hash string) (*types.Receipt, error) {
rpcRes, err := r.request("eth_getTransactionReceipt", fmt.Sprintf(`["%s"]`, hash))
if err != nil {
return nil, err
}
var rcp types.Receipt
err = json.Unmarshal(rpcRes, &rcp)
if err != nil {
return nil, err
}
return &rcp, nil
}
func (r *rpcTest) sendRawTx(signed []byte) (common.Hash, error) {
rpcRes, err := r.request("eth_sendRawTransaction", fmt.Sprintf(`["0x%x"]`, signed))
if err != nil {
return common.Hash{}, err
}
var h common.Hash
err = json.Unmarshal(rpcRes, &h)
if err != nil {
return common.Hash{}, err
}
return h, nil
}
func (r *rpcTest) getBalance(address common.Address) (*big.Int, error) {
balanceRes, err := r.request("eth_getBalance", fmt.Sprintf(`["%s", "latest"]`, address))
if err != nil {
return nil, err
}
var balance hexutil.Big
err = json.Unmarshal(balanceRes, &balance)
if err != nil {
return nil, err
}
return balance.ToInt(), nil
}