rpc: add (*Client).GetDesignatedByRole method

This commit is contained in:
Anna Shaleva 2021-03-22 20:34:27 +03:00
parent 1e649bc9a0
commit 10fb86c0b2
2 changed files with 80 additions and 0 deletions

View file

@ -3,9 +3,14 @@ package client
// Various non-policy things from native contracts.
import (
"crypto/elliptic"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// GetOraclePrice invokes `getPrice` method on a native Oracle contract.
@ -34,3 +39,54 @@ func (c *Client) GetGasPerBlock() (int64, error) {
}
return c.invokeNativeGetMethod(neoHash, "getGasPerBlock")
}
// GetDesignatedByRole invokes `getDesignatedByRole` method on a native RoleManagement contract.
func (c *Client) GetDesignatedByRole(role noderoles.Role, index uint32) (keys.PublicKeys, error) {
rmHash, err := c.GetNativeContractHash(nativenames.Designation)
if err != nil {
return nil, fmt.Errorf("failed to get native RoleManagement hash: %w", err)
}
result, err := c.InvokeFunction(rmHash, "getDesignatedByRole", []smartcontract.Parameter{
{
Type: smartcontract.IntegerType,
Value: int64(role),
},
{
Type: smartcontract.IntegerType,
Value: int64(index),
},
}, nil)
if err != nil {
return nil, err
}
err = getInvocationError(result)
if err != nil {
return nil, fmt.Errorf("`getDesignatedByRole`: %w", err)
}
return topPublicKeysFromStack(result.Stack)
}
// topPublicKeysFromStack returns the top array of public keys from stack.
func topPublicKeysFromStack(st []stackitem.Item) (keys.PublicKeys, error) {
index := len(st) - 1 // top stack element is last in the array
var (
pks keys.PublicKeys
err error
)
items, ok := st[index].Value().([]stackitem.Item)
if !ok {
return nil, fmt.Errorf("invalid stack item type: %s", st[index].Type())
}
pks = make(keys.PublicKeys, len(items))
for i, item := range items {
val, ok := item.Value().([]byte)
if !ok {
return nil, fmt.Errorf("invalid array element #%d: %s", i, item.Type())
}
pks[i], err = keys.NewPublicKeyFromBytes(val, elliptic.P256())
if err != nil {
return nil, err
}
}
return pks, nil
}

View file

@ -2,6 +2,7 @@ package client
import (
"context"
"crypto/elliptic"
"encoding/base64"
"encoding/hex"
"encoding/json"
@ -17,6 +18,7 @@ import (
"github.com/nspcc-dev/neo-go/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
@ -476,6 +478,28 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
},
},
},
"getDesignatedByRole": {
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetDesignatedByRole(noderoles.P2PNotary, 10)
},
serverResponse: `{"id" : 1,"result" : {"stack" : [{"value" : [{"type":"ByteString","value":"Aw0WkQoDc8WqpG18xPMTEgfHO6gRTVtMN0Mw6zw06fzl"},{"type":"ByteString","value":"A+bmJ9wIaj96Ygr+uQQvQ0AaUrQmj2b3AGnztAOkU3/L"}],"type" : "Array"}],"exception" : null,"script" : "ERQSwB8ME2dldERlc2lnbmF0ZWRCeVJvbGUMFOKV45FUTBeK2U8D7E3N/3hTTs9JQWJ9W1I=","gasconsumed" : "2028150","state" : "HALT"}, "jsonrpc" : "2.0"}`,
result: func(c *Client) interface{} {
pk1Bytes, _ := base64.StdEncoding.DecodeString("Aw0WkQoDc8WqpG18xPMTEgfHO6gRTVtMN0Mw6zw06fzl")
pk1, err := keys.NewPublicKeyFromBytes(pk1Bytes, elliptic.P256())
if err != nil {
panic("invalid pub key #1 bytes")
}
pk2Bytes, _ := base64.StdEncoding.DecodeString("A+bmJ9wIaj96Ygr+uQQvQ0AaUrQmj2b3AGnztAOkU3/L")
pk2, err := keys.NewPublicKeyFromBytes(pk2Bytes, elliptic.P256())
if err != nil {
panic("invalid pub key #2 bytes")
}
return keys.PublicKeys{pk1, pk2}
},
},
},
"getMaxNotValidBeforeDelta": {
{
name: "positive",