Skip to content

Commit 4321483

Browse files
authored
Fix the msg bus deploy at genesis (#1652)
* Fix the msg bus deploy at genesis * tests * remove ports on same test * remove more test ports * lint
1 parent 0b47af6 commit 4321483

File tree

5 files changed

+65
-46
lines changed

5 files changed

+65
-46
lines changed

go/enclave/components/batch_executor.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
208208
return nil, fmt.Errorf("failed adding cross chain data to batch. Cause: %w", err)
209209
}
210210

211-
executor.populateHeader(&copyBatch, allReceipts(txReceipts, ccReceipts))
211+
allReceipts := append(txReceipts, ccReceipts...)
212+
executor.populateHeader(&copyBatch, allReceipts)
212213
if failForEmptyBatch &&
213214
len(txReceipts) == 0 &&
214215
len(ccReceipts) == 0 &&
@@ -220,7 +221,7 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
220221
}
221222

222223
// the logs and receipts produced by the EVM have the wrong hash which must be adjusted
223-
for _, receipt := range txReceipts {
224+
for _, receipt := range allReceipts {
224225
receipt.BlockHash = copyBatch.Hash()
225226
for _, l := range receipt.Logs {
226227
l.BlockHash = copyBatch.Hash()
@@ -229,7 +230,7 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext, fail
229230

230231
return &ComputedBatch{
231232
Batch: &copyBatch,
232-
Receipts: txReceipts,
233+
Receipts: allReceipts,
233234
Commit: func(deleteEmptyObjects bool) (gethcommon.Hash, error) {
234235
executor.stateDBMutex.Lock()
235236
defer executor.stateDBMutex.Unlock()
@@ -429,10 +430,6 @@ func (executor *batchExecutor) processTransactions(
429430
return executedTransactions, excludedTransactions, txReceipts, nil
430431
}
431432

432-
func allReceipts(txReceipts []*types.Receipt, depositReceipts []*types.Receipt) types.Receipts {
433-
return append(txReceipts, depositReceipts...)
434-
}
435-
436433
type sortByTxIndex []*types.Receipt
437434

438435
func (c sortByTxIndex) Len() int { return len(c) }

go/enclave/nodetype/sequencer.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,33 @@ func (s *sequencer) createGenesisBatch(block *common.L1Block) error {
164164
return err
165165
}
166166

167-
// make sure the mempool queuing system is initialized before adding the msg bus tx to it
168-
time.Sleep(time.Second)
167+
// produce batch #2 which has the message bus and any other system contracts
168+
cb, err := s.produceBatch(
169+
batch.Header.SequencerOrderNo.Add(batch.Header.SequencerOrderNo, big.NewInt(1)),
170+
block.Hash(),
171+
batch.Hash(),
172+
common.L2Transactions{msgBusTx},
173+
uint64(time.Now().Unix()),
174+
false,
175+
)
176+
if err != nil {
177+
if errors.Is(err, components.ErrNoTransactionsToProcess) {
178+
// skip batch production when there are no transactions to process
179+
// todo: this might be a useful event to track for metrics (skipping batch production because empty batch)
180+
s.logger.Debug("Skipping batch production, no transactions to execute")
181+
return nil
182+
}
183+
return fmt.Errorf(" failed producing batch. Cause: %w", err)
184+
}
169185

170-
if err = s.mempool.Add(msgBusTx); err != nil {
171-
return fmt.Errorf("failed to queue message bus creation transaction to genesis - %w", err)
186+
if len(cb.Receipts) == 0 || cb.Receipts[0].TxHash.Hex() != msgBusTx.Hash().Hex() {
187+
err = fmt.Errorf("message Bus contract not minted - no receipts in batch")
188+
s.logger.Error(err.Error())
189+
return err
172190
}
191+
192+
s.logger.Info("Message Bus Contract minted successfully", "address", cb.Receipts[0].ContractAddress.Hex())
193+
173194
return nil
174195
}
175196

@@ -232,7 +253,14 @@ func (s *sequencer) createNewHeadBatch(l1HeadBlock *common.L1Block, skipBatchIfE
232253
return nil
233254
}
234255

235-
func (s *sequencer) produceBatch(sequencerNo *big.Int, l1Hash common.L1BlockHash, headBatch common.L2BatchHash, transactions common.L2Transactions, batchTime uint64, failForEmptyBatch bool) (*core.Batch, error) {
256+
func (s *sequencer) produceBatch(
257+
sequencerNo *big.Int,
258+
l1Hash common.L1BlockHash,
259+
headBatch common.L2BatchHash,
260+
transactions common.L2Transactions,
261+
batchTime uint64,
262+
failForEmptyBatch bool,
263+
) (*components.ComputedBatch, error) {
236264
cb, err := s.batchProducer.ComputeBatch(&components.BatchExecutionContext{
237265
BlockPtr: l1Hash,
238266
ParentPtr: headBatch,
@@ -268,7 +296,7 @@ func (s *sequencer) produceBatch(sequencerNo *big.Int, l1Hash common.L1BlockHash
268296
return nil, fmt.Errorf("unable to remove tx from mempool - %w", err)
269297
}
270298

271-
return cb.Batch, nil
299+
return cb, nil
272300
}
273301

274302
// StoreExecutedBatch - stores an executed batch in one go. This can be done for the sequencer because it is guaranteed
@@ -359,11 +387,11 @@ func (s *sequencer) duplicateBatches(l1Head *types.Block, nonCanonicalL1Path []c
359387
}
360388
sequencerNo = sequencerNo.Add(sequencerNo, big.NewInt(1))
361389
// create the duplicate and store/broadcast it, recreate batch even if it was empty
362-
b, err := s.produceBatch(sequencerNo, l1Head.ParentHash(), currentHead, orphanBatch.Transactions, orphanBatch.Header.Time, false)
390+
cb, err := s.produceBatch(sequencerNo, l1Head.ParentHash(), currentHead, orphanBatch.Transactions, orphanBatch.Header.Time, false)
363391
if err != nil {
364392
return fmt.Errorf("could not produce batch. Cause %w", err)
365393
}
366-
currentHead = b.Hash()
394+
currentHead = cb.Batch.Hash()
367395
s.logger.Info("Duplicated batch", log.BatchHashKey, currentHead)
368396
}
369397

integration/obscuroscan/obscuroscan_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ func TestObscuroscan(t *testing.T) {
8181
statusCode, body, err := fasthttp.Get(nil, fmt.Sprintf("%s/count/contracts/", serverAddress))
8282
assert.NoError(t, err)
8383
assert.Equal(t, 200, statusCode)
84-
assert.Equal(t, "{\"count\":1}", string(body))
84+
assert.Equal(t, "{\"count\":2}", string(body))
8585

8686
statusCode, body, err = fasthttp.Get(nil, fmt.Sprintf("%s/count/transactions/", serverAddress))
8787
assert.NoError(t, err)
8888
assert.Equal(t, 200, statusCode)
89-
assert.Equal(t, "{\"count\":5}", string(body))
89+
assert.Equal(t, "{\"count\":6}", string(body))
9090

9191
statusCode, body, err = fasthttp.Get(nil, fmt.Sprintf("%s/items/batch/latest/", serverAddress))
9292
assert.NoError(t, err)
@@ -120,8 +120,8 @@ func TestObscuroscan(t *testing.T) {
120120
publicTxsObj := publicTxsRes{}
121121
err = json.Unmarshal(body, &publicTxsObj)
122122
assert.NoError(t, err)
123-
assert.Equal(t, 5, len(publicTxsObj.Result.TransactionsData))
124-
assert.Equal(t, uint64(5), publicTxsObj.Result.Total)
123+
assert.Equal(t, 6, len(publicTxsObj.Result.TransactionsData))
124+
assert.Equal(t, uint64(6), publicTxsObj.Result.Total)
125125

126126
statusCode, body, err = fasthttp.Get(nil, fmt.Sprintf("%s/items/batches/?offset=0&size=10", serverAddress))
127127
assert.NoError(t, err)

tools/walletextension/test/utils.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
const jsonID = "1"
3333

34-
func createWalExtCfg(connectPort, wallHTTPPort, wallWSPort int) *config.Config {
34+
func createWalExtCfg(connectPort, wallHTTPPort, wallWSPort int) *config.Config { //nolint: unparam
3535
testDBPath, err := os.CreateTemp("", "")
3636
if err != nil {
3737
panic("could not create persistence file for wallet extension tests")
@@ -61,7 +61,7 @@ func createWalExt(t *testing.T, walExtCfg *config.Config) func() error {
6161
}
6262

6363
// Creates an RPC layer that the wallet extension can connect to. Returns a handle to shut down the host.
64-
func createDummyHost(t *testing.T, wsRPCPort int) (*DummyAPI, func() error) {
64+
func createDummyHost(t *testing.T, wsRPCPort int) (*DummyAPI, func() error) { //nolint: unparam
6565
dummyAPI := NewDummyAPI()
6666
cfg := gethnode.Config{
6767
WSHost: common.Localhost,
@@ -124,7 +124,7 @@ func makeHTTPEthJSONReqWithPath(port int, path string) []byte {
124124
}
125125

126126
// Makes an Ethereum JSON RPC request over HTTP and returns the response body with userID query paremeter.
127-
func makeHTTPEthJSONReqWithUserID(port int, method string, params interface{}, userID string) []byte {
127+
func makeHTTPEthJSONReqWithUserID(port int, method string, params interface{}, userID string) []byte { //nolint: unparam
128128
reqBody := prepareRequestBody(method, params)
129129
return makeRequestHTTP(fmt.Sprintf("http://%s:%d/v1/?u=%s", common.Localhost, port, userID), reqBody)
130130
}

tools/walletextension/test/wallet_extension_test.go

+18-24
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ import (
1818
const (
1919
errFailedDecrypt = "could not decrypt bytes with viewing key"
2020
dummyParams = "dummyParams"
21-
jsonKeyTopics = "topics"
2221
_hostWSPort = integration.StartPortWalletExtensionUnitTest
23-
_testOffset = 100 // offset each test by a multiplier of the offset to avoid port colision. ie: hostPort := _hostWSPort + _testOffset*2
2422
)
2523

2624
type testHelper struct {
@@ -41,14 +39,13 @@ func TestWalletExtension(t *testing.T) {
4139
"canRegisterViewingKeyAndMakeRequestsOverWebsockets": canRegisterViewingKeyAndMakeRequestsOverWebsockets,
4240
} {
4341
t.Run(name, func(t *testing.T) {
44-
hostPort := _hostWSPort + i*_testOffset
45-
dummyAPI, shutDownHost := createDummyHost(t, hostPort)
46-
shutdownWallet := createWalExt(t, createWalExtCfg(hostPort, hostPort+1, hostPort+2))
42+
dummyAPI, shutDownHost := createDummyHost(t, _hostWSPort)
43+
shutdownWallet := createWalExt(t, createWalExtCfg(_hostWSPort, _hostWSPort+1, _hostWSPort+2))
4744

4845
h := &testHelper{
49-
hostPort: hostPort,
50-
walletHTTPPort: hostPort + 1,
51-
walletWSPort: hostPort + 2,
46+
hostPort: _hostWSPort,
47+
walletHTTPPort: _hostWSPort + 1,
48+
walletWSPort: _hostWSPort + 2,
5249
hostAPI: dummyAPI,
5350
}
5451

@@ -173,14 +170,13 @@ func canRegisterViewingKeyAndMakeRequestsOverWebsockets(t *testing.T, testHelper
173170
}
174171

175172
func TestCannotInvokeSensitiveMethodsWithoutViewingKey(t *testing.T) {
176-
hostPort := _hostWSPort + _testOffset*7
177-
walletHTTPPort := hostPort + 1
178-
walletWSPort := hostPort + 2
173+
walletHTTPPort := _hostWSPort + 1
174+
walletWSPort := _hostWSPort + 2
179175

180-
_, shutdownHost := createDummyHost(t, hostPort)
176+
_, shutdownHost := createDummyHost(t, _hostWSPort)
181177
defer shutdownHost() //nolint: errcheck
182178

183-
shutdownWallet := createWalExt(t, createWalExtCfg(hostPort, walletHTTPPort, walletWSPort))
179+
shutdownWallet := createWalExt(t, createWalExtCfg(_hostWSPort, walletHTTPPort, walletWSPort))
184180
defer shutdownWallet() //nolint: errcheck
185181

186182
conn, err := openWSConn(walletWSPort)
@@ -202,13 +198,12 @@ func TestCannotInvokeSensitiveMethodsWithoutViewingKey(t *testing.T) {
202198
}
203199

204200
func TestKeysAreReloadedWhenWalletExtensionRestarts(t *testing.T) {
205-
hostPort := _hostWSPort + _testOffset*8
206-
walletHTTPPort := hostPort + 1
207-
walletWSPort := hostPort + 2
201+
walletHTTPPort := _hostWSPort + 1
202+
walletWSPort := _hostWSPort + 2
208203

209-
dummyAPI, shutdownHost := createDummyHost(t, hostPort)
204+
dummyAPI, shutdownHost := createDummyHost(t, _hostWSPort)
210205
defer shutdownHost() //nolint: errcheck
211-
walExtCfg := createWalExtCfg(hostPort, walletHTTPPort, walletWSPort)
206+
walExtCfg := createWalExtCfg(_hostWSPort, walletHTTPPort, walletWSPort)
212207
shutdownWallet := createWalExt(t, walExtCfg)
213208

214209
addr, viewingKeyBytes, signature := simulateViewingKeyRegister(t, walletHTTPPort, walletWSPort, false)
@@ -278,13 +273,12 @@ func TestKeysAreReloadedWhenWalletExtensionRestarts(t *testing.T) {
278273
//}
279274

280275
func TestGetStorageAtForReturningUserID(t *testing.T) {
281-
hostPort := _hostWSPort + _testOffset*8
282-
walletHTTPPort := hostPort + 1
283-
walletWSPort := hostPort + 2
276+
walletHTTPPort := _hostWSPort + 1
277+
walletWSPort := _hostWSPort + 2
284278

285-
createDummyHost(t, hostPort)
286-
walExtCfg := createWalExtCfg(hostPort, walletHTTPPort, walletWSPort)
287-
createWalExtCfg(hostPort, walletHTTPPort, walletWSPort)
279+
createDummyHost(t, _hostWSPort)
280+
walExtCfg := createWalExtCfg(_hostWSPort, walletHTTPPort, walletWSPort)
281+
createWalExtCfg(_hostWSPort, walletHTTPPort, walletWSPort)
288282
createWalExt(t, walExtCfg)
289283

290284
// create userID

0 commit comments

Comments
 (0)