Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect balance when CreateContract is used in block-stm #1382

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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))
}

27 changes: 27 additions & 0 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
@@ -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()