2020-06-16 12:46:28 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2020-06-16 12:46:28 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2020-12-14 09:18:59 +00:00
|
|
|
// GetExecFeeFactor invokes `getExecFeeFactor` method on a native Policy contract.
|
|
|
|
func (c *Client) GetExecFeeFactor() (int64, error) {
|
|
|
|
return c.invokeNativePolicyMethod("getExecFeeFactor")
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:46:28 +00:00
|
|
|
func (c *Client) invokeNativePolicyMethod(operation string) (int64, error) {
|
2020-12-13 16:08:47 +00:00
|
|
|
if !c.initDone {
|
|
|
|
return 0, errNetworkNotInitialized
|
|
|
|
}
|
2020-12-13 18:25:04 +00:00
|
|
|
result, err := c.InvokeFunction(c.cache.nativeHashes[nativenames.Policy], operation, []smartcontract.Parameter{}, nil)
|
2020-06-16 12:46:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-10-05 13:33:20 +00:00
|
|
|
}
|
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to invoke %s Policy method: %w", operation, err)
|
2020-06-16 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return topIntFromStack(result.Stack)
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:51:59 +00:00
|
|
|
// IsBlocked invokes `isBlocked` method on native Policy contract.
|
|
|
|
func (c *Client) IsBlocked(hash util.Uint160) (bool, error) {
|
2020-12-13 16:08:47 +00:00
|
|
|
if !c.initDone {
|
|
|
|
return false, errNetworkNotInitialized
|
|
|
|
}
|
2020-12-13 18:25:04 +00:00
|
|
|
result, err := c.InvokeFunction(c.cache.nativeHashes[nativenames.Policy], "isBlocked", []smartcontract.Parameter{{
|
2020-10-21 12:51:59 +00:00
|
|
|
Type: smartcontract.Hash160Type,
|
|
|
|
Value: hash,
|
|
|
|
}}, nil)
|
2020-06-16 12:46:28 +00:00
|
|
|
if err != nil {
|
2020-10-21 12:51:59 +00:00
|
|
|
return false, err
|
2020-06-16 12:46:28 +00:00
|
|
|
}
|
2020-10-05 13:33:20 +00:00
|
|
|
err = getInvocationError(result)
|
|
|
|
if err != nil {
|
2020-10-21 12:51:59 +00:00
|
|
|
return false, fmt.Errorf("failed to check if account is blocked: %w", err)
|
2020-10-05 13:33:20 +00:00
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
return topBoolFromStack(result.Stack)
|
2020-06-16 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 12:51:59 +00:00
|
|
|
// topBoolFromStack returns the top boolean value from stack
|
|
|
|
func topBoolFromStack(st []stackitem.Item) (bool, error) {
|
2020-06-16 12:46:28 +00:00
|
|
|
index := len(st) - 1 // top stack element is last in the array
|
2020-10-21 12:51:59 +00:00
|
|
|
result, ok := st[index].Value().(bool)
|
2020-07-31 12:26:28 +00:00
|
|
|
if !ok {
|
2020-10-21 12:51:59 +00:00
|
|
|
return false, fmt.Errorf("invalid stack item type: %s", st[index].Type())
|
2020-07-31 12:26:28 +00:00
|
|
|
}
|
2020-10-21 12:51:59 +00:00
|
|
|
return result, nil
|
2020-06-16 12:46:28 +00:00
|
|
|
}
|