2020-06-16 12:46:28 +00:00
|
|
|
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"
|
2020-07-31 12:26:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-06-16 12:46:28 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-07-22 16:03:05 +00:00
|
|
|
// PolicyContractHash represents a hash of native Policy contract.
|
|
|
|
var PolicyContractHash, _ = util.Uint160DecodeStringBE("e9ff4ca7cc252e1dfddb26315869cd79505906ce")
|
2020-06-16 12:46:28 +00:00
|
|
|
|
|
|
|
// 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) {
|
2020-07-02 13:38:33 +00:00
|
|
|
result, err := c.InvokeFunction(PolicyContractHash, operation, []smartcontract.Parameter{}, nil)
|
2020-06-16 12:46:28 +00:00
|
|
|
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) {
|
2020-07-02 13:38:33 +00:00
|
|
|
result, err := c.InvokeFunction(PolicyContractHash, "getBlockedAccounts", []smartcontract.Parameter{}, nil)
|
2020-06-16 12:46:28 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:26:28 +00:00
|
|
|
func topBlockedAccountsFromStack(st []stackitem.Item) (native.BlockedAccounts, error) {
|
2020-06-16 12:46:28 +00:00
|
|
|
index := len(st) - 1 // top stack element is last in the array
|
|
|
|
var (
|
|
|
|
ba native.BlockedAccounts
|
|
|
|
err error
|
|
|
|
)
|
2020-07-31 12:26:28 +00:00
|
|
|
items, ok := st[index].Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid stack item type: %s", st[index].Type())
|
|
|
|
}
|
|
|
|
ba = make(native.BlockedAccounts, len(items))
|
|
|
|
for i, account := range items {
|
|
|
|
val, ok := account.Value().([]byte)
|
2020-06-16 12:46:28 +00:00
|
|
|
if !ok {
|
2020-07-31 12:26:28 +00:00
|
|
|
return nil, fmt.Errorf("invalid array element: %s", account.Type())
|
2020-06-16 12:46:28 +00:00
|
|
|
}
|
2020-07-31 12:26:28 +00:00
|
|
|
ba[i], err = util.Uint160DecodeBytesLE(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-16 12:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ba, nil
|
|
|
|
}
|