Skip to content

Commit

Permalink
Cover instructions with unit tests (#64)
Browse files Browse the repository at this point in the history
* some tests added

* more unit tests

* lint fix

* lint again

* more tests

* lint fix

* test fix

* lint fix

* more tests

* better code, lint fix

* more tests

* lint fix

* more cases

* Rename opMload to opMLoad

* comments fix

* mstore test fix

* less code

* lint fix

* lint

* nolint fix

* more lint fix

* forcetypeassertfix

* typeassert

* lint

* comments fix

* more comment fix

* missed cast of mockHost

* Remove evm reference from the state

* Minor changes

* Remove TestReleaseAcquireBig as it is tested with TestExp and TestSignExtension

* Fix TestBalance

---------

Co-authored-by: Stefan Negovanović <[email protected]>
  • Loading branch information
dusannosovic-ethernal and Stefan-Ethernal authored Jan 12, 2024
1 parent f4176d4 commit 0d68f22
Show file tree
Hide file tree
Showing 7 changed files with 1,347 additions and 95 deletions.
2 changes: 1 addition & 1 deletion state/runtime/evm/dispatch_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func init() {
register(RETURN, handler{inst: opHalt(RETURN), stack: 2, gas: 0})

// memory
register(MLOAD, handler{inst: opMload, stack: 1, gas: 3})
register(MLOAD, handler{inst: opMLoad, stack: 1, gas: 3})
register(MSTORE, handler{inst: opMStore, stack: 2, gas: 3})
register(MSTORE8, handler{inst: opMStore8, stack: 2, gas: 3})

Expand Down
1 change: 0 additions & 1 deletion state/runtime/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (e *EVM) Run(c *runtime.Contract, host runtime.Host, config *chain.ForksInT

contract.msg = c
contract.code = c.Code
contract.evm = e
contract.gas = c.Gas
contract.host = host
contract.config = config
Expand Down
43 changes: 32 additions & 11 deletions state/runtime/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygon/polygon-edge/state/runtime/tracer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func newMockContract(value *big.Int, gas uint64, code []byte) *runtime.Contract {
Expand All @@ -26,6 +27,8 @@ func newMockContract(value *big.Int, gas uint64, code []byte) *runtime.Contract
// mockHost is a struct which meets the requirements of runtime.Host interface but throws panic in each methods
// we don't test all opcodes in this test
type mockHost struct {
mock.Mock

tracer runtime.VMTracer
}

Expand All @@ -34,7 +37,9 @@ func (m *mockHost) AccountExists(addr types.Address) bool {
}

func (m *mockHost) GetStorage(addr types.Address, key types.Hash) types.Hash {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called()

return args.Get(0).(types.Hash) //nolint:forcetypeassert
}

func (m *mockHost) SetState(
Expand All @@ -51,51 +56,67 @@ func (m *mockHost) SetStorage(
value types.Hash,
config *chain.ForksInTime,
) runtime.StorageStatus {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called()

return args.Get(0).(runtime.StorageStatus) //nolint:forcetypeassert
}

func (m *mockHost) SetNonPayable(bool) {
panic("Not implemented in tests") //nolint:gocritic
}

func (m *mockHost) GetBalance(addr types.Address) *big.Int {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called(addr)

return args.Get(0).(*big.Int) //nolint:forcetypeassert
}

func (m *mockHost) GetCodeSize(addr types.Address) int {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called(addr)

return args.Int(0)
}

func (m *mockHost) GetCodeHash(addr types.Address) types.Hash {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called(addr)

return types.StringToHash(args.String(0))
}

func (m *mockHost) GetCode(addr types.Address) []byte {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called(addr)

return types.StringToBytes(args.String(0))
}

func (m *mockHost) Selfdestruct(addr types.Address, beneficiary types.Address) {
panic("Not implemented in tests") //nolint:gocritic
m.Called()
}

func (m *mockHost) GetTxContext() runtime.TxContext {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called()

return args.Get(0).(runtime.TxContext) //nolint:forcetypeassert
}

func (m *mockHost) GetBlockHash(number int64) types.Hash {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called(number)

return args.Get(0).(types.Hash) //nolint:forcetypeassert
}

func (m *mockHost) EmitLog(addr types.Address, topics []types.Hash, data []byte) {
panic("Not implemented in tests") //nolint:gocritic
m.Called()
}

func (m *mockHost) Callx(*runtime.Contract, runtime.Host) *runtime.ExecutionResult {
panic("Not implemented in tests") //nolint:gocritic
}

func (m *mockHost) Empty(addr types.Address) bool {
panic("Not implemented in tests") //nolint:gocritic
args := m.Called(addr)

return args.Bool(0)
}

func (m *mockHost) GetNonce(addr types.Address) uint64 {
Expand Down
2 changes: 1 addition & 1 deletion state/runtime/evm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ var bufPool = sync.Pool{
},
}

func opMload(c *state) {
func opMLoad(c *state) {
offset := c.pop()

var ok bool
Expand Down
Loading

0 comments on commit 0d68f22

Please sign in to comment.