2019-11-28 16:06:09 +00:00
|
|
|
package state
|
2019-11-26 15:47:07 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-03 16:58:50 +00:00
|
|
|
"encoding/json"
|
2019-11-26 15:47:07 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/random"
|
2020-03-26 14:43:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
|
2020-09-21 11:08:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-07-27 14:57:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-09-03 16:58:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-26 15:47:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeDecodeNotificationEvent(t *testing.T) {
|
|
|
|
event := &NotificationEvent{
|
2019-12-04 12:24:49 +00:00
|
|
|
ScriptHash: random.Uint160(),
|
2020-06-29 08:25:32 +00:00
|
|
|
Name: "Event",
|
|
|
|
Item: stackitem.NewArray([]stackitem.Item{stackitem.NewBool(true)}),
|
2019-11-26 15:47:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, event, new(NotificationEvent))
|
2019-11-26 15:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncodeDecodeAppExecResult(t *testing.T) {
|
|
|
|
appExecResult := &AppExecResult{
|
2019-12-04 12:24:49 +00:00
|
|
|
TxHash: random.Uint256(),
|
2019-11-26 15:47:07 +00:00
|
|
|
Trigger: 1,
|
2020-07-27 14:57:53 +00:00
|
|
|
VMState: vm.HaltState,
|
2019-11-26 15:47:07 +00:00
|
|
|
GasConsumed: 10,
|
2020-07-31 12:48:35 +00:00
|
|
|
Stack: []stackitem.Item{},
|
2019-11-26 15:47:07 +00:00
|
|
|
Events: []NotificationEvent{},
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, appExecResult, new(AppExecResult))
|
2019-11-26 15:47:07 +00:00
|
|
|
}
|
2020-09-03 16:58:50 +00:00
|
|
|
|
|
|
|
func TestMarshalUnmarshalJSONNotificationEvent(t *testing.T) {
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
ne := &NotificationEvent{
|
|
|
|
ScriptHash: random.Uint160(),
|
|
|
|
Name: "my_ne",
|
|
|
|
Item: stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewBool(true),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
testserdes.MarshalUnmarshalJSON(t, ne, new(NotificationEvent))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MarshalJSON recursive reference", func(t *testing.T) {
|
|
|
|
i := make([]stackitem.Item, 1)
|
|
|
|
recursive := stackitem.NewArray(i)
|
|
|
|
i[0] = recursive
|
|
|
|
ne := &NotificationEvent{
|
|
|
|
Item: recursive,
|
|
|
|
}
|
|
|
|
_, err := json.Marshal(ne)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UnmarshalJSON error", func(t *testing.T) {
|
|
|
|
errorCases := []string{
|
|
|
|
`{"contract":"0xBadHash","eventname":"my_ne","state":{"type":"Array","value":[{"type":"Boolean","value":true}]}}`,
|
|
|
|
`{"contract":"0xab2f820e2aa7cca1e081283c58a7d7943c33a2f1","eventname":"my_ne","state":{"type":"Array","value":[{"type":"BadType","value":true}]}}`,
|
|
|
|
`{"contract":"0xab2f820e2aa7cca1e081283c58a7d7943c33a2f1","eventname":"my_ne","state":{"type":"Boolean", "value":true}}`,
|
|
|
|
}
|
|
|
|
for _, errCase := range errorCases {
|
|
|
|
err := json.Unmarshal([]byte(errCase), new(NotificationEvent))
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalUnmarshalJSONAppExecResult(t *testing.T) {
|
2020-09-21 11:08:15 +00:00
|
|
|
t.Run("positive, transaction", func(t *testing.T) {
|
2020-09-03 16:58:50 +00:00
|
|
|
appExecResult := &AppExecResult{
|
|
|
|
TxHash: random.Uint256(),
|
2020-09-21 11:08:15 +00:00
|
|
|
Trigger: trigger.Application,
|
2020-09-03 16:58:50 +00:00
|
|
|
VMState: vm.HaltState,
|
|
|
|
GasConsumed: 10,
|
|
|
|
Stack: []stackitem.Item{},
|
|
|
|
Events: []NotificationEvent{},
|
|
|
|
}
|
|
|
|
testserdes.MarshalUnmarshalJSON(t, appExecResult, new(AppExecResult))
|
|
|
|
})
|
|
|
|
|
2020-09-21 11:08:15 +00:00
|
|
|
t.Run("positive, block", func(t *testing.T) {
|
|
|
|
appExecResult := &AppExecResult{
|
|
|
|
TxHash: random.Uint256(),
|
|
|
|
Trigger: trigger.System,
|
|
|
|
VMState: vm.HaltState,
|
|
|
|
GasConsumed: 10,
|
|
|
|
Stack: []stackitem.Item{},
|
|
|
|
Events: []NotificationEvent{},
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(appExecResult)
|
|
|
|
require.NoError(t, err)
|
|
|
|
actual := new(AppExecResult)
|
|
|
|
require.NoError(t, json.Unmarshal(data, actual))
|
|
|
|
expected := &AppExecResult{
|
|
|
|
// we have no way to restore block hash as it was not marshalled
|
|
|
|
TxHash: util.Uint256{},
|
|
|
|
Trigger: appExecResult.Trigger,
|
|
|
|
VMState: appExecResult.VMState,
|
|
|
|
GasConsumed: appExecResult.GasConsumed,
|
|
|
|
Stack: appExecResult.Stack,
|
|
|
|
Events: appExecResult.Events,
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
2020-09-03 16:58:50 +00:00
|
|
|
t.Run("MarshalJSON recursive reference", func(t *testing.T) {
|
|
|
|
i := make([]stackitem.Item, 1)
|
|
|
|
recursive := stackitem.NewArray(i)
|
|
|
|
i[0] = recursive
|
|
|
|
errorCases := []*AppExecResult{
|
|
|
|
{
|
|
|
|
Stack: i,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, errCase := range errorCases {
|
|
|
|
_, err := json.Marshal(errCase)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UnmarshalJSON error", func(t *testing.T) {
|
|
|
|
nilStackCases := []string{
|
|
|
|
`{"txid":"0x17145a039fca704fcdbeb46e6b210af98a1a9e5b9768e46ffc38f71c79ac2521","trigger":"Application","vmstate":"HALT","gasconsumed":"1","stack":[{"type":"WrongType","value":"1"}],"notifications":[]}`,
|
|
|
|
}
|
|
|
|
for _, str := range nilStackCases {
|
|
|
|
actual := new(AppExecResult)
|
|
|
|
err := json.Unmarshal([]byte(str), actual)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, actual.Stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCases := []string{
|
|
|
|
`{"txid":"0xBadHash","trigger":"Application","vmstate":"HALT","gasconsumed":"1","stack":[{"type":"Integer","value":"1"}],"notifications":[]}`,
|
|
|
|
`{"txid":"0x17145a039fca704fcdbeb46e6b210af98a1a9e5b9768e46ffc38f71c79ac2521","trigger":"Application","vmstate":"BadState","gasconsumed":"1","stack":[{"type":"Integer","value":"1"}],"notifications":[]}`,
|
|
|
|
`{"txid":"0x17145a039fca704fcdbeb46e6b210af98a1a9e5b9768e46ffc38f71c79ac2521","trigger":"BadTrigger","vmstate":"HALT","gasconsumed":"1","stack":[{"type":"Integer","value":"1"}],"notifications":[]}`,
|
|
|
|
}
|
|
|
|
for _, str := range errorCases {
|
|
|
|
actual := new(AppExecResult)
|
|
|
|
err := json.Unmarshal([]byte(str), actual)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|