Skip to content

Commit 7774ca6

Browse files
committed
Address comments
1 parent 7710bc6 commit 7774ca6

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

consensus/system_contract/consensus.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"math/big"
9-
"sort"
109
"time"
1110

1211
"github.com/scroll-tech/go-ethereum/accounts"
@@ -176,8 +175,10 @@ func (s *SystemContract) verifyCascadingFields(chain consensus.ChainHeaderReader
176175
}
177176

178177
if err := s.correctSigner(header); err != nil {
179-
return err
178+
return fmt.Errorf("block %d: signer check failed: %w", header.Number.Uint64(), err)
179+
180180
}
181+
181182
return nil
182183
}
183184

@@ -201,20 +202,16 @@ func (s *SystemContract) correctSigner(header *types.Header) error {
201202
}
202203

203204
func (s *SystemContract) findSignerSlice(blockNum uint64) (common.Address, error) {
204-
// Acquire read-lock before accessing s.signerAddressesL1
205205
s.lock.RLock()
206206
defer s.lock.RUnlock()
207-
transitions := s.signerAddressesL1
208-
idx := sort.Search(len(transitions), func(i int) bool {
209-
// We want the first transition with StartBlock > blockNum
210-
return transitions[i].StartBlock > blockNum
211-
})
212-
// idx is now the insertion point for 'blockNum'
213-
// so the real match is at idx-1 if idx > 0
214-
if idx == 0 {
215-
return common.Address{}, errors.New("no signer for block")
216-
}
217-
return transitions[idx-1].Signer, nil
207+
208+
// Assuming s.signerAddressesL1 is sorted in descending order (largest StartBlock first)
209+
for _, t := range s.signerAddressesL1 {
210+
if blockNum >= t.StartBlock {
211+
return t.Signer, nil
212+
}
213+
}
214+
return common.Address{}, errors.New("no signer for block")
218215
}
219216

220217
// VerifyUncles implements consensus.Engine, always returning an error for any

consensus/system_contract/system_contract.go

+33-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
const (
20-
defaultSyncInterval = 10
20+
defaultSyncInterval = 10 * time.Second
2121
)
2222

2323
type SignerAddressL1 struct {
@@ -155,18 +155,46 @@ func (s *SystemContract) Start() {
155155
case <-s.ctx.Done():
156156
return
157157
case <-syncTicker.C:
158-
s.lock.Lock()
159-
signers, err := s.fetchAllSigners(nil) // nil -> latest state
158+
// Fetch the latest signers from L1 outside of the lock
159+
newSigners, err := s.fetchAllSigners(nil)
160160
if err != nil {
161-
log.Error("failed to get signer address from L1 System Contract", "err", err)
161+
log.Error("failed to fetch signer addresses from L1 System Contract", "err", err)
162+
continue
163+
}
164+
165+
// Optionally, compare with the currently stored signers first.
166+
s.lock.RLock()
167+
currentSigners := s.signerAddressesL1
168+
s.lock.RUnlock()
169+
if !signersChanged(currentSigners, newSigners) {
170+
// No change—no need to reacquire the write lock.
171+
continue
162172
}
163-
s.signerAddressesL1 = signers
173+
174+
s.lock.Lock()
175+
s.signerAddressesL1 = newSigners
164176
s.lock.Unlock()
177+
log.Info("Refreshed signer addresses", "count", len(newSigners))
165178
}
166179
}
167180
}()
168181
}
169182

183+
// signersChanged compares two slices of SignerAddressL1 and returns true if they differ.
184+
// For simplicity, we compare lengths and each element in order.
185+
func signersChanged(current, new []SignerAddressL1) bool {
186+
if len(current) != len(new) {
187+
return true
188+
}
189+
for i, cs := range current {
190+
ns := new[i]
191+
if cs.StartBlock != ns.StartBlock || cs.Signer != ns.Signer {
192+
return true
193+
}
194+
}
195+
return false
196+
}
197+
170198
// Close implements consensus.Engine.
171199
func (s *SystemContract) Close() error {
172200
s.cancel()

0 commit comments

Comments
 (0)