Merge pull request #763 from nspcc-dev/feature/rpc_client_tests
rpc: add testing to RPC client package
This commit is contained in:
commit
6ba2190394
7 changed files with 1635 additions and 4 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"crypto/elliptic"
|
||||
"crypto/x509"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
|
@ -274,3 +275,28 @@ func (p *PublicKey) String() string {
|
|||
by := hex.EncodeToString(p.Y.Bytes())
|
||||
return fmt.Sprintf("%s%s", bx, by)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaler interface.
|
||||
func (p PublicKey) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(hex.EncodeToString(p.Bytes()))
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler interface.
|
||||
func (p *PublicKey) UnmarshalJSON(data []byte) error {
|
||||
l := len(data)
|
||||
if l < 2 || data[0] != '"' || data[l-1] != '"' {
|
||||
return errors.New("wrong format")
|
||||
}
|
||||
|
||||
bytes := make([]byte, l-2)
|
||||
_, err := hex.Decode(bytes, data[1:l-1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = p.DecodeBytes(bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package keys
|
|||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"sort"
|
||||
"testing"
|
||||
|
@ -143,3 +144,45 @@ func getPubKey(t *testing.T) *PublicKey {
|
|||
require.NoError(t, err)
|
||||
return pubKey
|
||||
}
|
||||
|
||||
func TestMarshallJSON(t *testing.T) {
|
||||
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
|
||||
pubKey, err := NewPublicKeyFromString(str)
|
||||
require.NoError(t, err)
|
||||
|
||||
bytes, err := json.Marshal(&pubKey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte(`"`+str+`"`), bytes)
|
||||
}
|
||||
|
||||
func TestUnmarshallJSON(t *testing.T) {
|
||||
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
|
||||
expected, err := NewPublicKeyFromString(str)
|
||||
require.NoError(t, err)
|
||||
|
||||
actual := &PublicKey{}
|
||||
err = json.Unmarshal([]byte(`"`+str+`"`), actual)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestUnmarshallJSONBadCompresed(t *testing.T) {
|
||||
str := `"02ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"`
|
||||
actual := &PublicKey{}
|
||||
err := json.Unmarshal([]byte(str), actual)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUnmarshallJSONNotAHex(t *testing.T) {
|
||||
str := `"04Tb17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"`
|
||||
actual := &PublicKey{}
|
||||
err := json.Unmarshal([]byte(str), actual)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUnmarshallJSONBadFormat(t *testing.T) {
|
||||
str := "046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"
|
||||
actual := &PublicKey{}
|
||||
err := json.Unmarshal([]byte(str), actual)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
|
1490
pkg/rpc/client/rpc_test.go
Normal file
1490
pkg/rpc/client/rpc_test.go
Normal file
File diff suppressed because it is too large
Load diff
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/core"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
)
|
||||
|
@ -22,7 +23,7 @@ type (
|
|||
Timestamp uint32 `json:"time"`
|
||||
Index uint32 `json:"index"`
|
||||
Nonce string `json:"nonce"`
|
||||
NextConsensus util.Uint160 `json:"nextconsensus"`
|
||||
NextConsensus string `json:"nextconsensus"`
|
||||
Script transaction.Witness `json:"script"`
|
||||
Confirmations uint32 `json:"confirmations"`
|
||||
NextBlockHash *util.Uint256 `json:"nextblockhash,omitempty"`
|
||||
|
@ -40,7 +41,7 @@ func NewHeader(h *block.Header, chain core.Blockchainer) Header {
|
|||
Timestamp: h.Timestamp,
|
||||
Index: h.Index,
|
||||
Nonce: strconv.FormatUint(h.ConsensusData, 16),
|
||||
NextConsensus: h.NextConsensus,
|
||||
NextConsensus: address.Uint160ToString(h.NextConsensus),
|
||||
Script: h.Script,
|
||||
Confirmations: chain.BlockHeight() - h.Index + 1,
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package result
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
|
@ -29,6 +32,21 @@ type Properties struct {
|
|||
IsPayable bool `json:"is_payable"`
|
||||
}
|
||||
|
||||
// contractState is an auxilliary struct for proper Script marshaling.
|
||||
type contractState struct {
|
||||
Version byte `json:"version"`
|
||||
ScriptHash util.Uint160 `json:"hash"`
|
||||
Script string `json:"script"`
|
||||
ParamList []smartcontract.ParamType `json:"parameters"`
|
||||
ReturnType smartcontract.ParamType `json:"returntype"`
|
||||
Name string `json:"name"`
|
||||
CodeVersion string `json:"code_version"`
|
||||
Author string `json:"author"`
|
||||
Email string `json:"email"`
|
||||
Description string `json:"description"`
|
||||
Properties Properties `json:"properties"`
|
||||
}
|
||||
|
||||
// NewContractState creates a new Contract wrapper.
|
||||
func NewContractState(c *state.Contract) ContractState {
|
||||
properties := Properties{
|
||||
|
@ -51,3 +69,56 @@ func NewContractState(c *state.Contract) ContractState {
|
|||
Description: c.Description,
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler interface.
|
||||
func (c ContractState) MarshalJSON() ([]byte, error) {
|
||||
s := &contractState{
|
||||
Version: c.Version,
|
||||
ScriptHash: c.ScriptHash,
|
||||
Script: hex.EncodeToString(c.Script),
|
||||
ParamList: c.ParamList,
|
||||
ReturnType: c.ReturnType,
|
||||
Name: c.Name,
|
||||
CodeVersion: c.CodeVersion,
|
||||
Author: c.Author,
|
||||
Email: c.Email,
|
||||
Description: c.Description,
|
||||
Properties: Properties{
|
||||
HasStorage: c.Properties.HasStorage,
|
||||
HasDynamicInvoke: c.Properties.HasDynamicInvoke,
|
||||
IsPayable: c.Properties.IsPayable,
|
||||
},
|
||||
}
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler interface.
|
||||
func (c *ContractState) UnmarshalJSON(data []byte) error {
|
||||
s := new(contractState)
|
||||
if err := json.Unmarshal(data, s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
script, err := hex.DecodeString(s.Script)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.Version = s.Version
|
||||
c.ScriptHash = s.ScriptHash
|
||||
c.Script = script
|
||||
c.ParamList = s.ParamList
|
||||
c.ReturnType = s.ReturnType
|
||||
c.Name = s.Name
|
||||
c.CodeVersion = s.CodeVersion
|
||||
c.Author = s.Author
|
||||
c.Email = s.Email
|
||||
c.Description = s.Description
|
||||
c.Properties = Properties{
|
||||
HasStorage: s.Properties.HasStorage,
|
||||
HasDynamicInvoke: s.Properties.HasDynamicInvoke,
|
||||
IsPayable: s.Properties.IsPayable,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
// NEP5Balances is a result for the getnep5balances RPC call.
|
||||
type NEP5Balances struct {
|
||||
Balances []NEP5Balance `json:"balances"`
|
||||
Balances []NEP5Balance `json:"balance"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
|
|
|
@ -441,7 +441,7 @@ var rpcTestCases = map[string][]rpcTestCase{
|
|||
Timestamp: header.Timestamp,
|
||||
Index: header.Index,
|
||||
Nonce: strconv.FormatUint(header.ConsensusData, 16),
|
||||
NextConsensus: header.NextConsensus,
|
||||
NextConsensus: address.Uint160ToString(header.NextConsensus),
|
||||
Script: header.Script,
|
||||
Confirmations: e.chain.BlockHeight() - header.Index + 1,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue