Skip to content

Commit

Permalink
Fix incorrect balance when CreateContract is used in block-stm (#1382)
Browse files Browse the repository at this point in the history
* Fix incorrect balance when CreateContract is used in block-stm

* address CR comments
  • Loading branch information
cffls authored Dec 10, 2024
1 parent d227610 commit 67b12b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
9 changes: 3 additions & 6 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ func (s *StateDB) ApplyMVWriteSet(writes []blockstm.WriteDescriptor) {

switch path.GetSubpath() {
case BalancePath:
// todo: @anshalshukla || @cffls - check balance change reason
s.SetBalance(addr, sr.GetBalance(addr), tracing.BalanceChangeUnspecified)
case NoncePath:
s.SetNonce(addr, sr.GetNonce(addr))
Expand Down Expand Up @@ -1095,9 +1094,6 @@ func (s *StateDB) createObject(addr common.Address) *stateObject {
// consensus bug eventually.
func (s *StateDB) CreateAccount(addr common.Address) {
s.createObject(addr)
// todo: @anshalshukla || @cffls
// Check the below MV Write, balance path change have been removed
MVWrite(s, blockstm.NewAddressKey(addr))
}

// CreateContract is used whenever a contract is created. This may be preceded
Expand All @@ -1107,13 +1103,14 @@ func (s *StateDB) CreateAccount(addr common.Address) {
// correctly handle EIP-6780 'delete-in-same-transaction' logic.
func (s *StateDB) CreateContract(addr common.Address) {
obj := s.getStateObject(addr)
if obj != nil {
obj = s.mvRecordWritten(obj)
}
if !obj.newContract {
obj.newContract = true
s.journal.append(createContractChange{account: addr})
}

// todo: @anshalshukla || @cffls
// Check the below MV Write, balance path change have been removed
MVWrite(s, blockstm.NewAddressKey(addr))
}

Expand Down
27 changes: 27 additions & 0 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,33 @@ func TestMVHashMapReadWriteDelete(t *testing.T) {
assert.Equal(t, uint256.NewInt(0), b)
}

func TestMVHashMapCreateContract(t *testing.T) {
t.Parallel()

db := NewDatabase(rawdb.NewMemoryDatabase())
mvhm := blockstm.MakeMVHashMap()
s, _ := NewWithMVHashmap(common.Hash{}, db, nil, mvhm)

states := []*StateDB{s}

// Create copies of the original state for each transition
for i := 1; i <= 4; i++ {
sCopy := s.Copy()
sCopy.txIndex = i
states = append(states, sCopy)
}

addr := common.HexToAddress("0x01")
states[0].SetBalance(addr, uint256.NewInt(100), tracing.BalanceChangeTransfer)
states[0].FlushMVWriteSet()

states[1].CreateContract(addr)
states[1].FlushMVWriteSet()

b := states[1].GetBalance(addr)
assert.Equal(t, uint256.NewInt(100), b)
}

func TestMVHashMapRevert(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 67b12b5

Please sign in to comment.