Skip to content

Commit

Permalink
fix rebase issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Sep 30, 2021
1 parent af32764 commit be9f9e9
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 45 deletions.
4 changes: 2 additions & 2 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (s *stateObject) updateTrie(db Database) Trie {
var v []byte
if (value == common.Hash{}) {
if tr.IsVerkle() {
k := trieUtils.GetTreeKeyStorageSlot(s.address, new(uint256.Int).SetBytes(key[:]))
k := trieUtils.GetTreeKeyStorageSlot(s.address[:], new(uint256.Int).SetBytes(key[:]))
s.setError(tr.TryDelete(k))
//s.db.db.TrieDB().DiskDB().Delete(append(s.address[:], key[:]...))
} else {
Expand All @@ -371,7 +371,7 @@ func (s *stateObject) updateTrie(db Database) Trie {
if !tr.IsVerkle() {
s.setError(tr.TryUpdate(key[:], v))
} else {
k := trieUtils.GetTreeKeyStorageSlot(s.address, new(uint256.Int).SetBytes(key[:]))
k := trieUtils.GetTreeKeyStorageSlot(s.address[:], new(uint256.Int).SetBytes(key[:]))
// Update the trie, with v as a value
s.setError(tr.TryUpdate(k, v))
}
Expand Down
29 changes: 18 additions & 11 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,13 @@ func (s *StateDB) updateStatelessStateObject(obj *stateObject) {
ch common.Hash
)

versionKey := common.BytesToHash(trieUtils.GetTreeKeyVersion(addr))
versionKey := common.BytesToHash(trieUtils.GetTreeKeyVersion(addr[:]))
if v, ok = s.stateless[versionKey]; ok {
nonceKey := common.BytesToHash(trieUtils.GetTreeKeyNonce(addr))
nonceKey := common.BytesToHash(trieUtils.GetTreeKeyNonce(addr[:]))
if n, ok = s.stateless[nonceKey]; ok {
balanceKey := common.BytesToHash(trieUtils.GetTreeKeyBalance(addr))
balanceKey := common.BytesToHash(trieUtils.GetTreeKeyBalance(addr[:]))
if b, ok = s.stateless[balanceKey]; ok {
codeHashKey := common.BytesToHash(trieUtils.GetTreeKeyCodeKeccak(addr))
codeHashKey := common.BytesToHash(trieUtils.GetTreeKeyCodeKeccak(addr[:]))
if _, ok = s.stateless[codeHashKey]; ok {
v[0] = byte(0)
binary.BigEndian.PutUint64(n[:], obj.data.Nonce)
Expand Down Expand Up @@ -526,7 +526,14 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
}

if err := s.trie.TryUpdateAccount(addr[:], &obj.data); err != nil {
s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
s.setError(fmt.Errorf("updateStateObject (%x) error: %w", addr[:], err))
}
if len(obj.code) > 0 && s.trie.IsVerkle() {
cs := make([]byte, 32)
binary.BigEndian.PutUint64(cs, uint64(len(obj.code)))
if err := s.trie.TryUpdate(trieUtils.GetTreeKeyCodeSize(addr[:]), cs); err != nil {
s.setError(fmt.Errorf("updateStateObject (%x) error: %w", addr[:], err))
}
}

// If state snapshotting is active, cache the data til commit. Note, this
Expand Down Expand Up @@ -562,7 +569,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) {
}
} else {
for i := byte(0); i <= 255; i++ {
if err := s.trie.TryDelete(trieUtils.GetTreeKeyAccountLeaf(obj.Address(), i)); err != nil {
if err := s.trie.TryDelete(trieUtils.GetTreeKeyAccountLeaf(obj.Address().Bytes(), i)); err != nil {
s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", obj.Address(), err))
}
}
Expand All @@ -582,7 +589,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
func (s *StateDB) getStatelessDeletedStateObject(addr common.Address) *stateObject {
// Check that it is present in the witness, if running
// in stateless execution mode.
chunk := trieUtils.GetTreeKeyNonce(addr)
chunk := trieUtils.GetTreeKeyNonce(addr[:])
nb, ok := s.stateless[common.BytesToHash(chunk)]
if !ok {
log.Error("Failed to decode state object", "addr", addr)
Expand All @@ -591,7 +598,7 @@ func (s *StateDB) getStatelessDeletedStateObject(addr common.Address) *stateObje
panic("inivalid chunk")
return nil
}
chunk = trieUtils.GetTreeKeyBalance(addr)
chunk = trieUtils.GetTreeKeyBalance(addr[:])
bb, ok := s.stateless[common.BytesToHash(chunk)]
if !ok {
log.Error("Failed to decode state object", "addr", addr)
Expand All @@ -600,7 +607,7 @@ func (s *StateDB) getStatelessDeletedStateObject(addr common.Address) *stateObje
panic("inivalid chunk")
return nil
}
chunk = trieUtils.GetTreeKeyCodeKeccak(addr)
chunk = trieUtils.GetTreeKeyCodeKeccak(addr[:])
cb, ok := s.stateless[common.BytesToHash(chunk)]
if !ok {
// Assume that this is an externally-owned account, and that
Expand All @@ -610,7 +617,7 @@ func (s *StateDB) getStatelessDeletedStateObject(addr common.Address) *stateObje
// account code.
copy(cb[:], emptyCodeHash)
}
data := &Account{
data := &types.StateAccount{
Nonce: binary.BigEndian.Uint64(nb[:8]),
Balance: big.NewInt(0).SetBytes(bb[:]),
CodeHash: cb[:],
Expand Down Expand Up @@ -1082,7 +1089,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
if s.trie.IsVerkle() {
if chunks, err := trie.ChunkifyCode(addr, obj.code); err == nil {
for i, chunk := range chunks {
s.trie.TryUpdate(trieUtils.GetTreeKeyCodeChunk(addr, uint256.NewInt(uint64(i))), chunk[:])
s.trie.TryUpdate(trieUtils.GetTreeKeyCodeChunk(addr[:], uint256.NewInt(uint64(i))), chunk[:])
}
} else {
s.setError(err)
Expand Down
8 changes: 4 additions & 4 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,20 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
}
if st.evm.TxContext.Accesses != nil {
if msg.To() != nil {
toBalance := trieUtils.GetTreeKeyBalance(*msg.To())
toBalance := trieUtils.GetTreeKeyBalance(msg.To().Bytes())
pre := st.state.GetBalance(*msg.To())
gas += st.evm.TxContext.Accesses.TouchAddressAndChargeGas(toBalance, pre.Bytes())

// NOTE: Nonce also needs to be charged, because it is needed for execution
// on the statless side.
var preTN [8]byte
fromNonce := trieUtils.GetTreeKeyNonce(*msg.To())
fromNonce := trieUtils.GetTreeKeyNonce(msg.To().Bytes())
binary.BigEndian.PutUint64(preTN[:], st.state.GetNonce(*msg.To()))
gas += st.evm.TxContext.Accesses.TouchAddressAndChargeGas(fromNonce, preTN[:])
}
fromBalance := trieUtils.GetTreeKeyBalance(msg.From())
fromBalance := trieUtils.GetTreeKeyBalance(msg.From().Bytes())
preFB := st.state.GetBalance(msg.From()).Bytes()
fromNonce := trieUtils.GetTreeKeyNonce(msg.From())
fromNonce := trieUtils.GetTreeKeyNonce(msg.From().Bytes())
var preFN [8]byte
binary.BigEndian.PutUint64(preFN[:], st.state.GetNonce(msg.From()))
gas += st.evm.TxContext.Accesses.TouchAddressAndChargeGas(fromNonce, preFN[:])
Expand Down
10 changes: 5 additions & 5 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func memoryCopierGas(stackpos int) gasFunc {
func gasExtCodeSize(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
usedGas := uint64(0)
slot := stack.Back(0)
index := trieUtils.GetTreeKeyCodeSize(common.Address(slot.Bytes20()))
index := trieUtils.GetTreeKeyCodeSize(slot.Bytes())
// FIXME(@gballet) need to get the exact code size when executing the operation,
// the value passed here is invalid.
if evm.accesses != nil {
Expand Down Expand Up @@ -131,7 +131,7 @@ func gasCodeCopy(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memory
for ; chunk < endChunk; chunk++ {

// TODO make a version of GetTreeKeyCodeChunk without the bigint
index := trieUtils.GetTreeKeyCodeChunk(addr, uint256.NewInt(chunk))
index := trieUtils.GetTreeKeyCodeChunk(addr[:], uint256.NewInt(chunk))
// FIXME(@gballet) invalid code chunk, the jumpdest is missing
statelessGas += evm.TxContext.Accesses.TouchAddressAndChargeGas(index, code[chunk*31:chunk*31+31])
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func gasExtCodeCopy(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem
// XXX uint64 overflow in condition check
for ; chunk < endChunk; chunk++ {
// TODO(@gballet) make a version of GetTreeKeyCodeChunk without the bigint
index := trieUtils.GetTreeKeyCodeChunk(addr, uint256.NewInt(chunk))
index := trieUtils.GetTreeKeyCodeChunk(addr[:], uint256.NewInt(chunk))
statelessGas += evm.TxContext.Accesses.TouchAddressAndChargeGas(index, code[31*chunk:31*(chunk+1)])
}

Expand All @@ -180,7 +180,7 @@ func gasSLoad(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySiz
if evm.accesses != nil {
where := stack.Back(0)
addr := contract.Address()
index := trieUtils.GetTreeKeyStorageSlot(addr, where)
index := trieUtils.GetTreeKeyStorageSlot(addr[:], where)
// FIXME(@gballet) invalid value, got to read it from the DB.
usedGas += evm.TxContext.Accesses.TouchAddressAndChargeGas(index, index)
}
Expand Down Expand Up @@ -432,7 +432,7 @@ func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize
if evm.accesses != nil {
// Charge witness costs
for i := trieUtils.VersionLeafKey; i <= trieUtils.CodeSizeLeafKey; i++ {
index := trieUtils.GetTreeKeyAccountLeaf(address, byte(i))
index := trieUtils.GetTreeKeyAccountLeaf(address[:], byte(i))
// FIXME(@gballet) invalid loaded value - need to introduce a
// TouchAccount function.
gas += evm.TxContext.Accesses.TouchAddressAndChargeGas(index, index)
Expand Down
2 changes: 1 addition & 1 deletion core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func copyCodeFromAccesses(addr common.Address, codeOffset, codeEnd, memOffset ui
end = codeEnd % 31
}
// TODO make a version of GetTreeKeyCodeChunk without the bigint
index := common.BytesToHash(trieUtils.GetTreeKeyCodeChunk(addr, uint256.NewInt(chunk)))
index := common.BytesToHash(trieUtils.GetTreeKeyCodeChunk(addr[:], uint256.NewInt(chunk)))
h := in.evm.accesses[index]
scope.Memory.Set(memOffset+offset, end-start, h[1+start:1+end])
offset += end - start
Expand Down
2 changes: 1 addition & 1 deletion core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
inWitness := false
var codePage common.Hash
if in.evm.ChainConfig().UseVerkle {
index := trieUtils.GetTreeKeyCodeChunk(contract.Address(), uint256.NewInt(pc/31))
index := trieUtils.GetTreeKeyCodeChunk(contract.Address().Bytes(), uint256.NewInt(pc/31))
// FIXME(@gballet) this is only valid when not executing in stateless mode
contract.Gas -= in.evm.TxContext.Accesses.TouchAddressAndChargeGas(index, contract.Code[pc&0x1F:pc&0x1F+31])

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqG
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/gballet/go-verkle v0.0.0-20210923190028-da1602155380 h1:taRGq+n2tWrf/Daz9T7hU2L9b6GxPayhE714cBtHExw=
github.com/gballet/go-verkle v0.0.0-20210923190028-da1602155380/go.mod h1:HkmCsQaSenO8/T4Vyb+KjkjnFT5nJIhqLVPMAsZhPZc=
github.com/gballet/go-verkle v0.0.0-20210929123607-12e8d78bd33a h1:Le1Al9qBmeyU8NeiRnYumMCX80+lPe/MDU310ejT6ik=
github.com/gballet/go-verkle v0.0.0-20210929123607-12e8d78bd33a/go.mod h1:HkmCsQaSenO8/T4Vyb+KjkjnFT5nJIhqLVPMAsZhPZc=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down
21 changes: 10 additions & 11 deletions trie/utils/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package utils
import (
"crypto/sha256"

"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)

Expand All @@ -40,9 +39,9 @@ var (
codeStorageDelta = uint256.NewInt(0).Sub(HeaderStorageOffset, CodeOffset)
)

func GetTreeKey(address common.Address, treeIndex *uint256.Int, subIndex byte) []byte {
func GetTreeKey(address []byte, treeIndex *uint256.Int, subIndex byte) []byte {
digest := sha256.New()
digest.Write(address[:])
digest.Write(address)
treeIndexBytes := treeIndex.Bytes()
var payload [32]byte
copy(payload[:len(treeIndexBytes)], treeIndexBytes)
Expand All @@ -52,31 +51,31 @@ func GetTreeKey(address common.Address, treeIndex *uint256.Int, subIndex byte) [
return h
}

func GetTreeKeyAccountLeaf(address common.Address, leaf byte) []byte {
func GetTreeKeyAccountLeaf(address []byte, leaf byte) []byte {
return GetTreeKey(address, zero, leaf)
}

func GetTreeKeyVersion(address common.Address) []byte {
func GetTreeKeyVersion(address []byte) []byte {
return GetTreeKey(address, zero, VersionLeafKey)
}

func GetTreeKeyBalance(address common.Address) []byte {
func GetTreeKeyBalance(address []byte) []byte {
return GetTreeKey(address, zero, BalanceLeafKey)
}

func GetTreeKeyNonce(address common.Address) []byte {
func GetTreeKeyNonce(address []byte) []byte {
return GetTreeKey(address, zero, NonceLeafKey)
}

func GetTreeKeyCodeKeccak(address common.Address) []byte {
func GetTreeKeyCodeKeccak(address []byte) []byte {
return GetTreeKey(address, zero, CodeKeccakLeafKey)
}

func GetTreeKeyCodeSize(address common.Address) []byte {
func GetTreeKeyCodeSize(address []byte) []byte {
return GetTreeKey(address, zero, CodeSizeLeafKey)
}

func GetTreeKeyCodeChunk(address common.Address, chunk *uint256.Int) []byte {
func GetTreeKeyCodeChunk(address []byte, chunk *uint256.Int) []byte {
chunkOffset := new(uint256.Int).Add(CodeOffset, chunk)
treeIndex := new(uint256.Int).Div(chunkOffset, VerkleNodeWidth)
subIndexMod := new(uint256.Int).Mod(chunkOffset, VerkleNodeWidth).Bytes()
Expand All @@ -87,7 +86,7 @@ func GetTreeKeyCodeChunk(address common.Address, chunk *uint256.Int) []byte {
return GetTreeKey(address, treeIndex, subIndex)
}

func GetTreeKeyStorageSlot(address common.Address, storageKey *uint256.Int) []byte {
func GetTreeKeyStorageSlot(address []byte, storageKey *uint256.Int) []byte {
treeIndex := storageKey.Clone()
if storageKey.Cmp(codeStorageDelta) < 0 {
treeIndex.Add(HeaderStorageOffset, storageKey)
Expand Down
12 changes: 2 additions & 10 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package trie

import (
"encoding/binary"
"errors"
"fmt"
"encoding/binary"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/utils"
Expand Down Expand Up @@ -78,16 +77,9 @@ func (t *VerkleTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error
if err = t.TryUpdate(utils.GetTreeKeyBalance(key), acc.Balance.Bytes()); err != nil {
return fmt.Errorf("updateStateObject (%x) error: %v", key, err)
}
if err = t.TryUpdate(utils.GetTreeKeyCodeKeccak(key), acc.CodeHash()); err != nil {
if err = t.TryUpdate(utils.GetTreeKeyCodeKeccak(key), acc.CodeHash); err != nil {
return fmt.Errorf("updateStateObject (%x) error: %v", key, err)
}
if len(acc.Code) > 0 {
cs := make([]byte, 32)
binary.BigEndian.PutUint64(cs, uint64(len(obj.code)))
if err = t.TryUpdate(utils.GetTreeKeyCodeSize(key), cs); err != nil {
return fmt.Errorf("updateStateObject (%x) error: %v", key, err)
}
}

return nil
}
Expand Down

0 comments on commit be9f9e9

Please sign in to comment.