add slot uniqueness check and test for EIP-7928

This commit is contained in:
Sahil-4555 2026-03-05 18:40:26 +05:30
parent 344ce84a43
commit c840b954d3
2 changed files with 36 additions and 0 deletions

View file

@ -159,6 +159,19 @@ func (e *AccountAccess) validate() error {
return errors.New("storage read slots not in lexicographic order")
}
// EIP-7928: a slot must not appear in both storage_changes and storage_reads.
if len(e.StorageWrites) > 0 && len(e.StorageReads) > 0 {
changeSlots := make(map[common.Hash]struct{}, len(e.StorageWrites))
for _, sc := range e.StorageWrites {
changeSlots[sc.Slot] = struct{}{}
}
for _, key := range e.StorageReads {
if _, exists := changeSlots[key]; exists {
return fmt.Errorf("storage key %s in both changes and reads", key)
}
}
}
// Check the balance changes are sorted in order
if !slices.IsSortedFunc(e.BalanceChanges, func(a, b encodingBalanceChange) int {
return cmp.Compare[uint16](a.TxIdx, b.TxIdx)

View file

@ -252,3 +252,26 @@ func TestBlockAccessListValidation(t *testing.T) {
t.Fatalf("Unexpected validation error: %v", err)
}
}
func TestBlockAccessListSlotUniqueness(t *testing.T) {
var addr common.Address
addr[19] = 0x01
slot := common.HexToHash("0x01")
ac := AccountAccess{
Address: addr,
StorageWrites: []encodingSlotWrites{
{
Slot: slot,
Accesses: []encodingStorageWrite{
{TxIdx: 0, ValueAfter: [32]byte{1}},
},
},
},
StorageReads: [][32]byte{slot},
}
bal := BlockAccessList{[]AccountAccess{ac}}
if err := bal.Validate(); err == nil {
t.Fatal("expected error for slot in both changes and reads")
}
}