From bba8ac15ff3598203e006540f11e67d089c67403 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 20 Feb 2020 19:29:21 +0300 Subject: [PATCH] rpc: introduce proper type for sendrawtransaction, drop useless types sendrawtransaction just returns a bool, sendtoaddress returns a proper transaction and that should be the same as the one we have in TransactionOutputRaw. --- pkg/rpc/client/rpc.go | 24 ++++++++++------------ pkg/rpc/response/types.go | 43 ++++++--------------------------------- 2 files changed, 17 insertions(+), 50 deletions(-) diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index e7c135c69..d4e2c1e0b 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -112,10 +112,10 @@ func (c *Client) Invoke(script string, params []smartcontract.Parameter) (*respo // The given hex string needs to be signed with a keypair. // When the result of the response object is true, the TX has successfully // been broadcasted to the network. -func (c *Client) sendRawTransaction(rawTX *transaction.Transaction) (*response.Raw, error) { +func (c *Client) sendRawTransaction(rawTX *transaction.Transaction) (*response.SendRawTx, error) { var ( params = request.NewRawParams(hex.EncodeToString(rawTX.Bytes())) - resp = &response.Raw{} + resp = &response.SendRawTx{} ) if err := c.performRequest("sendrawtransaction", params, resp); err != nil { return nil, err @@ -126,7 +126,7 @@ func (c *Client) sendRawTransaction(rawTX *transaction.Transaction) (*response.R // SendToAddress sends an amount of specific asset to a given address. // This call requires open wallet. (`wif` key in client struct.) // If response.Result is `true` then transaction was formed correctly and was written in blockchain. -func (c *Client) SendToAddress(asset util.Uint256, address string, amount util.Fixed8) (*response.SendToAddress, error) { +func (c *Client) SendToAddress(asset util.Uint256, address string, amount util.Fixed8) (util.Uint256, error) { var ( err error rawTx *transaction.Transaction @@ -137,23 +137,21 @@ func (c *Client) SendToAddress(asset util.Uint256, address string, amount util.F WIF: c.WIF(), Balancer: c.Balancer(), } - respRaw *response.Raw - resp = &response.SendToAddress{} + respRaw *response.SendRawTx + resp = util.Uint256{} ) if rawTx, err = request.CreateRawContractTransaction(txParams); err != nil { - return nil, errors.Wrap(err, "failed to create raw transaction for `sendtoaddress`") + return resp, errors.Wrap(err, "failed to create raw transaction for `sendtoaddress`") } if respRaw, err = c.sendRawTransaction(rawTx); err != nil { - return nil, errors.Wrap(err, "failed to send raw transaction") + return resp, errors.Wrap(err, "failed to send raw transaction") } - resp.Error = respRaw.Error - resp.ID = respRaw.ID - resp.JSONRPC = respRaw.JSONRPC - resp.Result = &response.Tx{ - TxID: rawTx.Hash().StringLE(), + if respRaw.Result { + return rawTx.Hash(), nil + } else { + return resp, errors.New("failed to send raw transaction") } - return resp, nil } // SignAndPushInvocationTx signs and pushes given script as an invocation diff --git a/pkg/rpc/response/types.go b/pkg/rpc/response/types.go index 5b1e3ad79..917bed066 100644 --- a/pkg/rpc/response/types.go +++ b/pkg/rpc/response/types.go @@ -3,7 +3,6 @@ package response import ( "encoding/json" - "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/rpc/request" "github.com/CityOfZion/neo-go/pkg/rpc/response/result" "github.com/CityOfZion/neo-go/pkg/vm" @@ -59,7 +58,7 @@ type Raw struct { // SendToAddress stores response for the sendtoaddress call. type SendToAddress struct { HeaderAndError - Result *Tx + Result *result.TransactionOutputRaw } // GetTxOut represents result of `gettxout` RPC call. @@ -71,41 +70,11 @@ type GetTxOut struct { // GetRawTx represents verbose output of `getrawtransaction` RPC call. type GetRawTx struct { HeaderAndError - Result *RawTx `json:"result"` + Result *result.TransactionOutputRaw `json:"result"` } -// RawTx stores transaction with blockchain metadata to be sent as a response. -type RawTx struct { - Tx - BlockHash string `json:"blockhash"` - Confirmations uint `json:"confirmations"` - BlockTime uint `json:"blocktime"` -} - -// Tx stores transaction to be sent as a response. -type Tx struct { - TxID string `json:"txid"` - Size int `json:"size"` - Type string `json:"type"` // todo: convert to TransactionType - Version int `json:"version"` - Attributes []transaction.Attribute `json:"attributes"` - Vins []Vin `json:"vin"` - Vouts []Vout `json:"vout"` - SysFee int `json:"sys_fee"` - NetFee int `json:"net_fee"` - Scripts []transaction.Witness `json:"scripts"` -} - -// Vin represents JSON-serializable tx input. -type Vin struct { - TxID string `json:"txid"` - Vout int `json:"vout"` -} - -// Vout represents JSON-serializable tx output. -type Vout struct { - N int `json:"n"` - Asset string `json:"asset"` - Value int `json:"value"` - Address string `json:"address"` +// SendRawTx represents a `sendrawtransaction` RPC call response. +type SendRawTx struct { + HeaderAndError + Result bool `json:"result"` }