smartcontract: add JSON marshal/unmarshal for InteropType
We actually have to do that in order to answer getapplicationlog requests for transactions that leave some interop items on the stack. It follows the same logic our binary serializer/deserializes does leaving the type and stripping the value (whatever that is).
This commit is contained in:
parent
d5df1212c2
commit
29ada4ca46
2 changed files with 33 additions and 6 deletions
|
@ -85,6 +85,8 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
|
||||||
case MapType:
|
case MapType:
|
||||||
ppair := p.Value.([]ParameterPair)
|
ppair := p.Value.([]ParameterPair)
|
||||||
resultRawValue, resultErr = json.Marshal(ppair)
|
resultRawValue, resultErr = json.Marshal(ppair)
|
||||||
|
case InteropInterfaceType:
|
||||||
|
resultRawValue = []byte("null")
|
||||||
default:
|
default:
|
||||||
resultErr = errors.Errorf("Marshaller for type %s not implemented", p.Type)
|
resultErr = errors.Errorf("Marshaller for type %s not implemented", p.Type)
|
||||||
}
|
}
|
||||||
|
@ -166,6 +168,9 @@ func (p *Parameter) UnmarshalJSON(data []byte) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.Value = h
|
p.Value = h
|
||||||
|
case InteropInterfaceType:
|
||||||
|
// stub, ignore value, it can only be null
|
||||||
|
p.Value = nil
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("Unmarshaller for type %s not implemented", p.Type)
|
return errors.Errorf("Unmarshaller for type %s not implemented", p.Type)
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,6 +122,13 @@ var marshalJSONTestCases = []struct {
|
||||||
},
|
},
|
||||||
result: `{"type":"Hash256","value":"0xf037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"}`,
|
result: `{"type":"Hash256","value":"0xf037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"}`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: Parameter{
|
||||||
|
Type: InteropInterfaceType,
|
||||||
|
Value: nil,
|
||||||
|
},
|
||||||
|
result: `{"type":"InteropInterface","value":null}`,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var marshalJSONErrorCases = []Parameter{
|
var marshalJSONErrorCases = []Parameter{
|
||||||
|
@ -129,10 +136,6 @@ var marshalJSONErrorCases = []Parameter{
|
||||||
Type: UnknownType,
|
Type: UnknownType,
|
||||||
Value: nil,
|
Value: nil,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Type: InteropInterfaceType,
|
|
||||||
Value: nil,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Type: IntegerType,
|
Type: IntegerType,
|
||||||
Value: math.Inf(1),
|
Value: math.Inf(1),
|
||||||
|
@ -252,6 +255,27 @@ var unmarshalJSONTestCases = []struct {
|
||||||
},
|
},
|
||||||
input: `{"type":"PublicKey","value":"03b3bf1502fbdc05449b506aaf04579724024b06542e49262bfaa3f70e200040a9"}`,
|
input: `{"type":"PublicKey","value":"03b3bf1502fbdc05449b506aaf04579724024b06542e49262bfaa3f70e200040a9"}`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: `{"type":"InteropInterface","value":null}`,
|
||||||
|
result: Parameter{
|
||||||
|
Type: InteropInterfaceType,
|
||||||
|
Value: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `{"type":"InteropInterface","value":""}`,
|
||||||
|
result: Parameter{
|
||||||
|
Type: InteropInterfaceType,
|
||||||
|
Value: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `{"type":"InteropInterface","value":"Hundertwasser"}`,
|
||||||
|
result: Parameter{
|
||||||
|
Type: InteropInterfaceType,
|
||||||
|
Value: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var unmarshalJSONErrorCases = []string{
|
var unmarshalJSONErrorCases = []string{
|
||||||
|
@ -272,8 +296,6 @@ var unmarshalJSONErrorCases = []string{
|
||||||
`{"type": "Map","value": ["key": {}]}`, // incorrect Map value
|
`{"type": "Map","value": ["key": {}]}`, // incorrect Map value
|
||||||
`{"type": "Map","value": ["key": {"type":"String", "value":"qwer"}, "value": {"type":"Boolean"}]}`, // incorrect Map Value value
|
`{"type": "Map","value": ["key": {"type":"String", "value":"qwer"}, "value": {"type":"Boolean"}]}`, // incorrect Map Value value
|
||||||
`{"type": "Map","value": ["key": {"type":"String"}, "value": {"type":"Boolean", "value":true}]}`, // incorrect Map Key value
|
`{"type": "Map","value": ["key": {"type":"String"}, "value": {"type":"Boolean", "value":true}]}`, // incorrect Map Key value
|
||||||
|
|
||||||
`{"type": "InteropInterface","value": ""}`, // ununmarshable type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParam_UnmarshalJSON(t *testing.T) {
|
func TestParam_UnmarshalJSON(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue