Merge pull request #575 from nspcc-dev/feature/rpc_and_serialize_fixes

Fixes missing unmarshal function for TXType and missing parametrization for "getblock" so
it can return pure bytes.
This commit is contained in:
Roman Khimov 2019-12-25 08:46:01 +03:00 committed by GitHub
commit ef3510072a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 2 deletions

View file

@ -1,5 +1,11 @@
package transaction
import (
"strings"
"github.com/pkg/errors"
)
// TXType is the type of a transaction.
type TXType uint8
@ -52,3 +58,44 @@ func (t TXType) String() string {
func (t TXType) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.String() + `"`), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *TXType) UnmarshalJSON(data []byte) error {
l := len(data)
if l < 2 || data[0] != '"' || data[l-1] != '"' {
return errors.New("wrong format")
}
var err error
*t, err = TXTypeFromString(string(data[1 : l-1]))
return err
}
// TXTypeFromString searches for TXType by string name.
func TXTypeFromString(jsonString string) (TXType, error) {
switch jsonString = strings.TrimSpace(jsonString); jsonString {
case "MinerTransaction":
return MinerType, nil
case "IssueTransaction":
return IssueType, nil
case "ClaimTransaction":
return ClaimType, nil
case "EnrollmentTransaction":
return EnrollmentType, nil
case "VotingTransaction":
return VotingType, nil
case "RegisterTransaction":
return RegisterType, nil
case "ContractTransaction":
return ContractType, nil
case "StateTransaction":
return StateType, nil
case "AgencyTransaction":
return AgencyType, nil
case "PublishTransaction":
return PublishType, nil
case "InvocationTransaction":
return InvocationType, nil
default:
return 0, errors.New("unknown state")
}
}

View file

@ -152,7 +152,14 @@ Methods:
break
}
results = wrappers.NewBlock(block, s.chain)
if len(reqParams) == 2 && reqParams[1].Value == 1 {
results = wrappers.NewBlock(block, s.chain)
} else {
writer := io.NewBufBinWriter()
block.EncodeBinary(writer.BinWriter)
results = hex.EncodeToString(writer.Bytes())
}
case "getblockcount":
getblockcountCalled.Inc()
results = s.chain.BlockHeight() + 1

View file

@ -108,7 +108,7 @@ var rpcTestCases = map[string][]rpcTestCase{
"getblock": {
{
name: "positive",
params: "[1]",
params: "[1, 1]",
result: func(e *executor) interface{} { return &GetBlockResponse{} },
check: func(t *testing.T, e *executor, result interface{}) {
res, ok := result.(*GetBlockResponse)