-
Notifications
You must be signed in to change notification settings - Fork 17
/
relay_test.go
333 lines (292 loc) · 9.94 KB
/
relay_test.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"mergemock/api"
"mergemock/types"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
"github.com/prysmaticlabs/prysm/crypto/bls"
"github.com/prysmaticlabs/prysm/runtime/version"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
type testRelayBackend struct {
*RelayBackend
}
func newTestRelay(t *testing.T) *testRelayBackend {
sk, err := bls.RandKey()
require.NoError(t, err)
relay, err := NewRelayBackend(logrus.New(), "127.0.0.1:38551", "127.0.0.1:38552", "0x1234000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(sk.Marshal()))
if err != nil {
t.Fatal("unable to create relay")
}
testRelay := testRelayBackend{relay}
testRelay.engine.JwtSecretPath = newJwt(t)
testRelay.engine.GenesisPath = newGenesis(t)
return &testRelay
}
func (mr *testRelayBackend) testRequest(t *testing.T, method string, path string, payload any) *httptest.ResponseRecorder {
var req *http.Request
var err error
if payload == nil {
req, err = http.NewRequest(method, path, nil)
} else {
payloadBytes, err2 := json.Marshal(payload)
require.NoError(t, err2)
req, err = http.NewRequest(method, path, bytes.NewReader(payloadBytes))
}
require.NoError(t, err)
rr := httptest.NewRecorder()
mr.getRouter().ServeHTTP(rr, req)
return rr
}
func newKeypair(t *testing.T) (pubkey []byte, privkey bls.SecretKey) {
sk, err := bls.RandKey()
if err != nil {
t.Fatal("unable to generate bls key pair", err)
}
return sk.PublicKey().Marshal(), sk
}
func newJwt(t *testing.T) string {
path := fmt.Sprintf("%s/jwt.hex", t.TempDir())
jwt := []byte("ed6588309287e7dbbb0ca2ba8c8be6e6063a72dc0f2235999ee6a751e8459cbc")
if err := os.WriteFile(path, jwt, 0644); err != nil {
t.Fatal("unable to write tmp jwt file")
}
return path
}
func newGenesis(t *testing.T) string {
path := fmt.Sprintf("%s/genesis.json", t.TempDir())
genesis := core.DeveloperGenesisBlock(5, 30_000_000, common.Address{})
genesis.Config.MergeForkBlock = common.Big0
genesis.Config.TerminalTotalDifficulty = common.Big0
buf, err := genesis.MarshalJSON()
if err != nil {
t.Fatal("cannot marshal tmp genesis")
}
if err := os.WriteFile(path, buf, 0644); err != nil {
t.Fatal("unable to write tmp genesis file")
}
return path
}
func TestStatusEndpoint(t *testing.T) {
relay := newTestRelay(t)
rr := relay.testRequest(t, "GET", "/eth/v1/builder/status", nil)
require.Equal(t, http.StatusOK, rr.Code)
}
func TestValidatorRegistration(t *testing.T) {
relay := newTestRelay(t)
pk1, sk1 := newKeypair(t)
pk2, sk2 := newKeypair(t)
var pubkey1 types.PublicKey
var pubkey2 types.PublicKey
pubkey1.FromSlice(pk1)
pubkey2.FromSlice(pk2)
msg1 := &types.RegisterValidatorRequestMessage{
FeeRecipient: types.Address{0x42},
GasLimit: 15_000_000,
Timestamp: uint64(time.Now().Unix()),
Pubkey: pubkey1,
}
msg2 := &types.RegisterValidatorRequestMessage{
FeeRecipient: types.Address{0x42},
GasLimit: 15_000_000,
Timestamp: uint64(time.Now().Unix()),
Pubkey: pubkey2,
}
root1, err := types.ComputeSigningRoot(msg1, types.DomainBuilder)
require.NoError(t, err)
root2, err := types.ComputeSigningRoot(msg2, types.DomainBuilder)
require.NoError(t, err)
// Success
var sig1 types.Signature
sig1.FromSlice(sk1.Sign(root1[:]).Marshal())
var sig2 types.Signature
sig2.FromSlice(sk2.Sign(root2[:]).Marshal())
rr := relay.testRequest(t, "POST", "/eth/v1/builder/validators", []types.SignedValidatorRegistration{
{
Message: msg1,
Signature: sig1,
},
{
Message: msg2,
Signature: sig2,
},
})
require.Equal(t, http.StatusOK, rr.Code)
// Invalid signature
sig1[len(sig1)-1] = 0x00
rr = relay.testRequest(t, "POST", "/eth/v1/builder/validators", []types.SignedValidatorRegistration{
{
Message: msg1,
Signature: sig1,
},
})
require.Equal(t, http.StatusBadRequest, rr.Code)
require.Equal(t, errInvalidSignature.Error()+"\n", rr.Body.String())
// Old registration
msg := &types.RegisterValidatorRequestMessage{
FeeRecipient: types.Address{0x42},
GasLimit: 15_000_000,
Timestamp: msg1.Timestamp,
Pubkey: pubkey1,
}
root, err := types.ComputeSigningRoot(msg, types.DomainBuilder)
var sig types.Signature
sig.FromSlice(sk1.Sign(root[:]).Marshal())
require.NoError(t, err)
rr = relay.testRequest(t, "POST", "/eth/v1/builder/validators", []types.SignedValidatorRegistration{
{
Message: msg,
Signature: sig,
},
})
require.Equal(t, http.StatusBadRequest, rr.Code)
require.Equal(t, errInvalidTimestamp.Error()+"\n", rr.Body.String())
}
func TestGetHeader(t *testing.T) {
ctx := context.Background()
relay := newTestRelay(t)
relay.engine.Run(ctx)
pk, _ := newKeypair(t)
parent := relay.engine.mockChain().CurrentHeader()
parentHash := parent.Hash()
// Initialize engine
_, err := relay.engine.backend.ForkchoiceUpdatedV1(
ctx,
&types.ForkchoiceStateV1{
HeadBlockHash: parentHash,
SafeBlockHash: parentHash,
FinalizedBlockHash: parentHash,
},
&types.PayloadAttributesV1{
Timestamp: parent.Time + 1,
PrevRandao: common.Hash{0x01},
SuggestedFeeRecipient: common.Address{0x02},
},
)
require.NoError(t, err, "unable to initialize engine")
path := fmt.Sprintf("/eth/v1/builder/header/%d/%s/0x%x", 0, parentHash.Hex(), pk)
rr := relay.testRequest(t, "GET", path, nil)
require.Equal(t, http.StatusOK, rr.Code, rr.Body.String())
bid := new(types.GetHeaderResponse)
err = json.Unmarshal(rr.Body.Bytes(), bid)
require.NoError(t, err)
require.Equal(t, parentHash[:], bid.Data.Message.Header.ParentHash[:], "didn't build on expected parent")
ok, err := types.VerifySignature(bid.Data.Message, types.DomainBuilder, relay.pk[:], bid.Data.Signature[:])
require.NoError(t, err, "error verifying signature")
require.True(t, ok, "bid signature not valid")
require.Equal(t, pk, relay.latestPubkey[:])
}
func TestGetPayload(t *testing.T) {
ctx := context.Background()
relay := newTestRelay(t)
relay.engine.Run(ctx)
pk, sk := newKeypair(t)
parent := relay.engine.mockChain().CurrentHeader()
parentHash := parent.Hash()
// Initialize engine
_, err := relay.engine.backend.ForkchoiceUpdatedV1(
ctx,
&types.ForkchoiceStateV1{
HeadBlockHash: parentHash,
SafeBlockHash: parentHash,
FinalizedBlockHash: parentHash,
},
&types.PayloadAttributesV1{
Timestamp: parent.Time + 1,
PrevRandao: common.Hash{0x01},
SuggestedFeeRecipient: common.Address{0x02},
},
)
require.NoError(t, err, "unable to initialize engine")
// Call getHeader to prepare payload
path := fmt.Sprintf("/eth/v1/builder/header/%d/%s/0x%x", 0, parentHash.Hex(), pk)
rr := relay.testRequest(t, "GET", path, nil)
require.Equal(t, http.StatusOK, rr.Code)
bid := new(types.GetHeaderResponse)
err = json.Unmarshal(rr.Body.Bytes(), bid)
require.NoError(t, err)
// Create request payload
msg := &types.BlindedBeaconBlock{
Slot: 1,
ProposerIndex: 2,
ParentRoot: types.Root{0x03},
StateRoot: types.Root{0x04},
Body: &types.BlindedBeaconBlockBody{
Eth1Data: &types.Eth1Data{
DepositRoot: types.Root{0x05},
DepositCount: 5,
BlockHash: types.Hash{0x06},
},
SyncAggregate: &types.SyncAggregate{
CommitteeBits: types.CommitteeBits{0x07},
CommitteeSignature: types.Signature{0x08},
},
ExecutionPayloadHeader: bid.Data.Message.Header,
},
}
// Sign payload
root, err := types.ComputeSigningRoot(msg, types.ComputeDomain(types.DomainTypeBeaconProposer, version.Bellatrix, &relay.genesisValidatorsRoot))
require.NoError(t, err)
sig := sk.Sign(root[:]).Marshal()
var signature types.Signature
signature.FromSlice(sig)
require.Equal(t, sig[:], signature[:])
// Call getPayload with invalid signature
rr = relay.testRequest(t, "POST", "/eth/v1/builder/blinded_blocks", types.SignedBlindedBeaconBlock{
Message: msg,
Signature: types.Signature{0x09},
})
require.Equal(t, http.StatusBadRequest, rr.Code)
// Call getPayload with correct signature
rr = relay.testRequest(t, "POST", "/eth/v1/builder/blinded_blocks", types.SignedBlindedBeaconBlock{
Message: msg,
Signature: signature,
})
// Verify getPayload response
require.Equal(t, http.StatusOK, rr.Code)
getPayloadResponse := new(types.GetPayloadResponse)
err = json.Unmarshal(rr.Body.Bytes(), getPayloadResponse)
require.NoError(t, err)
require.Equal(t, bid.Data.Message.Header.BlockHash, getPayloadResponse.Data.BlockHash)
}
func TestExecutionPayloadTransformations(t *testing.T) {
// Test: block -> EL payload -> CL payload -> EL payload -> block -> compare blockhash
relay := newTestRelay(t)
relay.engine.Run(context.Background())
parent := relay.engine.mockChain().CurrentHeader()
txsCreator := TransactionsCreator{nil, func(config *params.ChainConfig, bc core.ChainContext,
statedb *state.StateDB, header *ethTypes.Header, cfg vm.Config, accounts []TestAccount) []*ethTypes.Transaction {
return nil // TODO: create some transactions
}}
// Create a block
block1, err := relay.engine.mockChain().AddNewBlock(parent.Hash(), common.Address{0x02}, 12345, 23456, txsCreator, common.Hash{0x04}, []byte("hello"), nil, false)
require.NoError(t, err)
// Transform to EL payload
payloadEl, err := api.BlockToPayload(block1)
require.NoError(t, err)
// Transform EL payload to CL payload
payloadCl, err := types.ELPayloadToRESTPayload(payloadEl)
require.NoError(t, err)
// Transform CL payload back to EL payload
payloadEl2, err := types.RESTPayloadToELPayload(payloadCl)
require.NoError(t, err)
// Create a block from the 'new' EL payload and ensure correctness
block2, err := relay.engine.mockChain().ProcessPayload(payloadEl2)
require.NoError(t, err)
require.Equal(t, block1.Hash(), block2.Hash())
}