Skip to content

Commit 67b12b5

Browse files
authored
Fix incorrect balance when CreateContract is used in block-stm (ethereum#1382)
* Fix incorrect balance when CreateContract is used in block-stm * address CR comments
1 parent d227610 commit 67b12b5

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

core/state/statedb.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ func (s *StateDB) ApplyMVWriteSet(writes []blockstm.WriteDescriptor) {
418418

419419
switch path.GetSubpath() {
420420
case BalancePath:
421-
// todo: @anshalshukla || @cffls - check balance change reason
422421
s.SetBalance(addr, sr.GetBalance(addr), tracing.BalanceChangeUnspecified)
423422
case NoncePath:
424423
s.SetNonce(addr, sr.GetNonce(addr))
@@ -1095,9 +1094,6 @@ func (s *StateDB) createObject(addr common.Address) *stateObject {
10951094
// consensus bug eventually.
10961095
func (s *StateDB) CreateAccount(addr common.Address) {
10971096
s.createObject(addr)
1098-
// todo: @anshalshukla || @cffls
1099-
// Check the below MV Write, balance path change have been removed
1100-
MVWrite(s, blockstm.NewAddressKey(addr))
11011097
}
11021098

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

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

core/state/statedb_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,33 @@ func TestMVHashMapReadWriteDelete(t *testing.T) {
813813
assert.Equal(t, uint256.NewInt(0), b)
814814
}
815815

816+
func TestMVHashMapCreateContract(t *testing.T) {
817+
t.Parallel()
818+
819+
db := NewDatabase(rawdb.NewMemoryDatabase())
820+
mvhm := blockstm.MakeMVHashMap()
821+
s, _ := NewWithMVHashmap(common.Hash{}, db, nil, mvhm)
822+
823+
states := []*StateDB{s}
824+
825+
// Create copies of the original state for each transition
826+
for i := 1; i <= 4; i++ {
827+
sCopy := s.Copy()
828+
sCopy.txIndex = i
829+
states = append(states, sCopy)
830+
}
831+
832+
addr := common.HexToAddress("0x01")
833+
states[0].SetBalance(addr, uint256.NewInt(100), tracing.BalanceChangeTransfer)
834+
states[0].FlushMVWriteSet()
835+
836+
states[1].CreateContract(addr)
837+
states[1].FlushMVWriteSet()
838+
839+
b := states[1].GetBalance(addr)
840+
assert.Equal(t, uint256.NewInt(100), b)
841+
}
842+
816843
func TestMVHashMapRevert(t *testing.T) {
817844
t.Parallel()
818845

0 commit comments

Comments
 (0)