2019-11-28 16:06:09 +00:00
|
|
|
package state
|
2019-09-30 16:52:16 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-13 15:26:35 +00:00
|
|
|
"math"
|
2019-09-30 16:52:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2021-01-13 12:34:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-09-30 16:52:16 +00:00
|
|
|
)
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
func TestContractStateToFromSI(t *testing.T) {
|
2019-09-30 16:52:16 +00:00
|
|
|
script := []byte("testscript")
|
|
|
|
|
2020-06-09 09:12:56 +00:00
|
|
|
h := hash.Hash160(script)
|
2020-11-18 09:43:51 +00:00
|
|
|
m := manifest.NewManifest("Test")
|
2020-06-09 09:12:56 +00:00
|
|
|
m.ABI.Methods = []manifest.Method{{
|
|
|
|
Name: "main",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
{
|
|
|
|
Name: "amount",
|
|
|
|
Type: smartcontract.IntegerType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hash",
|
|
|
|
Type: smartcontract.Hash160Type,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.BoolType,
|
|
|
|
}}
|
2019-11-28 16:06:09 +00:00
|
|
|
contract := &Contract{
|
2020-11-18 20:10:48 +00:00
|
|
|
UpdateCounter: 42,
|
2021-02-09 09:02:38 +00:00
|
|
|
ContractBase: ContractBase{
|
|
|
|
ID: 123,
|
|
|
|
Hash: h,
|
|
|
|
NEF: nef.File{
|
|
|
|
Header: nef.Header{
|
|
|
|
Magic: nef.Magic,
|
|
|
|
Compiler: "neo-go.test-test",
|
|
|
|
},
|
|
|
|
Tokens: []nef.MethodToken{},
|
|
|
|
Script: script,
|
|
|
|
Checksum: 0,
|
2021-01-13 12:34:10 +00:00
|
|
|
},
|
2021-02-09 09:02:38 +00:00
|
|
|
Manifest: *m,
|
2021-01-13 12:34:10 +00:00
|
|
|
},
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
2021-01-13 12:34:10 +00:00
|
|
|
contract.NEF.Checksum = contract.NEF.CalculateChecksum()
|
2019-09-30 16:52:16 +00:00
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
t.Run("Convertible", func(t *testing.T) {
|
2020-06-09 09:12:56 +00:00
|
|
|
contractDecoded := new(Contract)
|
2021-07-17 15:37:33 +00:00
|
|
|
testserdes.ToFromStackItem(t, contract, contractDecoded)
|
2020-06-09 09:12:56 +00:00
|
|
|
})
|
|
|
|
t.Run("JSON", func(t *testing.T) {
|
|
|
|
contractDecoded := new(Contract)
|
|
|
|
testserdes.MarshalUnmarshalJSON(t, contract, contractDecoded)
|
|
|
|
})
|
2019-10-10 14:56:58 +00:00
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
|
|
|
|
func TestCreateContractHash(t *testing.T) {
|
2021-01-22 09:22:48 +00:00
|
|
|
var neff = nef.File{
|
|
|
|
Header: nef.Header{
|
|
|
|
Compiler: "test",
|
|
|
|
Magic: nef.Magic,
|
|
|
|
},
|
|
|
|
Tokens: []nef.MethodToken{},
|
|
|
|
Script: []byte{1, 2, 3},
|
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
var sender util.Uint160
|
|
|
|
var err error
|
|
|
|
|
2021-01-22 09:22:48 +00:00
|
|
|
neff.Checksum = neff.CalculateChecksum()
|
|
|
|
require.Equal(t, "9b9628e4f1611af90e761eea8cc21372380c74b6", CreateContractHash(sender, neff.Checksum, "").StringLE())
|
2020-11-18 20:10:48 +00:00
|
|
|
sender, err = util.Uint160DecodeStringLE("a400ff00ff00ff00ff00ff00ff00ff00ff00ff01")
|
|
|
|
require.NoError(t, err)
|
2021-01-22 09:22:48 +00:00
|
|
|
require.Equal(t, "66eec404d86b918d084e62a29ac9990e3b6f4286", CreateContractHash(sender, neff.Checksum, "").StringLE())
|
2020-11-18 20:10:48 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
|
|
|
|
func TestContractFromStackItem(t *testing.T) {
|
|
|
|
var (
|
|
|
|
id = stackitem.Make(42)
|
|
|
|
counter = stackitem.Make(11)
|
|
|
|
chash = stackitem.Make(util.Uint160{1, 2, 3}.BytesBE())
|
2021-01-13 12:34:10 +00:00
|
|
|
script = []byte{0, 9, 8}
|
|
|
|
nefFile, _ = nef.NewFile(script)
|
|
|
|
rawNef, _ = nefFile.Bytes()
|
|
|
|
nefItem = stackitem.NewByteArray(rawNef)
|
2020-12-13 15:26:35 +00:00
|
|
|
manifest = manifest.DefaultManifest("stack item")
|
2021-02-03 18:09:50 +00:00
|
|
|
manifItem, _ = manifest.ToStackItem()
|
2020-12-13 15:26:35 +00:00
|
|
|
|
|
|
|
badCases = []struct {
|
|
|
|
name string
|
|
|
|
item stackitem.Item
|
|
|
|
}{
|
|
|
|
{"not an array", stackitem.Make(1)},
|
2022-08-16 14:19:47 +00:00
|
|
|
{"wrong array", stackitem.Make([]stackitem.Item{})},
|
2021-01-13 12:34:10 +00:00
|
|
|
{"id is not a number", stackitem.Make([]stackitem.Item{manifItem, counter, chash, nefItem, manifItem})},
|
|
|
|
{"id is out of range", stackitem.Make([]stackitem.Item{stackitem.Make(math.MaxUint32), counter, chash, nefItem, manifItem})},
|
|
|
|
{"counter is not a number", stackitem.Make([]stackitem.Item{id, manifItem, chash, nefItem, manifItem})},
|
|
|
|
{"counter is out of range", stackitem.Make([]stackitem.Item{id, stackitem.Make(100500), chash, nefItem, manifItem})},
|
|
|
|
{"hash is not a byte string", stackitem.Make([]stackitem.Item{id, counter, stackitem.NewArray(nil), nefItem, manifItem})},
|
|
|
|
{"hash is not a hash", stackitem.Make([]stackitem.Item{id, counter, stackitem.Make([]byte{1, 2, 3}), nefItem, manifItem})},
|
|
|
|
{"nef is not a byte string", stackitem.Make([]stackitem.Item{id, counter, chash, stackitem.NewArray(nil), manifItem})},
|
2021-02-03 18:09:50 +00:00
|
|
|
{"manifest is not an array", stackitem.Make([]stackitem.Item{id, counter, chash, nefItem, stackitem.NewByteArray(nil)})},
|
|
|
|
{"manifest is not correct", stackitem.Make([]stackitem.Item{id, counter, chash, nefItem, stackitem.NewArray([]stackitem.Item{stackitem.Make(100500)})})},
|
2020-12-13 15:26:35 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
for _, cs := range badCases {
|
|
|
|
t.Run(cs.name, func(t *testing.T) {
|
|
|
|
var c = new(Contract)
|
|
|
|
err := c.FromStackItem(cs.item)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var c = new(Contract)
|
2021-01-13 12:34:10 +00:00
|
|
|
err := c.FromStackItem(stackitem.Make([]stackitem.Item{id, counter, chash, nefItem, manifItem}))
|
2020-12-13 15:26:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|