Skip to content

Commit

Permalink
fix: witness gas costs being charged more than once (#70)
Browse files Browse the repository at this point in the history
* fix: witness gas costs being charged more than one

* update test case

Co-authored-by: Jared Wasinger <j-wasinger@hotmail.com>
  • Loading branch information
gballet and jwasinger authored Feb 3, 2022
1 parent 7f5f975 commit c82b074
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 7 additions & 9 deletions core/types/access_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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])

Expand All @@ -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)
}
Expand Down

0 comments on commit c82b074

Please sign in to comment.