Skip to content

Commit fdf6b7d

Browse files
committed
Merge branch 'gregor/callbacks/metrics' of https://github.com/devbugging/flow-go into gregor/callbacks/metrics
2 parents 488faba + 6f8f798 commit fdf6b7d

File tree

3 files changed

+354
-14
lines changed

3 files changed

+354
-14
lines changed

fvm/evm/emulator/state/base.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,25 +356,56 @@ func (v *BaseView) PurgeAllSlotsOfAnAccount(addr gethCommon.Address) error {
356356
if err != nil {
357357
return err
358358
}
359-
if acc == nil { // if account doesn't exist return
359+
// if account doesn't exist, return
360+
// len(acc.CollectionID) == 0 means the account is a non smart contract
361+
// account, which has no slots to purge, so we naturally return
362+
if acc == nil || len(acc.CollectionID) == 0 {
360363
return nil
361364
}
362-
col, err := v.collectionProvider.CollectionByID(acc.CollectionID)
365+
366+
// remove storage slots
367+
col, found := v.slots[addr]
368+
if !found {
369+
col, err = v.collectionProvider.CollectionByID(acc.CollectionID)
370+
if err != nil {
371+
return err
372+
}
373+
}
374+
375+
delete(v.slots, addr)
376+
377+
keys := [][]byte{}
378+
keysIterator, err := col.ReadOnlyIterator()
363379
if err != nil {
364380
return err
365381
}
366-
// delete all slots related to this account (eip-6780)
367-
keys, err := col.Destroy()
382+
383+
key, _, err := keysIterator.Next()
368384
if err != nil {
369385
return err
370386
}
371-
delete(v.slots, addr)
372-
for _, key := range keys {
387+
388+
// we need to collect all the keys, before removing them,
389+
// as per the ReadOnlyIterator's specification
390+
for key != nil {
391+
keys = append(keys, key)
373392
delete(v.cachedSlots, types.SlotAddress{
374393
Address: addr,
375394
Key: gethCommon.BytesToHash(key),
376395
})
396+
key, _, err = keysIterator.Next()
397+
if err != nil {
398+
return err
399+
}
377400
}
401+
402+
// remove slot keys from account's collection
403+
for _, key := range keys {
404+
if err = col.Remove(key); err != nil {
405+
return err
406+
}
407+
}
408+
378409
return nil
379410
}
380411

@@ -676,9 +707,6 @@ func (v *BaseView) storeSlot(sk types.SlotAddress, data gethCommon.Hash) error {
676707
if acc == nil {
677708
return fmt.Errorf("slot belongs to a non-existing account")
678709
}
679-
if !acc.HasCode() {
680-
return fmt.Errorf("slot belongs to a non-smart contract account")
681-
}
682710
col, err := v.getSlotCollection(acc)
683711
if err != nil {
684712
return err

fvm/evm/offchain/query/view.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,15 @@ func WithStateOverrideState(
277277
if err != nil {
278278
return err
279279
}
280+
281+
// This forces the account of the slots to be created, otherwise we
282+
// might add slots without its owner account being created.
283+
if err := setupAccount(addr, baseView); err != nil {
284+
return err
285+
}
286+
280287
// purge all the slots
281-
err = baseView.PurgeAllSlotsOfAnAccount(addr)
282-
if err != nil {
288+
if err = baseView.PurgeAllSlotsOfAnAccount(addr); err != nil {
283289
return err
284290
}
285291
// no need to be sorted this is off-chain operation
@@ -307,6 +313,13 @@ func WithStateOverrideStateDiff(
307313
if err != nil {
308314
return err
309315
}
316+
317+
// This forces the account of the slots to be created, otherwise we
318+
// might add slots without its owner account being created.
319+
if err := setupAccount(addr, baseView); err != nil {
320+
return err
321+
}
322+
310323
// no need to be sorted this is off-chain operation
311324
for k, v := range slots {
312325
err = baseView.UpdateSlot(types.SlotAddress{
@@ -344,3 +357,31 @@ func WithExtraPrecompiledContracts(pcs []types.PrecompiledContract) DryCallOptio
344357
return nil
345358
}
346359
}
360+
361+
// setupAccount updates an account's metadata. If the account does not exist,
362+
// it will be created and initialized with the proper default values.
363+
func setupAccount(addr gethCommon.Address, baseView *state.BaseView) error {
364+
balance, err := baseView.GetBalance(addr)
365+
if err != nil {
366+
return err
367+
}
368+
nonce, err := baseView.GetNonce(addr)
369+
if err != nil {
370+
return err
371+
}
372+
code, err := baseView.GetCode(addr)
373+
if err != nil {
374+
return err
375+
}
376+
codeHash := gethTypes.EmptyCodeHash
377+
if len(code) > 0 {
378+
codeHash = gethCrypto.Keccak256Hash(code)
379+
}
380+
381+
err = baseView.UpdateAccount(addr, balance, nonce, code, codeHash)
382+
if err != nil {
383+
return err
384+
}
385+
386+
return nil
387+
}

0 commit comments

Comments
 (0)