-
Notifications
You must be signed in to change notification settings - Fork 917
/
eth1.go
156 lines (142 loc) · 4.26 KB
/
eth1.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
package endtoend
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/big"
"os"
"os/exec"
"path"
"strings"
"testing"
"time"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
"github.com/prysmaticlabs/prysm/shared/params"
)
// startEth1 starts an eth1 local dev chain and deploys a deposit contract.
func startEth1(t *testing.T, tmpPath string) (common.Address, string, int) {
binaryPath, found := bazel.FindBinary("cmd/geth", "geth")
if !found {
t.Fatal("go-ethereum binary not found")
}
eth1Path := path.Join(tmpPath, "eth1data/")
// Clear out ETH1 to prevent issues.
if _, err := os.Stat(eth1Path); !os.IsNotExist(err) {
if err := os.RemoveAll(eth1Path); err != nil {
t.Fatal(err)
}
}
args := []string{
fmt.Sprintf("--datadir=%s", eth1Path),
"--rpc",
"--rpcaddr=0.0.0.0",
"--rpccorsdomain=\"*\"",
"--rpcvhosts=\"*\"",
"--rpcport=8745",
"--ws",
"--wsaddr=0.0.0.0",
"--wsorigins=\"*\"",
"--wsport=8746",
"--dev",
"--dev.period=0",
"--ipcdisable",
}
cmd := exec.Command(binaryPath, args...)
file, err := os.Create(path.Join(tmpPath, "eth1.log"))
if err != nil {
t.Fatal(err)
}
cmd.Stdout = file
cmd.Stderr = file
if err := cmd.Start(); err != nil {
t.Fatalf("Failed to start eth1 chain: %v", err)
}
if err = waitForTextInFile(file, "Commit new mining work"); err != nil {
t.Fatalf("mining log not found, this means the eth1 chain had issues starting: %v", err)
}
// Connect to the started geth dev chain.
client, err := rpc.DialHTTP("http://127.0.0.1:8745")
if err != nil {
t.Fatalf("Failed to connect to ipc: %v", err)
}
web3 := ethclient.NewClient(client)
// Access the dev account keystore to deploy the contract.
fileName, err := exec.Command("ls", path.Join(tmpPath, "eth1data/keystore")).Output()
if err != nil {
t.Fatal(err)
}
keystorePath := path.Join(tmpPath, fmt.Sprintf("eth1data/keystore/%s", strings.TrimSpace(string(fileName))))
jsonBytes, err := ioutil.ReadFile(keystorePath)
if err != nil {
t.Fatal(err)
}
keystore, err := keystore.DecryptKey(jsonBytes, "" /*password*/)
if err != nil {
t.Fatal(err)
}
// Advancing the blocks eth1follow distance to prevent issues reading the chain.
if err := mineBlocks(web3, keystore, params.BeaconConfig().Eth1FollowDistance); err != nil {
t.Fatalf("Unable to advance chain: %v", err)
}
txOpts, err := bind.NewTransactor(bytes.NewReader(jsonBytes), "" /*password*/)
if err != nil {
t.Fatal(err)
}
nonce, err := web3.PendingNonceAt(context.Background(), keystore.Address)
if err != nil {
t.Fatal(err)
}
txOpts.Nonce = big.NewInt(int64(nonce))
contractAddr, tx, _, err := contracts.DeployDepositContract(txOpts, web3, txOpts.From)
if err != nil {
t.Fatalf("Failed to deploy deposit contract: %v", err)
}
// Wait for contract to mine.
for pending := true; pending; _, pending, err = web3.TransactionByHash(context.Background(), tx.Hash()) {
if err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
}
return contractAddr, keystorePath, cmd.Process.Pid
}
func mineBlocks(web3 *ethclient.Client, keystore *keystore.Key, blocksToMake uint64) error {
nonce, err := web3.PendingNonceAt(context.Background(), keystore.Address)
if err != nil {
return err
}
chainID, err := web3.NetworkID(context.Background())
if err != nil {
return err
}
block, err := web3.BlockByNumber(context.Background(), nil)
if err != nil {
return err
}
finishBlock := block.NumberU64() + blocksToMake
for block.NumberU64() <= finishBlock {
spamTX := types.NewTransaction(nonce, keystore.Address, big.NewInt(0), 21000, big.NewInt(1e6), []byte{})
signed, err := types.SignTx(spamTX, types.NewEIP155Signer(chainID), keystore.PrivateKey)
if err != nil {
return err
}
if err := web3.SendTransaction(context.Background(), signed); err != nil {
return err
}
nonce++
time.Sleep(250 * time.Microsecond)
block, err = web3.BlockByNumber(context.Background(), nil)
if err != nil {
return err
}
}
return nil
}