Skip to content

Commit 3c2347e

Browse files
committed
validate that the read address from L1 is not empty and improved error handling when fetching address
1 parent 3839bf7 commit 3c2347e

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

consensus/system_contract/system_contract.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package system_contract
22

33
import (
44
"context"
5+
"fmt"
56
"sync"
67
"time"
78

@@ -34,19 +35,19 @@ type SystemContract struct {
3435
// signers set to the ones provided by the user.
3536
func New(ctx context.Context, config *params.SystemContractConfig, client sync_service.EthClient) *SystemContract {
3637
ctx, cancel := context.WithCancel(ctx)
37-
address, err := client.StorageAt(ctx, config.SystemContractAddress, config.SystemContractSlot, nil)
38-
if err != nil {
39-
log.Error("failed to get signer address from L1 System Contract", "err", err)
40-
}
4138

42-
return &SystemContract{
43-
config: config,
44-
client: client,
45-
signerAddressL1: common.BytesToAddress(address),
39+
s := &SystemContract{
40+
config: config,
41+
client: client,
4642

4743
ctx: ctx,
4844
cancel: cancel,
4945
}
46+
47+
if err := s.fetchAddressFromL1(); err != nil {
48+
log.Error("failed to fetch signer address from L1", "err", err)
49+
}
50+
return s
5051
}
5152

5253
// Authorize injects a private key into the consensus engine to mint new blocks
@@ -75,20 +76,27 @@ func (s *SystemContract) Start() {
7576
case <-s.ctx.Done():
7677
return
7778
case <-syncTicker.C:
78-
s.fetchAddressFromL1()
79+
if err := s.fetchAddressFromL1(); err != nil {
80+
log.Error("failed to fetch signer address from L1", "err", err)
81+
}
7982
}
8083
}
8184
}()
8285
}
8386

84-
func (s *SystemContract) fetchAddressFromL1() {
87+
func (s *SystemContract) fetchAddressFromL1() error {
8588
address, err := s.client.StorageAt(s.ctx, s.config.SystemContractAddress, s.config.SystemContractSlot, nil)
8689
if err != nil {
87-
log.Error("failed to get signer address from L1 System Contract", "err", err)
90+
return fmt.Errorf("failed to get signer address from L1 System Contract: %w", err)
8891
}
8992
bAddress := common.BytesToAddress(address)
9093

91-
log.Info("Read address from system contract", "address", bAddress.Hex())
94+
// Validate the address is not empty
95+
if bAddress == (common.Address{}) {
96+
return fmt.Errorf("retrieved empty signer address from L1 System Contract")
97+
}
98+
99+
log.Debug("Read address from system contract", "address", bAddress.Hex())
92100

93101
s.lock.RLock()
94102
addressChanged := s.signerAddressL1 != bAddress
@@ -99,6 +107,8 @@ func (s *SystemContract) fetchAddressFromL1() {
99107
s.signerAddressL1 = bAddress
100108
s.lock.Unlock()
101109
}
110+
111+
return nil
102112
}
103113

104114
// Close implements consensus.Engine.

consensus/system_contract/system_contract_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestSystemContract_FetchSigner(t *testing.T) {
4141
sys := New(ctx, config, fakeClient)
4242
defer sys.Close()
4343

44-
sys.fetchAddressFromL1()
44+
require.NoError(t, sys.fetchAddressFromL1())
4545

4646
actualSigner := sys.currentSignerAddressL1()
4747

@@ -67,7 +67,7 @@ func TestSystemContract_AuthorizeCheck(t *testing.T) {
6767
sys := New(ctx, config, fakeClient)
6868
defer sys.Close()
6969

70-
sys.fetchAddressFromL1()
70+
require.NoError(t, sys.fetchAddressFromL1())
7171

7272
// Authorize with a different signer than expected.
7373
differentSigner := common.HexToAddress("0xABCDEFabcdefABCDEFabcdefabcdefABCDEFABCD")
@@ -122,7 +122,7 @@ func TestSystemContract_SignsAfterUpdate(t *testing.T) {
122122
sys := New(ctx, config, fakeClient)
123123
defer sys.Close()
124124

125-
sys.fetchAddressFromL1()
125+
require.NoError(t, sys.fetchAddressFromL1())
126126

127127
// Verify that initially the fetched signer equals oldSigner.
128128
initialSigner := sys.currentSignerAddressL1()
@@ -134,7 +134,7 @@ func TestSystemContract_SignsAfterUpdate(t *testing.T) {
134134
fakeClient.mu.Unlock()
135135

136136
// fetch new value from L1 (simulating a background poll)
137-
sys.fetchAddressFromL1()
137+
require.NoError(t, sys.fetchAddressFromL1())
138138

139139
// Verify that system contract's signerAddressL1 is now updated to updatedSigner.
140140
newSigner := sys.currentSignerAddressL1()

0 commit comments

Comments
 (0)