util: move Fixed8 to encoding/fixedn package

This commit is contained in:
Evgenii Stratonikov 2020-12-01 11:40:58 +03:00
parent 8ed1d4dfba
commit e4c3339c91
16 changed files with 63 additions and 53 deletions

View file

@ -4,13 +4,13 @@ import (
"flag"
"strings"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/urfave/cli"
)
// Fixed8 is a wrapper for Uint160 with flag.Value methods.
type Fixed8 struct {
Value util.Fixed8
Value fixedn.Fixed8
}
// Fixed8Flag is a flag with type string.
@ -32,7 +32,7 @@ func (a Fixed8) String() string {
// Set implements flag.Value interface.
func (a *Fixed8) Set(s string) error {
f, err := util.Fixed8FromString(s)
f, err := fixedn.Fixed8FromString(s)
if err != nil {
return cli.NewExitError(err, 1)
}
@ -41,7 +41,7 @@ func (a *Fixed8) Set(s string) error {
}
// Fixed8 casts address to util.Fixed8.
func (a *Fixed8) Fixed8() util.Fixed8 {
func (a *Fixed8) Fixed8() fixedn.Fixed8 {
return a.Value
}
@ -70,6 +70,6 @@ func (f Fixed8Flag) Apply(set *flag.FlagSet) {
}
// Fixed8FromContext returns parsed util.Fixed8 value provided flag name.
func Fixed8FromContext(ctx *cli.Context, name string) util.Fixed8 {
func Fixed8FromContext(ctx *cli.Context, name string) fixedn.Fixed8 {
return ctx.Generic(name).(*Fixed8).Value
}

View file

@ -5,12 +5,12 @@ import (
"io/ioutil"
"testing"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/stretchr/testify/require"
)
func TestFixed8_String(t *testing.T) {
value := util.Fixed8(123)
value := fixedn.Fixed8(123)
f := Fixed8{
Value: value,
}
@ -19,7 +19,7 @@ func TestFixed8_String(t *testing.T) {
}
func TestFixed8_Set(t *testing.T) {
value := util.Fixed8(123)
value := fixedn.Fixed8(123)
f := Fixed8{}
require.Error(t, f.Set("not-a-fixed8"))
@ -30,10 +30,10 @@ func TestFixed8_Set(t *testing.T) {
func TestFixed8_Fixed8(t *testing.T) {
f := Fixed8{
Value: util.Fixed8(123),
Value: fixedn.Fixed8(123),
}
require.Equal(t, util.Fixed8(123), f.Fixed8())
require.Equal(t, fixedn.Fixed8(123), f.Fixed8())
}
func TestFixed8Flag_String(t *testing.T) {

View file

@ -10,7 +10,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
)
@ -47,7 +47,7 @@ func TestNEP17Balance(t *testing.T) {
e.checkNextLine(t, "^\\s*Account\\s+"+validatorAddr)
e.checkNextLine(t, "^\\s*GAS:\\s+GAS \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)")
b := e.Chain.GetUtilityTokenBalance(validatorHash)
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+util.Fixed8(b.Int64()).String())
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(b.Int64()).String())
})
t.Run("all accounts", func(t *testing.T) {
e.Run(t, cmdbase...)
@ -56,7 +56,7 @@ func TestNEP17Balance(t *testing.T) {
e.checkNextLine(t, "^Account "+address.Uint160ToString(addr1))
e.checkNextLine(t, "^\\s*GAS:\\s+GAS \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)")
balance := e.Chain.GetUtilityTokenBalance(addr1)
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+util.Fixed8(balance.Int64()).String())
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String())
e.checkNextLine(t, "^\\s*Updated:")
e.checkNextLine(t, "^\\s*$")
@ -74,7 +74,7 @@ func TestNEP17Balance(t *testing.T) {
if strings.Contains(line, "GAS") {
e.checkLine(t, line, "^\\s*GAS:\\s+GAS \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)")
balance = e.Chain.GetUtilityTokenBalance(addr3)
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+util.Fixed8(balance.Int64()).String())
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String())
e.checkNextLine(t, "^\\s*Updated:")
} else {
balance, index := e.Chain.GetGoverningTokenBalance(validatorHash)
@ -156,7 +156,7 @@ func TestNEP17MultiTransfer(t *testing.T) {
b, _ := e.Chain.GetGoverningTokenBalance(privs[0].GetScriptHash())
require.Equal(t, big.NewInt(42), b)
b = e.Chain.GetUtilityTokenBalance(privs[1].GetScriptHash())
require.Equal(t, big.NewInt(int64(util.Fixed8FromInt64(7))), b)
require.Equal(t, big.NewInt(int64(fixedn.Fixed8FromInt64(7))), b)
b, _ = e.Chain.GetGoverningTokenBalance(privs[2].GetScriptHash())
require.Equal(t, big.NewInt(13), b)
}

View file

@ -19,6 +19,7 @@ import (
"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/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -446,7 +447,7 @@ func invokeFunction(ctx *cli.Context) error {
func invokeInternal(ctx *cli.Context, signAndPush bool) error {
var (
err error
gas util.Fixed8
gas fixedn.Fixed8
operation string
params = make([]smartcontract.Parameter, 0)
paramsStart = 1

View file

@ -9,6 +9,7 @@ import (
"github.com/nspcc-dev/neo-go/cli/options"
"github.com/nspcc-dev/neo-go/cli/paramcontext"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
@ -390,7 +391,7 @@ func multiTransferNEP17(ctx *cli.Context) error {
if err != nil {
return cli.NewExitError(fmt.Errorf("invalid address: '%s'", ss[1]), 1)
}
amount, err := util.FixedNFromString(ss[2], int(token.Decimals))
amount, err := fixedn.FixedNFromString(ss[2], int(token.Decimals))
if err != nil {
return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1)
}
@ -437,7 +438,7 @@ func transferNEP17(ctx *cli.Context) error {
}
}
amount, err := util.FixedNFromString(ctx.String("amount"), int(token.Decimals))
amount, err := fixedn.FixedNFromString(ctx.String("amount"), int(token.Decimals))
if err != nil {
return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1)
}

View file

@ -22,6 +22,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
@ -198,7 +199,7 @@ func TestCreateBasicChain(t *testing.T) {
acc0 := wallet.NewAccountFromPrivateKey(priv0)
// Prepare some transaction for future submission.
txSendRaw := newNEP17Transfer(bc.contracts.NEO.Hash, priv0ScriptHash, priv1.GetScriptHash(), int64(util.Fixed8FromInt64(1000)))
txSendRaw := newNEP17Transfer(bc.contracts.NEO.Hash, priv0ScriptHash, priv1.GetScriptHash(), int64(fixedn.Fixed8FromInt64(1000)))
txSendRaw.ValidUntilBlock = transaction.MaxValidUntilBlockIncrement
txSendRaw.Nonce = 0x1234
txSendRaw.Signers = []transaction.Signer{{
@ -245,7 +246,7 @@ func initBasicChain(t *testing.T, bc *Blockchain) {
txMoveNeo, err := testchain.NewTransferFromOwner(bc, neoHash, priv0ScriptHash, neoAmount, getNextNonce(), validUntilBlock)
require.NoError(t, err)
// Move some GAS to one simple account.
txMoveGas, err := testchain.NewTransferFromOwner(bc, gasHash, priv0ScriptHash, int64(util.Fixed8FromInt64(1000)),
txMoveGas, err := testchain.NewTransferFromOwner(bc, gasHash, priv0ScriptHash, int64(fixedn.Fixed8FromInt64(1000)),
getNextNonce(), validUntilBlock)
require.NoError(t, err)

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -173,7 +174,7 @@ type Execution struct {
type executionAux struct {
Trigger string `json:"trigger"`
VMState string `json:"vmstate"`
GasConsumed util.Fixed8 `json:"gasconsumed,string"`
GasConsumed fixedn.Fixed8 `json:"gasconsumed,string"`
Stack json.RawMessage `json:"stack"`
Events []NotificationEvent `json:"notifications"`
FaultException string `json:"exception,omitempty"`
@ -202,7 +203,7 @@ func (e Execution) MarshalJSON() ([]byte, error) {
return json.Marshal(&executionAux{
Trigger: e.Trigger.String(),
VMState: e.VMState.String(),
GasConsumed: util.Fixed8(e.GasConsumed),
GasConsumed: fixedn.Fixed8(e.GasConsumed),
Stack: st,
Events: e.Events,
FaultException: e.FaultException,

View file

@ -10,6 +10,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
)
@ -306,8 +307,8 @@ type transactionJSON struct {
Version uint8 `json:"version"`
Nonce uint32 `json:"nonce"`
Sender string `json:"sender"`
SystemFee util.Fixed8 `json:"sysfee,string"`
NetworkFee util.Fixed8 `json:"netfee,string"`
SystemFee fixedn.Fixed8 `json:"sysfee,string"`
NetworkFee fixedn.Fixed8 `json:"netfee,string"`
ValidUntilBlock uint32 `json:"validuntilblock"`
Attributes []Attribute `json:"attributes"`
Signers []Signer `json:"signers"`
@ -328,8 +329,8 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {
Signers: t.Signers,
Script: t.Script,
Scripts: t.Scripts,
SystemFee: util.Fixed8(t.SystemFee),
NetworkFee: util.Fixed8(t.NetworkFee),
SystemFee: fixedn.Fixed8(t.SystemFee),
NetworkFee: fixedn.Fixed8(t.NetworkFee),
}
return json.Marshal(tx)
}

View file

@ -7,6 +7,7 @@ import (
"math"
"testing"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -119,8 +120,8 @@ func TestMarshalUnmarshalJSONInvocationTX(t *testing.T) {
Script: []byte{1, 2, 3, 4},
Attributes: []Attribute{{Type: HighPriority}},
Scripts: []Witness{},
SystemFee: int64(util.Fixed8FromFloat(123.45)),
NetworkFee: int64(util.Fixed8FromFloat(0.123)),
SystemFee: int64(fixedn.Fixed8FromFloat(123.45)),
NetworkFee: int64(fixedn.Fixed8FromFloat(0.123)),
Trimmed: false,
}

View file

@ -1,4 +1,4 @@
package util
package fixedn
import (
"encoding/json"

View file

@ -1,4 +1,4 @@
package util
package fixedn
import (
"encoding/json"

View file

@ -13,6 +13,7 @@ import (
"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/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
@ -170,10 +171,10 @@ func (c *Client) GetBlockHeaderVerbose(hash util.Uint256) (*result.Header, error
}
// GetBlockSysFee returns the system fees of the block, based on the specified index.
func (c *Client) GetBlockSysFee(index uint32) (util.Fixed8, error) {
func (c *Client) GetBlockSysFee(index uint32) (fixedn.Fixed8, error) {
var (
params = request.NewRawParams(index)
resp util.Fixed8
resp fixedn.Fixed8
)
if err := c.performRequest("getblocksysfee", params, &resp); err != nil {
return resp, err
@ -473,7 +474,7 @@ func (c *Client) SubmitBlock(b block.Block) (util.Uint256, error) {
// SignAndPushInvocationTx signs and pushes given script as an invocation
// transaction using given wif to sign it and spending the amount of gas
// specified. It returns a hash of the invocation transaction and an error.
func (c *Client) SignAndPushInvocationTx(script []byte, acc *wallet.Account, sysfee int64, netfee util.Fixed8, cosigners []transaction.Signer) (util.Uint256, error) {
func (c *Client) SignAndPushInvocationTx(script []byte, acc *wallet.Account, sysfee int64, netfee fixedn.Fixed8, cosigners []transaction.Signer) (util.Uint256, error) {
var txHash util.Uint256
var err error

View file

@ -22,6 +22,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -285,7 +286,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
},
serverResponse: `{"jsonrpc":"2.0","id":1,"result":"195500"}`,
result: func(c *Client) interface{} {
return util.Fixed8FromInt64(195500)
return fixedn.Fixed8FromInt64(195500)
},
},
},

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
@ -26,7 +26,7 @@ type Invoke struct {
type invokeAux struct {
State string `json:"state"`
GasConsumed util.Fixed8 `json:"gasconsumed,string"`
GasConsumed fixedn.Fixed8 `json:"gasconsumed,string"`
Script []byte `json:"script"`
Stack json.RawMessage `json:"stack"`
FaultException string `json:"exception,omitempty"`
@ -55,7 +55,7 @@ func (r Invoke) MarshalJSON() ([]byte, error) {
}
return json.Marshal(&invokeAux{
GasConsumed: util.Fixed8(r.GasConsumed),
GasConsumed: fixedn.Fixed8(r.GasConsumed),
Script: r.Script,
State: r.State,
Stack: st,

View file

@ -6,7 +6,7 @@ import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
@ -14,7 +14,7 @@ import (
func TestInvoke_MarshalJSON(t *testing.T) {
result := &Invoke{
State: "HALT",
GasConsumed: int64(util.Fixed8FromFloat(123.45)),
GasConsumed: int64(fixedn.Fixed8FromFloat(123.45)),
Script: []byte{10},
Stack: []stackitem.Item{stackitem.NewBigInteger(big.NewInt(1))},
FaultException: "",

View file

@ -1,6 +1,8 @@
package rpc
import "github.com/nspcc-dev/neo-go/pkg/util"
import (
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
)
type (
// Config is an RPC service configuration information
@ -10,7 +12,7 @@ type (
EnableCORSWorkaround bool `yaml:"EnableCORSWorkaround"`
// MaxGasInvoke is a maximum amount of gas which
// can be spent during RPC call.
MaxGasInvoke util.Fixed8 `yaml:"MaxGasInvoke"`
MaxGasInvoke fixedn.Fixed8 `yaml:"MaxGasInvoke"`
Port uint16 `yaml:"Port"`
TLSConfig TLSConfig `yaml:"TLSConfig"`
}