Skip to content

Commit

Permalink
Restructure selfdestruct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlechner committed Oct 17, 2024
1 parent 8efca01 commit 7bdc44d
Showing 1 changed file with 54 additions and 119 deletions.
173 changes: 54 additions & 119 deletions go/interpreter/lfvm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,79 +851,6 @@ func TestSelfDestruct_Refund(t *testing.T) {
}
}

func TestSelfDestruct_NewAccountCost(t *testing.T) {
tests := map[string]struct {
beneficiaryEmpty bool
balance tosca.Value
cost tosca.Gas
}{
"account empty no balance": {
beneficiaryEmpty: true,
balance: tosca.Value{},
cost: 0,
},
"account empty with balance": {
beneficiaryEmpty: true,
balance: tosca.Value{1},
cost: 25_000,
},
"account without balance": {
beneficiaryEmpty: false,
balance: tosca.Value{},
cost: 0,
},
"account with balance": {
beneficiaryEmpty: false,
balance: tosca.Value{1},
cost: 0,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
address := tosca.Address{0x01}
beneficiary := tosca.Address{0x02}
ctrl := gomock.NewController(t)
runContext := tosca.NewMockRunContext(ctrl)

runContext.EXPECT().AccessAccount(beneficiary).Return(tosca.WarmAccess)
runContext.EXPECT().GetBalance(beneficiary).Return(tosca.Value{})
runContext.EXPECT().GetCode(beneficiary).Return(nil)
if test.beneficiaryEmpty {
runContext.EXPECT().GetNonce(beneficiary).Return(uint64(0))
} else {
runContext.EXPECT().GetNonce(beneficiary).Return(uint64(1))
}
runContext.EXPECT().GetBalance(address).Return(test.balance)
runContext.EXPECT().SelfDestruct(address, beneficiary).Return(true)

ctxt := context{
params: tosca.Parameters{
BlockParameters: tosca.BlockParameters{
Revision: tosca.R13_Cancun,
},
Recipient: address,
},
stack: NewStack(),
memory: NewMemory(),
context: runContext,
gas: test.cost,
}
ctxt.stack.push(new(uint256.Int).SetBytes(beneficiary[:]))
status, err := opSelfdestruct(&ctxt)
if err != nil {
t.Fatalf("unexpected error, got %v", err)
}
if want, got := statusSelfDestructed, status; want != got {
t.Fatalf("unexpected status, wanted %v, got %v", want, got)
}
if want, got := tosca.Gas(0), ctxt.gas; want != got {
t.Errorf("unexpected gas, wanted %d, got %d", want, got)
}
})
}
}

func TestSelfDestruct_ExistingAccountToNewBeneficiary(t *testing.T) {
// This tests produces the combination of context calls/results for the maximum dynamic gas cost possible.

Expand Down Expand Up @@ -970,35 +897,54 @@ func TestSelfDestruct_ExistingAccountToNewBeneficiary(t *testing.T) {

func TestSelfDestruct_ReturnsSelfDestructedAndConsumesGas(t *testing.T) {
tests := map[string]struct {
balance tosca.Value
beneficiaryNonce uint64
beneficiaryBalance tosca.Value
beneficiaryCode []byte
remainingGas tosca.Gas
}{
"with empty beneficiary account": {
"balance and empty beneficiary account": {
tosca.Value{1},
0,
tosca.Value{},
nil,
tosca.Gas(0),
},
"non empty beneficiary account with nonce": {
"balance and non empty beneficiary account with nonce": {
tosca.Value{1},
1,
tosca.Value{},
nil,
tosca.Gas(25_000),
},
"non empty beneficiary account with Balance": {
"balance and non empty beneficiary account with Balance": {
tosca.Value{1},
0,
tosca.Value{1},
nil,
tosca.Gas(25_000),
},
"non empty beneficiary account with Code": {
"balance and non empty beneficiary account with Code": {
tosca.Value{1},
0,
tosca.Value{},
[]byte{0x01},
tosca.Gas(25_000),
},
"no balance and empty beneficiary": {
tosca.Value{},
0,
tosca.Value{},
nil,
tosca.Gas(25_000),
},
"no balance and non empty beneficiary": {
tosca.Value{},
1,
tosca.Value{1},
nil,
tosca.Gas(25_000),
},
}

for name, test := range tests {
Expand All @@ -1012,7 +958,7 @@ func TestSelfDestruct_ReturnsSelfDestructedAndConsumesGas(t *testing.T) {
runContext.EXPECT().GetBalance(beneficiary).Return(test.beneficiaryBalance)
runContext.EXPECT().GetCode(beneficiary).Return(test.beneficiaryCode).AnyTimes()
runContext.EXPECT().GetNonce(beneficiary).Return(test.beneficiaryNonce).AnyTimes()
runContext.EXPECT().GetBalance(address).Return(tosca.Value{1})
runContext.EXPECT().GetBalance(address).Return(test.balance)
runContext.EXPECT().SelfDestruct(address, beneficiary).Return(true)

ctxt := context{
Expand Down Expand Up @@ -1042,53 +988,42 @@ func TestSelfDestruct_ReturnsSelfDestructedAndConsumesGas(t *testing.T) {
}
}

func TestSelfDestruct_ProperlyReportsNotEnoughGas(t *testing.T) {
func TestSelfDestruct_ReturnsOutOfGasError(t *testing.T) {
for _, beneficiaryAccess := range []tosca.AccessStatus{tosca.WarmAccess, tosca.ColdAccess} {
for _, accountExists := range []bool{true, false} {
t.Run(fmt.Sprintf("beneficiaryAccess:%v_accountExists:%v", beneficiaryAccess, accountExists), func(t *testing.T) {
beneficiaryAddress := tosca.Address{1}
selfAddress := tosca.Address{2}
t.Run(fmt.Sprintf("beneficiaryAccess:%v", beneficiaryAccess), func(t *testing.T) {
beneficiaryAddress := tosca.Address{1}
selfAddress := tosca.Address{2}

ctrl := gomock.NewController(t)
runContext := tosca.NewMockRunContext(ctrl)
runContext.EXPECT().AccessAccount(beneficiaryAddress).Return(beneficiaryAccess)
runContext.EXPECT().GetBalance(beneficiaryAddress).Return(tosca.Value{})
runContext.EXPECT().GetCode(beneficiaryAddress).Return(nil)
if accountExists {
runContext.EXPECT().GetNonce(beneficiaryAddress).Return(uint64(1))
} else {
runContext.EXPECT().GetNonce(beneficiaryAddress).Return(uint64(0))
}
runContext.EXPECT().GetBalance(selfAddress).Return(tosca.Value{1})
ctrl := gomock.NewController(t)
runContext := tosca.NewMockRunContext(ctrl)
runContext.EXPECT().AccessAccount(beneficiaryAddress).Return(beneficiaryAccess)
// Existing beneficiary
runContext.EXPECT().GetBalance(beneficiaryAddress).Return(tosca.Value{1})
runContext.EXPECT().GetBalance(selfAddress).Return(tosca.Value{1})

ctxt := context{
params: tosca.Parameters{
BlockParameters: tosca.BlockParameters{
Revision: tosca.R13_Cancun,
},
Recipient: selfAddress,
ctxt := context{
params: tosca.Parameters{
BlockParameters: tosca.BlockParameters{
Revision: tosca.R13_Cancun,
},
stack: NewStack(),
memory: NewMemory(),
context: runContext,
}
if beneficiaryAccess == tosca.ColdAccess {
ctxt.gas += 2600
}
if !accountExists {
ctxt.gas += 25000
}
ctxt.gas -= 1

ctxt.stack.push(new(uint256.Int).SetBytes(beneficiaryAddress[:]))
Recipient: selfAddress,
},
stack: NewStack(),
memory: NewMemory(),
context: runContext,
}
if beneficiaryAccess == tosca.ColdAccess {
ctxt.gas += 2600
}
ctxt.gas -= 1

_, err := opSelfdestruct(&ctxt)
if err != errOutOfGas {
t.Fatalf("expected out of gas but got %v", err)
}
ctxt.stack.push(new(uint256.Int).SetBytes(beneficiaryAddress[:]))

})
}
_, err := opSelfdestruct(&ctxt)
if err != errOutOfGas {
t.Fatalf("expected out of gas but got %v", err)
}
})
}
}

Expand Down

0 comments on commit 7bdc44d

Please sign in to comment.