diff --git a/core/state_processor_test.go b/core/state_processor_test.go index bab1009e77f1..d90d63c32e31 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -381,7 +381,7 @@ func TestProcessStateless(t *testing.T) { genesis := gspec.MustCommit(db) blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil) defer blockchain.Stop() - txCost1 := params.WitnessBranchWriteCost*2 + params.WitnessBranchReadCost*2 + params.WitnessChunkWriteCost*3 + params.WitnessChunkReadCost*12 + params.TxGas + txCost1 := params.WitnessBranchWriteCost*2 + params.WitnessBranchReadCost*2 + params.WitnessChunkWriteCost*3 + params.WitnessChunkReadCost*10 + params.TxGas txCost2 := params.WitnessBranchWriteCost + params.WitnessBranchReadCost*2 + params.WitnessChunkWriteCost*2 + params.WitnessChunkReadCost*10 + params.TxGas blockGasUsedExpected := txCost1*2 + txCost2 chain, _ := GenerateVerkleChain(gspec.Config, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) { diff --git a/core/types/access_witness.go b/core/types/access_witness.go index d7b9f85602da..2f68defa41a6 100644 --- a/core/types/access_witness.go +++ b/core/types/access_witness.go @@ -67,10 +67,11 @@ func (aw *AccessWitness) SetLeafValue(addr []byte, value []byte) { var stem [31]byte copy(stem[:], addr[:31]) - if chunk, exists := aw.Chunks[common.BytesToHash(addr)]; exists { + if chunk, exists := aw.Chunks[common.BytesToHash(addr)]; exists && len(chunk.value) == 0 { + // overwrite nil chunk.value = value aw.Chunks[common.BytesToHash(addr)] = chunk - } else { + } else if !exists { panic(fmt.Sprintf("address not in access witness: %x", addr)) } } @@ -113,9 +114,9 @@ func (aw *AccessWitness) touchAddressOnWrite(addr []byte) (bool, bool, bool) { // true if the stem or the stub weren't arleady present. func (aw *AccessWitness) touchAddress(addr []byte, isWrite bool) (bool, bool, bool, bool, bool) { var ( - stem [31]byte - stemRead bool - selectorRead bool + stem [31]byte + stemRead, selectorRead bool + stemWrite, selectorWrite, chunkFill bool ) copy(stem[:], addr[:31]) @@ -125,18 +126,15 @@ func (aw *AccessWitness) touchAddress(addr []byte, isWrite bool) (bool, bool, bo aw.Branches[stem] = AccessWitnessReadFlag } - selectorRead = true - // Check for the presence of the leaf selector if _, hasSelector := aw.Chunks[common.BytesToHash(addr)]; !hasSelector { + selectorRead = true aw.Chunks[common.BytesToHash(addr)] = ChunkValue{ AccessWitnessReadFlag, nil, } } - var stemWrite, selectorWrite, chunkFill bool - if isWrite { stemWrite, selectorWrite, chunkFill = aw.touchAddressOnWrite(addr) }