core: avoid nil values during natives manifest marshalling

This commit is contained in:
Anna Shaleva 2021-09-09 17:03:11 +03:00
parent cba259dc47
commit 61fd7bd6ba
3 changed files with 12 additions and 0 deletions

View file

@ -140,6 +140,7 @@ func NewContractMD(name string, id int32) *ContractMD {
// Therefore values are taken from C# node.
c.NEF.Header.Compiler = "neo-core-v3.0"
c.NEF.Header.Magic = nef.Magic
c.NEF.Tokens = []nef.MethodToken{} // avoid `nil` result during JSON marshalling
c.Hash = state.CreateContractHash(util.Uint160{}, 0, c.Name)
c.Manifest = *manifest.DefaultManifest(name)

View file

@ -294,6 +294,9 @@ func (c *nep17TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount
}
func newDescriptor(name string, ret smartcontract.ParamType, ps ...manifest.Parameter) *manifest.Method {
if len(ps) == 0 {
ps = []manifest.Parameter{}
}
return &manifest.Method{
Name: name,
Parameters: ps,

View file

@ -69,6 +69,14 @@ func TestEncodeDecodeBinary(t *testing.T) {
expected.Header.Magic = Magic
testserdes.EncodeDecodeBinary(t, expected, &File{})
})
t.Run("positive with empty tokens", func(t *testing.T) {
expected.Script = script
// Check `Tokens` won't be deserialized to `nil`. We expect them to be non-nil slice in the related code.
expected.Tokens = []MethodToken{}
expected.Checksum = expected.CalculateChecksum()
expected.Header.Magic = Magic
testserdes.EncodeDecodeBinary(t, expected, &File{})
})
t.Run("invalid reserved bytes", func(t *testing.T) {
expected.Script = script
expected.Tokens = expected.Tokens[:0]