Skip to content

Commit

Permalink
dedup call to ChunkifyCode, same as replay branch (ethereum#156)
Browse files Browse the repository at this point in the history
* dedup call to ChunkifyCode, same as replay branch

* fix some linter issues
  • Loading branch information
gballet authored Feb 5, 2023
1 parent 1e77f02 commit fc011dd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
18 changes: 5 additions & 13 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,19 +523,11 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
s.setError(fmt.Errorf("updateStateObject (%x) error: %w", addr[:], err))
}
if s.trie.IsVerkle() {
if len(obj.code) > 0 {
cs := make([]byte, 32)
binary.LittleEndian.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 obj.dirtyCode {
chunks := trie.ChunkifyCode(obj.code)
for i := 0; i < len(chunks); i += 32 {
s.trie.TryUpdate(trieUtils.GetTreeKeyCodeChunkWithEvaluatedAddress(obj.pointEval, uint256.NewInt(uint64(i)/32)), chunks[i:i+32])
}
}
cs := make([]byte, 32)
binary.LittleEndian.PutUint64(cs, uint64(len(obj.code)))
key := trieUtils.GetTreeKeyCodeSize(addr[:])
if err := s.trie.TryUpdate(key, cs); err != nil {
s.setError(fmt.Errorf("updateStateObject (%x) error: %w", addr[:], err))
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/types/access_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type VerkleStem [31]byte

// Mode specifies how a tree location has been accessed
// for the byte value:
// the first bit is set if the branch has been edited
// the second bit is set if the branch has been read
// * the first bit is set if the branch has been edited
// * the second bit is set if the branch has been read
type Mode byte

const (
Expand Down
4 changes: 0 additions & 4 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,6 @@ func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
return nil, nil
}

func touchEachChunksOnReadAndChargeGasWithAddress(offset, size uint64, contract *Contract, code []byte, accesses *types.AccessWitness, deployment bool) uint64 {
return touchEachChunksOnReadAndChargeGas(offset, size, contract, code, accesses, deployment)
}

// touchChunkOnReadAndChargeGas is a helper function to touch every chunk in a code range and charge witness gas costs
func touchChunkOnReadAndChargeGas(chunks trie.ChunkedCode, offset uint64, evals [][]byte, code []byte, accesses *types.AccessWitness, deployment bool) uint64 {
// note that in the case where the executed code is outside the range of
Expand Down
21 changes: 14 additions & 7 deletions miner/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type testWorkerBackend struct {
uncleBlock *types.Block
}

func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int) *testWorkerBackend {
func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int, isVerkle bool) *testWorkerBackend {
var gspec = &core.Genesis{
Config: chainConfig,
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
Expand Down Expand Up @@ -146,10 +146,17 @@ func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine
t.Fatalf("failed to insert origin chain: %v", err)
}
parent := chain.GetBlockByHash(chain.CurrentBlock().ParentHash())
blocks, _ = core.GenerateChain(chainConfig, parent, engine, genDb, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testUserAddress)
})
uncle = blocks[0]
if isVerkle {
blocks, _, _, _ = core.GenerateVerkleChain(chainConfig, parent, engine, genDb, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testUserAddress)
})
uncle = blocks[0]
} else {
blocks, _ = core.GenerateChain(chainConfig, parent, engine, genDb, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testUserAddress)
})
uncle = blocks[0]
}
} else {
_, blocks, _ := core.GenerateChainWithGenesis(gspec, engine, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testUserAddress)
Expand Down Expand Up @@ -196,7 +203,7 @@ func (b *testWorkerBackend) newRandomVerkleUncle() *types.Block {
} else {
parent = b.chain.GetBlockByHash(b.chain.CurrentBlock().ParentHash())
}
blocks, _ := core.GenerateVerkleChain(b.chain.Config(), parent, b.chain.Engine(), b.db, 1, func(i int, gen *core.BlockGen) {
blocks, _, _, _ := core.GenerateVerkleChain(b.chain.Config(), parent, b.chain.Engine(), b.db, 1, func(i int, gen *core.BlockGen) {
var addr = make([]byte, common.AddressLength)
rand.Read(addr)
gen.SetCoinbase(common.BytesToAddress(addr))
Expand Down Expand Up @@ -245,7 +252,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
chainConfig = *params.AllEthashProtocolChanges
engine = ethash.NewFaker()
}
w, b := newTestWorker(t, &chainConfig, engine, db, 0)
w, b := newTestWorker(t, &chainConfig, engine, db, 0, false)
defer w.close()

// This test chain imports the mined blocks.
Expand Down

0 comments on commit fc011dd

Please sign in to comment.