Skip to content

Commit

Permalink
fixes from replaying nyota costs (#472)
Browse files Browse the repository at this point in the history
* fixes from replaying nyota costs

Signed-off-by: Guillaume Ballet <[email protected]>

* add left-out conversion file

* fix balance endianness + set code size in UpdateAccount

* add leftover endianness change

* remove code cleanup leftover

* use 5 as the code size offset

this matches the spec, even if we have to add a weird -1 here and there

* save buffer creation for PutUint32

---------

Signed-off-by: Guillaume Ballet <[email protected]>
  • Loading branch information
gballet committed Aug 23, 2024
1 parent 063552e commit edabe0f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 41 deletions.
13 changes: 4 additions & 9 deletions core/overlay/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,23 @@ func (kvm *keyValueMigrator) addAccount(addr []byte, acc *types.StateAccount) {
leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex))

var basicData [verkle.LeafValueSize]byte
basicData[utils.BasicDataVersionOffset] = 0 // version
basicData[utils.BasicDataVersionOffset] = 0
binary.BigEndian.PutUint64(basicData[utils.BasicDataNonceOffset:], acc.Nonce)

// get the lower 16 bytes of water and change its endianness
balanceBytes := acc.Balance.Bytes()
for i := 0; i < 16 && i < len(balanceBytes); i++ {
basicData[utils.BasicDataBalanceOffset+i] = balanceBytes[len(balanceBytes)-1-i]
}
binary.LittleEndian.PutUint64(basicData[utils.BasicDataNonceOffset:], acc.Nonce)
copy(basicData[32-len(balanceBytes):], balanceBytes[:])

leafNodeData.Values[utils.BasicDataLeafKey] = basicData[:]
leafNodeData.Values[utils.CodeHashLeafKey] = acc.CodeHash[:]
}

// addAccountCode needs to be called AFTER addAccount, as it will reuse the leaf
// that was created in there.
func (kvm *keyValueMigrator) addAccountCode(addr []byte, codeSize uint64, chunks []byte) {
leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex))

// Save the code size.
var cs [8]byte
binary.LittleEndian.PutUint64(cs[:], codeSize)
copy(leafNodeData.Values[utils.BasicDataLeafKey][utils.BasicDataCodeSizeOffset:utils.BasicDataNonceOffset], cs[:3])
binary.BigEndian.PutUint32(leafNodeData.Values[utils.BasicDataLeafKey][utils.BasicDataCodeSizeOffset-1:utils.BasicDataNonceOffset], uint32(codeSize))

// The first 128 chunks are stored in the account header leaf.
for i := 0; i < 128 && i < len(chunks)/32; i++ {
Expand Down
37 changes: 5 additions & 32 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error
}

if len(values[utils.BasicDataLeafKey]) > 0 {
acc.Nonce = binary.LittleEndian.Uint64(values[utils.BasicDataLeafKey][utils.BasicDataNonceOffset:])
acc.Nonce = binary.BigEndian.Uint64(values[utils.BasicDataLeafKey][utils.BasicDataNonceOffset:])
}

// if the account has been deleted, then values[10] will be 0 and not nil. If it has
Expand All @@ -142,11 +142,8 @@ func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error
return nil, nil
}
}
var balance [32]byte
copy(balance[16:], values[utils.BasicDataLeafKey][utils.BasicDataBalanceOffset:])
for i := 0; i < 8; i++ {
balance[31-i], balance[utils.BasicDataBalanceOffset+i] = balance[utils.BasicDataBalanceOffset+i], balance[31-i]
}
var balance [16]byte
copy(balance[:], values[utils.BasicDataLeafKey][utils.BasicDataBalanceOffset:])
acc.Balance = new(big.Int).SetBytes(balance[:])
acc.CodeHash = values[utils.CodeHashLeafKey]

Expand All @@ -163,16 +160,11 @@ func (t *VerkleTrie) UpdateAccount(addr common.Address, acc *types.StateAccount,
stem = t.pointCache.GetTreeKeyBasicDataCached(addr[:])
)

binary.BigEndian.PutUint32(basicData[utils.BasicDataCodeSizeOffset-1:], uint32(codeLen))
binary.BigEndian.PutUint64(basicData[utils.BasicDataNonceOffset:], acc.Nonce)
// get the lower 16 bytes of water and change its endianness
balanceBytes := acc.Balance.Bytes()
for i := 0; i < 16 && i < len(balanceBytes); i++ {
basicData[utils.BasicDataBalanceOffset+i] = balanceBytes[i]
}
// var cs [8]byte
// binary.BigEndian.PutUint64(cs[:], uint64(codeLen))
// copy(basicData[utils.BasicDataCodeSizeOffset:], cs[:3])

copy(basicData[32-len(balanceBytes):], balanceBytes[:])
values[utils.BasicDataLeafKey] = basicData[:]
values[utils.CodeHashLeafKey] = acc.CodeHash[:]

Expand Down Expand Up @@ -455,25 +447,6 @@ func (t *VerkleTrie) UpdateContractCode(addr common.Address, codeHash common.Has
}
values[groupOffset] = chunks[i : i+32]

// Reuse the calculated key to also update the code size.
if i == 0 {
var basicDataKey [32]byte
copy(basicDataKey[:], key[:31])
basicDataKey[31] = utils.BasicDataLeafKey
// XXX add subfield update api
basicdata, err := t.root.Get(basicDataKey[:], nil)
if err != nil {
return fmt.Errorf("UpdateContractCode (addr=%x) error getting basic data leaf: %w", addr[:], err)
}
if len(basicdata) == 0 {
return fmt.Errorf("UpdateContractCode (addr=%x) error getting non-zero basic data leaf: %w", addr[:], err)
}
cs := make([]byte, 32)
copy(cs[:], basicdata)
binary.BigEndian.PutUint32(cs[utils.BasicDataCodeSizeOffset-1:], uint32(len(code)))
values[utils.BasicDataLeafKey] = cs
}

if groupOffset == 255 || len(chunks)-i <= 32 {
err = t.UpdateStem(key[:31], values)

Expand Down

0 comments on commit edabe0f

Please sign in to comment.