Skip to content

Commit

Permalink
upgrade go-verkle to CoW version and get TestProcessVerkle to build (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Nov 29, 2022
1 parent 40585c2 commit 0f327e5
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 263 deletions.
4 changes: 0 additions & 4 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
if config.IsCancun(header.Number) {
uncleCoinbase := utils.GetTreeKeyBalance(uncle.Coinbase.Bytes())
state.Witness().TouchAddressOnReadAndComputeGas(uncleCoinbase)
state.Witness().SetLeafValue(uncleCoinbase, state.GetBalance(uncle.Coinbase).Bytes())
}
state.AddBalance(uncle.Coinbase, r)

Expand All @@ -687,9 +686,6 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header
state.Witness().TouchAddressOnReadAndComputeGas(coinbase)
coinbase[31] = utils.CodeKeccakLeafKey // mark code keccak
state.Witness().TouchAddressOnReadAndComputeGas(coinbase)
balance := state.GetBalance(header.Coinbase)
coinbase[31] = utils.BalanceLeafKey
state.Witness().SetLeafValue(coinbase, balance.Bytes())
}
state.AddBalance(header.Coinbase, reward)
}
33 changes: 16 additions & 17 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/gballet/go-verkle"
)

// BlockGen creates blocks for testing.
Expand Down Expand Up @@ -297,23 +298,26 @@ func GenerateChainWithGenesis(genesis *Genesis, engine consensus.Engine, n int,
panic(err)
}
if genesis.Config != nil && genesis.Config.IsCancun(genesis.ToBlock().Number()) {
blocks, receipts := GenerateVerkleChain(genesis.Config, genesis.ToBlock(), engine, db, n, gen)
blocks, receipts, _, _ := GenerateVerkleChain(genesis.Config, genesis.ToBlock(), engine, db, n, gen)
return db, blocks, receipts
}
blocks, receipts := GenerateChain(genesis.Config, genesis.ToBlock(), engine, db, n, gen)
return db, blocks, receipts
}

func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine consensus.Engine, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine consensus.Engine, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts, [][]byte, [][]verkle.KeyValuePair) {
if config == nil {
config = params.TestChainConfig
}
proofs := make([][]byte, 0, n)
keyvals := make([][]verkle.KeyValuePair, 0, n)
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
chainreader := &fakeChainReader{config: config}
genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) {
b := &BlockGen{i: i, chain: blocks, parent: parent, statedb: statedb, config: config, engine: engine}
b.header = makeHeader(chainreader, parent, statedb, b.engine)
preState := statedb.Copy()
fmt.Println("prestate", preState.GetTrie().(*trie.VerkleTrie).ToDot())

// Mutate the state and block according to any hard-fork specs
if daoBlock := config.DAOForkBlock; daoBlock != nil {
Expand Down Expand Up @@ -358,28 +362,23 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
// building the proof. Ultimately, node
// resolution can be done with a prefetcher
// or from GetCommitmentsAlongPath.
kvs := statedb.Witness().KeyVals()
kvs := make(map[string][]byte)
keys := statedb.Witness().Keys()
for _, key := range keys {
_, err := vtr.TryGet(key)
v, err := vtr.TryGet(key)
if err != nil {
panic(err)
}

// Sanity check: ensure all flagged addresses have an associated
// value: keys is built from Chunks and kvs from InitialValue.
if _, exists := kvs[string(key)]; !exists {
panic(fmt.Sprintf("address not in access witness: %x", key))
}
}

// sanity check: ensure all values correspond to a flagged key by
// comparing the lengths of both structures: they should be equal
if len(kvs) != len(keys) {
panic("keys without a value in witness")
kvs[string(key)] = v
}

vtr.Hash()
p, k, err := vtr.ProveAndSerialize(statedb.Witness().Keys(), kvs)
if err != nil {
panic(err)
}
proofs = append(proofs, p)
keyvals = append(keyvals, k)
return block, b.receipts
}
return nil, nil
Expand All @@ -396,7 +395,7 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
parent = block
snaps = statedb.Snaps()
}
return blocks, receipts
return blocks, receipts, proofs, keyvals
}

func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header {
Expand Down
58 changes: 29 additions & 29 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,32 +210,32 @@ func TestGenesis_Commit(t *testing.T) {
}
}

func TestReadWriteGenesisAlloc(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
alloc = &GenesisAlloc{
{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
{2}: {Balance: big.NewInt(2), Storage: map[common.Hash]common.Hash{{2}: {2}}},
}
hash, _ = alloc.deriveHash()
)
alloc.flush(db)

var reload GenesisAlloc
err := reload.UnmarshalJSON(rawdb.ReadGenesisStateSpec(db, hash))
if err != nil {
t.Fatalf("Failed to load genesis state %v", err)
}
if len(reload) != len(*alloc) {
t.Fatal("Unexpected genesis allocation")
}
for addr, account := range reload {
want, ok := (*alloc)[addr]
if !ok {
t.Fatal("Account is not found")
}
if !reflect.DeepEqual(want, account) {
t.Fatal("Unexpected account")
}
}
}
// func TestReadWriteGenesisAlloc(t *testing.T) {
// var (
// db = rawdb.NewMemoryDatabase()
// alloc = &GenesisAlloc{
// {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
// {2}: {Balance: big.NewInt(2), Storage: map[common.Hash]common.Hash{{2}: {2}}},
// }
// hash, _ = alloc.deriveHash()
// )
// alloc.flush(db)

// var reload GenesisAlloc
// err := reload.UnmarshalJSON(rawdb.ReadGenesisStateSpec(db, hash))
// if err != nil {
// t.Fatalf("Failed to load genesis state %v", err)
// }
// if len(reload) != len(*alloc) {
// t.Fatal("Unexpected genesis allocation")
// }
// for addr, account := range reload {
// want, ok := (*alloc)[addr]
// if !ok {
// t.Fatal("Account is not found")
// }
// if !reflect.DeepEqual(want, account) {
// t.Fatal("Unexpected account")
// }
// }
// }
49 changes: 0 additions & 49 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package state

import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math/big"
Expand Down Expand Up @@ -107,24 +106,6 @@ func (s *stateObject) empty() bool {

// newObject creates a state object.
func newObject(db *StateDB, address common.Address, data types.StateAccount) *stateObject {
if db.trie.IsVerkle() {
var nonce, balance, version []byte

// preserve nil as a balance value, it means it's not in the tree
// use is as a heuristic for the nonce being null as well
if data.Balance != nil {
nonce = make([]byte, 32)
balance = make([]byte, 32)
version = make([]byte, 32)
for i, b := range data.Balance.Bytes() {
balance[len(data.Balance.Bytes())-1-i] = b
}

binary.LittleEndian.PutUint64(nonce[:8], data.Nonce)
}
db.witness.SetGetObjectTouchedLeaves(address.Bytes(), version, balance[:], nonce[:], data.CodeHash)
}

if data.Balance == nil {
data.Balance = new(big.Int)
}
Expand Down Expand Up @@ -272,20 +253,6 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
value.SetBytes(content)
}

// Capture the initial value of the location in the verkle proof witness
if s.db.GetTrie().IsVerkle() {
if err != nil {
return common.Hash{}
}
loc := new(uint256.Int).SetBytes(key[:])
index := trieUtils.GetTreeKeyStorageSlotWithEvaluatedAddress(s.pointEval, loc)
if len(enc) > 0 {
s.db.Witness().SetLeafValue(index, value.Bytes())
} else {
s.db.Witness().SetLeafValue(index, nil)
}
}

s.originStorage[key] = value
return value
}
Expand Down Expand Up @@ -525,21 +492,12 @@ func (s *stateObject) Code(db Database) []byte {
return s.code
}
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
if s.db.GetTrie().IsVerkle() {
// Mark the code size and code hash as empty
s.db.witness.SetObjectCodeTouchedLeaves(s.address.Bytes(), nil, nil)
}
return nil
}
code, err := db.ContractCode(s.addrHash, common.BytesToHash(s.CodeHash()))
if err != nil {
s.setError(fmt.Errorf("can't load code hash %x: %v", s.CodeHash(), err))
}
if s.db.GetTrie().IsVerkle() {
var cs [32]byte
binary.LittleEndian.PutUint64(cs[:8], uint64(len(code)))
s.db.witness.SetObjectCodeTouchedLeaves(s.address.Bytes(), cs[:], s.CodeHash())
}
s.code = code
return code
}
Expand All @@ -553,20 +511,13 @@ func (s *stateObject) CodeSize(db Database) int {
}
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
if s.db.trie.IsVerkle() {
var sz [32]byte
s.db.witness.SetLeafValuesMessageCall(s.address.Bytes(), sz[:])
}
return 0
}
size, err := db.ContractCodeSize(s.addrHash, common.BytesToHash(s.CodeHash()))
if err != nil {
s.setError(fmt.Errorf("can't load code size %x: %v", s.CodeHash(), err))
}
if s.db.trie.IsVerkle() {
var sz [32]byte
binary.LittleEndian.PutUint64(sz[:8], uint64(size))
s.db.witness.SetLeafValuesMessageCall(s.address.Bytes(), sz[:])
}
return size
}

Expand Down
16 changes: 11 additions & 5 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,11 @@ func TestProcessVerkle(t *testing.T) {
signer = types.LatestSigner(config)
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
db = rawdb.NewMemoryDatabase()
coinbase = common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7")
gspec = &Genesis{
Config: config,
Alloc: GenesisAlloc{
common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{
coinbase: GenesisAccount{
Balance: big.NewInt(1000000000000000000), // 1 ether
Nonce: 0,
},
Expand All @@ -386,7 +387,7 @@ func TestProcessVerkle(t *testing.T) {
// Verkle trees use the snapshot, which must be enabled before the
// data is saved into the tree+database.
genesis := gspec.MustCommit(db)
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
blockchain, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
defer blockchain.Stop()

code := common.FromHex(`6060604052600a8060106000396000f360606040526008565b00`)
Expand All @@ -398,7 +399,7 @@ func TestProcessVerkle(t *testing.T) {
txCost1*2 + txCost2,
txCost1*2 + txCost2 + contractCreationCost + codeWithExtCodeCopyGas,
}
chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
chain, _, proofs, keyvals := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
// TODO need to check that the tx cost provided is the exact amount used (no remaining left-over)
tx, _ := types.SignTx(types.NewTransaction(uint64(i)*3, common.Address{byte(i), 2, 3}, big.NewInt(999), txCost1, big.NewInt(875000000), nil), signer, testKey)
gen.AddTx(tx)
Expand All @@ -424,8 +425,13 @@ func TestProcessVerkle(t *testing.T) {
//rlp.Encode(&buf, chain[1])
//f.Write(buf.Bytes())
//fmt.Printf("root= %x\n", chain[0].Root())
// check the proof for the last block
err := trie.DeserializeAndVerifyVerkleProof(proofs[1], chain[0].Root().Bytes(), keyvals[1])
if err != nil {
t.Fatal(err)
}

_, err := blockchain.InsertChain(chain)
_, err = blockchain.InsertChain(chain)
if err != nil {
t.Fatalf("block imported with error: %v", err)
}
Expand All @@ -436,7 +442,7 @@ func TestProcessVerkle(t *testing.T) {
t.Fatalf("expected block %d to be present in chain", i+1)
}
if b.GasUsed() != blockGasUsagesExpected[i] {
t.Fatalf("expected block txs to use %d, got %d\n", blockGasUsagesExpected[i], b.GasUsed())
t.Fatalf("expected block #%d txs to use %d, got %d\n", i+1, blockGasUsagesExpected[i], b.GasUsed())
}
}
}
5 changes: 0 additions & 5 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,14 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gas, gas)
}
if st.evm.ChainConfig().IsCancun(st.evm.Context.BlockNumber) {
var originBalance, originNonceBytes []byte

targetAddr := msg.To()
originAddr := msg.From()

statelessGasOrigin := st.evm.Accesses.TouchTxOriginAndComputeGas(originAddr.Bytes())
if !tryConsumeGas(&st.gas, statelessGasOrigin) {
return nil, fmt.Errorf("%w: Insufficient funds to cover witness access costs for transaction: have %d, want %d", ErrInsufficientBalanceWitness, st.gas, gas)
}
originBalance = st.evm.StateDB.GetBalanceLittleEndian(originAddr)
originNonce := st.evm.StateDB.GetNonce(originAddr)
originNonceBytes = st.evm.StateDB.GetNonceLittleEndian(originAddr)
st.evm.Accesses.SetTxOriginTouchedLeaves(originAddr.Bytes(), originBalance, originNonceBytes, st.evm.StateDB.GetCodeSize(originAddr))

if msg.To() != nil {
statelessGasDest := st.evm.Accesses.TouchTxExistingAndComputeGas(targetAddr.Bytes(), msg.Value().Sign() != 0)
Expand Down
Loading

0 comments on commit 0f327e5

Please sign in to comment.