rpc: add native policy API to RPC client

part of #904
This commit is contained in:
Anna Shaleva 2020-06-16 15:46:28 +03:00
parent 9097a1a23d
commit b88863948d
3 changed files with 131 additions and 7 deletions

77
pkg/rpc/client/policy.go Normal file
View file

@ -0,0 +1,77 @@
package client
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/pkg/errors"
)
// PolicyContractHash represents BE hash of native Policy contract.
var PolicyContractHash = util.Uint160{154, 97, 164, 110, 236, 151, 184, 147, 6, 215, 206, 129, 241, 91, 70, 32, 145, 208, 9, 50}
// GetMaxTransactionsPerBlock invokes `getMaxTransactionsPerBlock` method on a
// native Policy contract.
func (c *Client) GetMaxTransactionsPerBlock() (int64, error) {
return c.invokeNativePolicyMethod("getMaxTransactionsPerBlock")
}
// GetMaxBlockSize invokes `getMaxBlockSize` method on a native Policy contract.
func (c *Client) GetMaxBlockSize() (int64, error) {
return c.invokeNativePolicyMethod("getMaxBlockSize")
}
// GetFeePerByte invokes `getFeePerByte` method on a native Policy contract.
func (c *Client) GetFeePerByte() (int64, error) {
return c.invokeNativePolicyMethod("getFeePerByte")
}
func (c *Client) invokeNativePolicyMethod(operation string) (int64, error) {
result, err := c.InvokeFunction(PolicyContractHash.StringLE(), operation, []smartcontract.Parameter{}, nil)
if err != nil {
return 0, err
} else if result.State != "HALT" || len(result.Stack) == 0 {
return 0, errors.New("invalid VM state")
}
return topIntFromStack(result.Stack)
}
// GetBlockedAccounts invokes `getBlockedAccounts` method on a native Policy contract.
func (c *Client) GetBlockedAccounts() (native.BlockedAccounts, error) {
result, err := c.InvokeFunction(PolicyContractHash.StringLE(), "getBlockedAccounts", []smartcontract.Parameter{}, nil)
if err != nil {
return nil, err
} else if result.State != "HALT" || len(result.Stack) == 0 {
return nil, errors.New("invalid VM state")
}
return topBlockedAccountsFromStack(result.Stack)
}
func topBlockedAccountsFromStack(st []smartcontract.Parameter) (native.BlockedAccounts, error) {
index := len(st) - 1 // top stack element is last in the array
var (
ba native.BlockedAccounts
err error
)
switch typ := st[index].Type; typ {
case smartcontract.ArrayType:
data, ok := st[index].Value.([]smartcontract.Parameter)
if !ok {
return nil, errors.New("invalid Array item")
}
ba = make(native.BlockedAccounts, len(data))
for i, account := range data {
ba[i], err = util.Uint160DecodeBytesLE(account.Value.([]byte))
if err != nil {
return nil, err
}
}
default:
return nil, fmt.Errorf("invalid stack item type: %s", typ)
}
return ba, nil
}

View file

@ -522,12 +522,10 @@ func (c *Client) AddNetworkFee(tx *transaction.Transaction, acc *wallet.Account)
tx.NetworkFee += netFee
size += sizeDelta
}
tx.NetworkFee += util.Fixed8(int64(size) * int64(c.GetFeePerByte()))
fee, err := c.GetFeePerByte()
if err != nil {
return err
}
tx.NetworkFee += util.Fixed8(int64(size) * fee)
return nil
}
// GetFeePerByte returns transaction network fee per byte
func (c *Client) GetFeePerByte() util.Fixed8 {
// TODO: make it a part of policy contract
return util.Fixed8(1000)
}

View file

@ -13,6 +13,7 @@ import (
"github.com/gorilla/websocket"
"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"
"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"
@ -318,6 +319,54 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
},
},
},
"getFeePerByte": {
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetFeePerByte()
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gas_consumed":"0.0200739","script":"10c00c0d676574466565506572427974650c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"1000"}]}}`,
result: func(c *Client) interface{} {
return int64(1000)
},
},
},
"getMaxTransacctionsPerBlock": {
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetMaxTransactionsPerBlock()
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gas_consumed":"0.0200739","script":"10c00c1a6765744d61785472616e73616374696f6e73506572426c6f636b0c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"512"}]}}`,
result: func(c *Client) interface{} {
return int64(512)
},
},
},
"getMaxBlockSize": {
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetMaxBlockSize()
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gas_consumed":"0.0200739","script":"10c00c0f6765744d6178426c6f636b53697a650c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"262144"}]}}`,
result: func(c *Client) interface{} {
return int64(262144)
},
},
},
"getBlockedAccounts": {
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetBlockedAccounts()
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gas_consumed":"0.0200739","script":"10c00c12676574426c6f636b65644163636f756e74730c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Array","value":[]}]}}`,
result: func(c *Client) interface{} {
return native.BlockedAccounts{}
},
},
},
"getnep5balances": {
{
name: "positive",