2019-11-28 16:06:09 +00:00
|
|
|
package state
|
2019-09-30 16:52:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-09-30 16:52:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeDecodeContractState(t *testing.T) {
|
|
|
|
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
|
|
|
ID: 123,
|
|
|
|
UpdateCounter: 42,
|
|
|
|
Hash: h,
|
|
|
|
Script: script,
|
|
|
|
Manifest: *m,
|
2019-09-30 16:52:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 09:12:56 +00:00
|
|
|
t.Run("Serializable", func(t *testing.T) {
|
|
|
|
contractDecoded := new(Contract)
|
|
|
|
testserdes.EncodeDecodeBinary(t, contract, contractDecoded)
|
|
|
|
})
|
|
|
|
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) {
|
|
|
|
var script = []byte{1, 2, 3}
|
|
|
|
var sender util.Uint160
|
|
|
|
var err error
|
|
|
|
|
|
|
|
require.Equal(t, "b4b7417195feca1cdb5a99504ab641d8c220ae99", CreateContractHash(sender, script).StringLE())
|
|
|
|
sender, err = util.Uint160DecodeStringLE("a400ff00ff00ff00ff00ff00ff00ff00ff00ff01")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "e56e4ee87f89a70e9138432c387ad49f2ee5b55f", CreateContractHash(sender, script).StringLE())
|
|
|
|
}
|