2020-01-20 12:31:12 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-04-07 09:41:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-04-21 14:45:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-01-20 12:31:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These tests are taken from C# code
|
|
|
|
// https://github.com/neo-project/neo/blob/master-2.x/neo.UnitTests/UT_InteropPrices.cs#L245
|
|
|
|
func TestGetPrice(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
2020-02-29 14:53:28 +00:00
|
|
|
defer bc.Close()
|
2020-04-07 09:41:12 +00:00
|
|
|
sdao := dao.NewSimple(storage.NewMemoryStore())
|
2020-04-03 06:49:01 +00:00
|
|
|
systemInterop := bc.newInteropContext(trigger.Application, sdao, nil, nil)
|
2020-01-20 12:31:12 +00:00
|
|
|
|
2020-04-08 10:29:15 +00:00
|
|
|
v := SpawnVM(systemInterop)
|
2020-01-20 12:31:12 +00:00
|
|
|
v.SetPriceGetter(getPrice)
|
|
|
|
|
|
|
|
t.Run("System.Storage.Put", func(t *testing.T) {
|
|
|
|
// System.Storage.Put: e63f1884 (requires push key and value)
|
2020-04-21 14:45:34 +00:00
|
|
|
v.Load([]byte{byte(opcode.PUSH3), byte(opcode.PUSH3), byte(opcode.PUSH0),
|
|
|
|
byte(opcode.SYSCALL), 0xe6, 0x3f, 0x18, 0x84})
|
2020-01-20 12:31:12 +00:00
|
|
|
require.NoError(t, v.StepInto()) // push 03 (length 1)
|
|
|
|
require.NoError(t, v.StepInto()) // push 03 (length 1)
|
|
|
|
require.NoError(t, v.StepInto()) // push 00
|
|
|
|
|
|
|
|
checkGas(t, util.Fixed8FromInt64(1), v)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("System.Storage.PutEx", func(t *testing.T) {
|
|
|
|
// System.Storage.PutEx: 73e19b3a (requires push key and value)
|
2020-04-21 14:45:34 +00:00
|
|
|
v.Load([]byte{byte(opcode.PUSH3), byte(opcode.PUSH3), byte(opcode.PUSH0),
|
|
|
|
byte(opcode.SYSCALL), 0x73, 0xe1, 0x9b, 0x3a})
|
2020-01-20 12:31:12 +00:00
|
|
|
require.NoError(t, v.StepInto()) // push 03 (length 1)
|
|
|
|
require.NoError(t, v.StepInto()) // push 03 (length 1)
|
|
|
|
require.NoError(t, v.StepInto()) // push 00
|
|
|
|
|
|
|
|
checkGas(t, util.Fixed8FromInt64(1), v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkGas(t *testing.T, expected util.Fixed8, v *vm.VM) {
|
|
|
|
op, par, err := v.Context().Next()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, getPrice(v, op, par))
|
|
|
|
}
|