forked from TrueCloudLab/neoneo-go
transaction: marshal oracle response codes as strings
C# node does it this way.
This commit is contained in:
parent
607cdcf13d
commit
bba22cb736
3 changed files with 111 additions and 1 deletions
|
@ -29,6 +29,18 @@ func TestAttribute_EncodeBinary(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
||||||
|
for _, code := range []OracleResponseCode{ProtocolNotSupported, ConsensusUnreachable,
|
||||||
|
NotFound, Timeout, Forbidden, ResponseTooLarge, InsufficientFunds, Error} {
|
||||||
|
attr = &Attribute{
|
||||||
|
Type: OracleResponseT,
|
||||||
|
Value: &OracleResponse{
|
||||||
|
ID: 42,
|
||||||
|
Code: code,
|
||||||
|
Result: []byte{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
t.Run("NotValidBefore", func(t *testing.T) {
|
t.Run("NotValidBefore", func(t *testing.T) {
|
||||||
t.Run("positive", func(t *testing.T) {
|
t.Run("positive", func(t *testing.T) {
|
||||||
|
@ -144,7 +156,7 @@ func TestAttribute_MarshalJSON(t *testing.T) {
|
||||||
require.JSONEq(t, `{
|
require.JSONEq(t, `{
|
||||||
"type":"OracleResponse",
|
"type":"OracleResponse",
|
||||||
"id": 123,
|
"id": 123,
|
||||||
"code": 0,
|
"code": "Success",
|
||||||
"result": "`+base64.StdEncoding.EncodeToString(res)+`"}`, string(data))
|
"result": "`+base64.StdEncoding.EncodeToString(res)+`"}`, string(data))
|
||||||
|
|
||||||
actual := new(Attribute)
|
actual := new(Attribute)
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:generate stringer -type=OracleResponseCode
|
||||||
|
|
||||||
// OracleResponseCode represents result code of oracle response.
|
// OracleResponseCode represents result code of oracle response.
|
||||||
type OracleResponseCode byte
|
type OracleResponseCode byte
|
||||||
|
|
||||||
|
@ -46,6 +50,43 @@ func (c OracleResponseCode) IsValid() bool {
|
||||||
c == InsufficientFunds || c == Error
|
c == InsufficientFunds || c == Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler interface.
|
||||||
|
func (c OracleResponseCode) MarshalJSON() ([]byte, error) {
|
||||||
|
return []byte(`"` + c.String() + `"`), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
||||||
|
func (c *OracleResponseCode) UnmarshalJSON(data []byte) error {
|
||||||
|
var js string
|
||||||
|
if err := json.Unmarshal(data, &js); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
js = strings.ToLower(js)
|
||||||
|
switch js {
|
||||||
|
case "success":
|
||||||
|
*c = Success
|
||||||
|
case "protocolnotsupported":
|
||||||
|
*c = ProtocolNotSupported
|
||||||
|
case "consensusunreachable":
|
||||||
|
*c = ConsensusUnreachable
|
||||||
|
case "notfound":
|
||||||
|
*c = NotFound
|
||||||
|
case "timeout":
|
||||||
|
*c = Timeout
|
||||||
|
case "forbidden":
|
||||||
|
*c = Forbidden
|
||||||
|
case "responsetoolarge":
|
||||||
|
*c = ResponseTooLarge
|
||||||
|
case "insufficientfunds":
|
||||||
|
*c = InsufficientFunds
|
||||||
|
case "error":
|
||||||
|
*c = Error
|
||||||
|
default:
|
||||||
|
return errors.New("invalid oracle response code")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DecodeBinary implements io.Serializable interface.
|
// DecodeBinary implements io.Serializable interface.
|
||||||
func (r *OracleResponse) DecodeBinary(br *io.BinReader) {
|
func (r *OracleResponse) DecodeBinary(br *io.BinReader) {
|
||||||
r.ID = br.ReadU64LE()
|
r.ID = br.ReadU64LE()
|
||||||
|
|
57
pkg/core/transaction/oracleresponsecode_string.go
Normal file
57
pkg/core/transaction/oracleresponsecode_string.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
// Code generated by "stringer -type=OracleResponseCode"; DO NOT EDIT.
|
||||||
|
|
||||||
|
package transaction
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
|
// Re-run the stringer command to generate them again.
|
||||||
|
var x [1]struct{}
|
||||||
|
_ = x[Success-0]
|
||||||
|
_ = x[ProtocolNotSupported-16]
|
||||||
|
_ = x[ConsensusUnreachable-18]
|
||||||
|
_ = x[NotFound-20]
|
||||||
|
_ = x[Timeout-22]
|
||||||
|
_ = x[Forbidden-24]
|
||||||
|
_ = x[ResponseTooLarge-26]
|
||||||
|
_ = x[InsufficientFunds-28]
|
||||||
|
_ = x[Error-255]
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
_OracleResponseCode_name_0 = "Success"
|
||||||
|
_OracleResponseCode_name_1 = "ProtocolNotSupported"
|
||||||
|
_OracleResponseCode_name_2 = "ConsensusUnreachable"
|
||||||
|
_OracleResponseCode_name_3 = "NotFound"
|
||||||
|
_OracleResponseCode_name_4 = "Timeout"
|
||||||
|
_OracleResponseCode_name_5 = "Forbidden"
|
||||||
|
_OracleResponseCode_name_6 = "ResponseTooLarge"
|
||||||
|
_OracleResponseCode_name_7 = "InsufficientFunds"
|
||||||
|
_OracleResponseCode_name_8 = "Error"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (i OracleResponseCode) String() string {
|
||||||
|
switch {
|
||||||
|
case i == 0:
|
||||||
|
return _OracleResponseCode_name_0
|
||||||
|
case i == 16:
|
||||||
|
return _OracleResponseCode_name_1
|
||||||
|
case i == 18:
|
||||||
|
return _OracleResponseCode_name_2
|
||||||
|
case i == 20:
|
||||||
|
return _OracleResponseCode_name_3
|
||||||
|
case i == 22:
|
||||||
|
return _OracleResponseCode_name_4
|
||||||
|
case i == 24:
|
||||||
|
return _OracleResponseCode_name_5
|
||||||
|
case i == 26:
|
||||||
|
return _OracleResponseCode_name_6
|
||||||
|
case i == 28:
|
||||||
|
return _OracleResponseCode_name_7
|
||||||
|
case i == 255:
|
||||||
|
return _OracleResponseCode_name_8
|
||||||
|
default:
|
||||||
|
return "OracleResponseCode(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue