Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Create object if it doesn't exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
miha-stopar committed Sep 21, 2023
1 parent 9c29bf4 commit d58d770
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 3 additions & 3 deletions mpt-witness-generator/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,14 @@ func (s *StateDB) setStateObject(object *stateObject) {
func (s *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
stateObject := s.getStateObject(addr)
if stateObject == nil {
stateObject, _ = s.createObject(addr)
stateObject, _ = s.CreateObject(addr)
}
return stateObject
}

// createObject creates a new state object. If there is an existing account with
// the given address, it is overwritten and returned as the second return value.
func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
func (s *StateDB) CreateObject(addr common.Address) (newobj, prev *stateObject) {
prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!

var prevdestruct bool
Expand Down Expand Up @@ -664,7 +664,7 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject)
//
// Carrying over the balance ensures that Ether doesn't disappear.
func (s *StateDB) CreateAccount(addr common.Address) {
newObj, prev := s.createObject(addr)
newObj, prev := s.CreateObject(addr)
if prev != nil {
newObj.setBalance(prev.data.Balance)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,6 @@ func TestNeighbourNodeInHashedBranch(t *testing.T) {
}

func TestLongKey(t *testing.T) {
// Currently, it results into leaf RLP constrain failure.
blockNum := 2000069
blockNumberParent := big.NewInt(int64(blockNum))
blockHeaderParent := oracle.PrefetchBlock(blockNumberParent, true, nil)
Expand All @@ -2265,4 +2264,29 @@ func TestLongKey(t *testing.T) {
trieModifications := []TrieModification{trieMod1}

prepareWitness("LongKey", trieModifications, statedb)
}

func TestTrieDoesNotExist(t *testing.T) {
blockNum := 2000003
blockNumberParent := big.NewInt(int64(blockNum))
blockHeaderParent := oracle.PrefetchBlock(blockNumberParent, true, nil)
database := state.NewDatabase(blockHeaderParent)
statedb, _ := state.New(blockHeaderParent.Root, database, nil)
addr := common.HexToAddress("0xcaac46d9bd68bffb533320545a90cd92c6e98e58")

statedb.DisableLoadingRemoteAccounts()

key1 := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
val1 := common.BigToHash(big.NewInt(int64(17)))

trieMod1 := TrieModification{
Type: StorageChanged,
Key: key1,
Value: val1,
Address: addr,
}

trieModifications := []TrieModification{trieMod1}

prepareWitness("StorageTrieDoesNotExist", trieModifications, statedb)
}
5 changes: 5 additions & 0 deletions mpt-witness-generator/witness/prepare_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st

accountProof, aNeighbourNode1, aExtNibbles1, aIsLastLeaf1, aIsNeighbourNodeHashed1, err := statedb.GetProof(addr)
check(err)

if !statedb.Exist(addr) {
statedb.CreateObject(addr)
}

storageProof, neighbourNode1, extNibbles1, isLastLeaf1, isNeighbourNodeHashed1, err := statedb.GetStorageProof(addr, tMod.Key)
check(err)

Expand Down

0 comments on commit d58d770

Please sign in to comment.