/
eth1.go
executable file
·231 lines (199 loc) · 4.9 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
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
package testutil
import (
"fmt"
"net/http"
"time"
"github.com/umbracle/eth2-validator/internal/beacon"
"github.com/umbracle/eth2-validator/internal/bls"
"github.com/umbracle/eth2-validator/internal/deposit"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/contract"
"github.com/umbracle/ethgo/jsonrpc"
"github.com/umbracle/ethgo/wallet"
)
type NodeClient string
const (
Teku NodeClient = "teku"
Prysm NodeClient = "prysm"
Lighthouse NodeClient = "lighthouse"
)
type Account struct {
Bls *bls.Key
Ecdsa *wallet.Key
}
func NewAccount() *Account {
key, err := wallet.GenerateKey()
if err != nil {
panic(fmt.Errorf("BUG: failed to generate key %v", err))
}
account := &Account{
Bls: bls.NewRandomKey(),
Ecdsa: key,
}
return account
}
// Eth1Server is an eth1x testutil server using go-ethereum
type Eth1Server struct {
*node
deposit ethgo.Address
client *jsonrpc.Client
}
// NewEth1Server creates a new eth1 server with go-ethereum
func NewEth1Server() (*Eth1Server, error) {
cmd := []string{
"--dev",
"--dev.period", "1",
"--http", "--http.addr", "0.0.0.0",
"--http.port", `{{ Port "eth1.http" }}`,
"--ws", "--ws.addr", "0.0.0.0",
}
opts := []nodeOption{
WithName("eth1"),
WithContainer("ethereum/client-go", "v1.9.25"),
WithCmd(cmd),
WithRetry(func(n *node) error {
return testHTTPEndpoint(n.GetAddr(NodePortEth1Http))
}),
}
node, err := newNode(opts...)
if err != nil {
return nil, err
}
server := &Eth1Server{
node: node,
}
provider, err := jsonrpc.NewClient(server.http())
if err != nil {
return nil, err
}
server.client = provider
if err := server.deployDeposit(); err != nil {
return nil, err
}
return server, nil
}
func (e *Eth1Server) http() string {
return e.GetAddr(NodePortEth1Http)
}
const (
defaultGasPrice = 1879048192 // 0x70000000
defaultGasLimit = 5242880 // 0x500000
)
func (e *Eth1Server) fund(addr ethgo.Address) error {
nonce, err := e.client.Eth().GetNonce(addr, ethgo.Latest)
if err != nil {
return err
}
txn := ðgo.Transaction{
From: e.Owner(),
To: &addr,
Nonce: nonce,
GasPrice: defaultGasPrice,
Gas: defaultGasLimit,
// fund the account with enoung balance to validate and send the transaction
Value: ethgo.Ether(deposit.MinGweiAmount + 1),
}
hash, err := e.client.Eth().SendTransaction(txn)
if err != nil {
return err
}
if _, err := e.waitForReceipt(hash); err != nil {
return err
}
return nil
}
func (e *Eth1Server) waitForReceipt(hash ethgo.Hash) (*ethgo.Receipt, error) {
var count uint64
for {
receipt, err := e.client.Eth().GetTransactionReceipt(hash)
if err != nil {
if err.Error() != "not found" {
return nil, err
}
}
if receipt != nil {
return receipt, nil
}
if count > 120 {
break
}
time.Sleep(1 * time.Second)
count++
}
return nil, fmt.Errorf("timeout")
}
// Provider returns the jsonrpc provider
func (e *Eth1Server) Provider() *jsonrpc.Client {
return e.client
}
// Owner returns the account with balance on go-ethereum
func (e *Eth1Server) Owner() ethgo.Address {
owner, _ := e.Provider().Eth().Accounts()
return owner[0]
}
// DeployDeposit deploys the eth2.0 deposit contract
func (e *Eth1Server) deployDeposit() error {
provider := e.Provider()
owner, err := provider.Eth().Accounts()
if err != nil {
return err
}
deployTxn := ðgo.Transaction{
Input: deposit.DepositBin(),
From: owner[0],
}
hash, err := provider.Eth().SendTransaction(deployTxn)
if err != nil {
return err
}
receipt, err := e.waitForReceipt(hash)
if err != nil {
return err
}
e.deposit = receipt.ContractAddress
return nil
}
func (e *Eth1Server) Deposit() ethgo.Address {
return e.deposit
}
func (e *Eth1Server) GetDepositContract() *deposit.Deposit {
return deposit.NewDeposit(e.deposit, contract.WithJsonRPC(e.Provider().Eth()))
}
// MakeDeposit deposits the minimum required value to become a validator
func (e *Eth1Server) MakeDeposit(account *Account, config *beacon.ChainConfig) error {
depositAmount := deposit.MinGweiAmount
// fund the owner address
if err := e.fund(account.Ecdsa.Address()); err != nil {
return err
}
data, err := deposit.Input(account.Bls, nil, ethgo.Gwei(depositAmount).Uint64(), config)
if err != nil {
return err
}
depositC := deposit.NewDeposit(e.deposit, contract.WithSender(account.Ecdsa), contract.WithJsonRPC(e.Provider().Eth()))
txn, err := depositC.Deposit(data.Pubkey, data.WithdrawalCredentials, data.Signature, data.Root)
if err != nil {
return err
}
txn.WithOpts(&contract.TxnOpts{Value: ethgo.Ether(depositAmount)})
if err := txn.Do(); err != nil {
return err
}
if _, err := txn.Wait(); err != nil {
return err
}
return nil
}
func testHTTPEndpoint(endpoint string) error {
resp, err := http.Post(endpoint, "application/json", nil)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
type Node interface {
IP() string
Type() NodeClient
GetAddr(NodePort) string
}