From 71cbe3954c0b7110ea9322adf2676dc03409843d Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 6 Dec 2024 09:36:58 -0800 Subject: [PATCH 1/2] Fix incorrect balance when CreateContract is used in block-stm --- core/state/statedb.go | 3 +++ core/state/statedb_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/core/state/statedb.go b/core/state/statedb.go index e477651906..6ad2b13ee1 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1107,6 +1107,9 @@ 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}) diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index a514f7c9f4..9fe1cd9995 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -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() From 4969964fb3692837111073bfae5206e6152e21d1 Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 9 Dec 2024 11:26:36 -0800 Subject: [PATCH 2/2] address CR comments --- core/state/statedb.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 6ad2b13ee1..2ea0662ce0 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -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)) @@ -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 @@ -1115,8 +1111,6 @@ func (s *StateDB) CreateContract(addr common.Address) { 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)) }