Skip to content

Commit

Permalink
Gas refunding
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlechner committed Jul 29, 2024
1 parent c4d2dce commit 7389f97
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 133 deletions.
126 changes: 126 additions & 0 deletions go/integration_test/processor/gas_billing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2024 Fantom Foundation
//
// Use of this software is governed by the Business Source License included
// in the LICENSE file and at fantom.foundation/bsl11.
//
// Change Date: 2028-4-16
//
// On the date above, in accordance with the Business Source License, use of
// this software will be governed by the GNU Lesser General Public License v3.

package processor

import (
"fmt"
"testing"

"github.com/Fantom-foundation/Tosca/go/tosca"
"github.com/Fantom-foundation/Tosca/go/tosca/vm"
op "github.com/ethereum/go-ethereum/core/vm"
"go.uber.org/mock/gomock"

_ "github.com/Fantom-foundation/Tosca/go/processor/floria" // < registers floria processor for testing
_ "github.com/Fantom-foundation/Tosca/go/processor/opera" // < registers opera processor for testing
)

func TestProcessor_GasBillingEndToEnd(t *testing.T) {
tests := map[string]struct {
after WorldState
receipt tosca.Receipt
result tosca.Result
}{
"success": {
after: WorldState{
{1}: Account{Balance: tosca.NewValue(524000), Nonce: 5},
{2}: Account{Balance: tosca.NewValue(0),
Code: tosca.Code{
byte(vm.PUSH1), byte(0), // < push 0
byte(vm.PUSH1), byte(0), // < push 0
byte(op.RETURN),
},
},
},
receipt: tosca.Receipt{
Success: true,
GasUsed: 95200,
},
result: tosca.Result{
GasLeft: 5000,
Success: true,
GasRefund: 300,
},
},
"failed": {
after: WorldState{
{1}: Account{Balance: tosca.NewValue(522500), Nonce: 5},
{2}: Account{Balance: tosca.NewValue(0),
Code: tosca.Code{
byte(vm.PUSH1), byte(0), // < push 0
byte(vm.PUSH1), byte(0), // < push 0
byte(op.RETURN),
},
},
},
receipt: tosca.Receipt{
Success: false,
GasUsed: 95500,
},
result: tosca.Result{
GasLeft: 5000,
Success: false,
GasRefund: 3000,
},
},
}

ctrl := gomock.NewController(t)
interpreter := tosca.NewMockInterpreter(ctrl)

before := WorldState{
{1}: Account{Balance: tosca.NewValue(1000000), Nonce: 4},
{2}: Account{Balance: tosca.NewValue(0),
Code: tosca.Code{
byte(vm.PUSH1), byte(0), // < push 0
byte(vm.PUSH1), byte(0), // < push 0
byte(op.RETURN),
},
},
}

transaction := tosca.Transaction{
Sender: tosca.Address{1},
Recipient: &tosca.Address{2},
GasLimit: 100000,
GasPrice: tosca.NewValue(5),
Nonce: 4,
}

for name, test := range tests {
for processorName, processor := range processorsWithInterpreter("mockInterpreter", interpreter) {
t.Run(fmt.Sprintf("%s/%s", processorName, name), func(t *testing.T) {

scenario := Scenario{
Before: before,
Transaction: transaction,
After: test.after,
Receipt: test.receipt,
}

interpreter.EXPECT().Run(gomock.Any()).Return(test.result, nil)
scenario.Run(t, processor)
})
}
}

}

func processorsWithInterpreter(name string, interpreter tosca.Interpreter) map[string]tosca.Processor {
factories := tosca.GetAllRegisteredProcessorFactories()
res := map[string]tosca.Processor{}
for processorName, factory := range factories {
processor := factory(interpreter)
res[fmt.Sprintf("%s/%s", processorName, name)] = processor
}

return res
}
Loading

0 comments on commit 7389f97

Please sign in to comment.