From aea836a5c34041a7a414f01dfa115e40a91331b6 Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Tue, 10 Oct 2023 11:01:17 +0300 Subject: [PATCH] replaced SendTxRequest with FrontendTransaction --- api/groups/transactionGroup.go | 145 +++++++++-------------- api/groups/transactionGroup_test.go | 38 +++--- integrationTests/api/transaction_test.go | 4 +- 3 files changed, 76 insertions(+), 111 deletions(-) diff --git a/api/groups/transactionGroup.go b/api/groups/transactionGroup.go index 26567186343..c2b47bf7a87 100644 --- a/api/groups/transactionGroup.go +++ b/api/groups/transactionGroup.go @@ -3,7 +3,6 @@ package groups import ( "encoding/hex" "fmt" - "math/big" "net/http" "strconv" "sync" @@ -144,43 +143,9 @@ func NewTransactionGroup(facade transactionFacadeHandler) (*transactionGroup, er return tg, nil } -// TxRequest represents the structure on which user input for generating a new transaction will validate against -type TxRequest struct { - Sender string `form:"sender" json:"sender"` - Receiver string `form:"receiver" json:"receiver"` - Value *big.Int `form:"value" json:"value"` - Data string `form:"data" json:"data"` -} - -// MultipleTxRequest represents the structure on which user input for generating a bulk of transactions will validate against -type MultipleTxRequest struct { - Receiver string `form:"receiver" json:"receiver"` - Value *big.Int `form:"value" json:"value"` - TxCount int `form:"txCount" json:"txCount"` -} - -// SendTxRequest represents the structure that maps and validates user input for publishing a new transaction -type SendTxRequest struct { - Sender string `form:"sender" json:"sender"` - Receiver string `form:"receiver" json:"receiver"` - SenderUsername []byte `json:"senderUsername,omitempty"` - ReceiverUsername []byte `json:"receiverUsername,omitempty"` - Value string `form:"value" json:"value"` - Data []byte `form:"data" json:"data"` - Nonce uint64 `form:"nonce" json:"nonce"` - GasPrice uint64 `form:"gasPrice" json:"gasPrice"` - GasLimit uint64 `form:"gasLimit" json:"gasLimit"` - Signature string `form:"signature" json:"signature"` - ChainID string `form:"chainID" json:"chainID"` - Version uint32 `form:"version" json:"version"` - Options uint32 `json:"options,omitempty"` - GuardianAddr string `json:"guardian,omitempty"` - GuardianSignature string `json:"guardianSignature,omitempty"` -} - // TxResponse represents the structure on which the response will be validated against type TxResponse struct { - SendTxRequest + transaction.FrontendTransaction ShardID uint32 `json:"shardId"` Hash string `json:"hash"` BlockNumber uint64 `json:"blockNumber"` @@ -190,8 +155,8 @@ type TxResponse struct { // simulateTransaction will receive a transaction from the client and will simulate its execution and return the results func (tg *transactionGroup) simulateTransaction(c *gin.Context) { - var gtx = SendTxRequest{} - err := c.ShouldBindJSON(>x) + var ftx = transaction.FrontendTransaction{} + err := c.ShouldBindJSON(&ftx) if err != nil { c.JSON( http.StatusBadRequest, @@ -218,21 +183,21 @@ func (tg *transactionGroup) simulateTransaction(c *gin.Context) { } txArgs := &external.ArgsCreateTransaction{ - Nonce: gtx.Nonce, - Value: gtx.Value, - Receiver: gtx.Receiver, - ReceiverUsername: gtx.ReceiverUsername, - Sender: gtx.Sender, - SenderUsername: gtx.SenderUsername, - GasPrice: gtx.GasPrice, - GasLimit: gtx.GasLimit, - DataField: gtx.Data, - SignatureHex: gtx.Signature, - ChainID: gtx.ChainID, - Version: gtx.Version, - Options: gtx.Options, - Guardian: gtx.GuardianAddr, - GuardianSigHex: gtx.GuardianSignature, + Nonce: ftx.Nonce, + Value: ftx.Value, + Receiver: ftx.Receiver, + ReceiverUsername: ftx.ReceiverUsername, + Sender: ftx.Sender, + SenderUsername: ftx.SenderUsername, + GasPrice: ftx.GasPrice, + GasLimit: ftx.GasLimit, + DataField: ftx.Data, + SignatureHex: ftx.Signature, + ChainID: ftx.ChainID, + Version: ftx.Version, + Options: ftx.Options, + Guardian: ftx.GuardianAddr, + GuardianSigHex: ftx.GuardianSignature, } start := time.Now() tx, txHash, err := tg.getFacade().CreateTransaction(txArgs) @@ -293,8 +258,8 @@ func (tg *transactionGroup) simulateTransaction(c *gin.Context) { // sendTransaction will receive a transaction from the client and propagate it for processing func (tg *transactionGroup) sendTransaction(c *gin.Context) { - var gtx = SendTxRequest{} - err := c.ShouldBindJSON(>x) + var ftx = transaction.FrontendTransaction{} + err := c.ShouldBindJSON(&ftx) if err != nil { c.JSON( http.StatusBadRequest, @@ -308,21 +273,21 @@ func (tg *transactionGroup) sendTransaction(c *gin.Context) { } txArgs := &external.ArgsCreateTransaction{ - Nonce: gtx.Nonce, - Value: gtx.Value, - Receiver: gtx.Receiver, - ReceiverUsername: gtx.ReceiverUsername, - Sender: gtx.Sender, - SenderUsername: gtx.SenderUsername, - GasPrice: gtx.GasPrice, - GasLimit: gtx.GasLimit, - DataField: gtx.Data, - SignatureHex: gtx.Signature, - ChainID: gtx.ChainID, - Version: gtx.Version, - Options: gtx.Options, - Guardian: gtx.GuardianAddr, - GuardianSigHex: gtx.GuardianSignature, + Nonce: ftx.Nonce, + Value: ftx.Value, + Receiver: ftx.Receiver, + ReceiverUsername: ftx.ReceiverUsername, + Sender: ftx.Sender, + SenderUsername: ftx.SenderUsername, + GasPrice: ftx.GasPrice, + GasLimit: ftx.GasLimit, + DataField: ftx.Data, + SignatureHex: ftx.Signature, + ChainID: ftx.ChainID, + Version: ftx.Version, + Options: ftx.Options, + Guardian: ftx.GuardianAddr, + GuardianSigHex: ftx.GuardianSignature, } start := time.Now() tx, txHash, err := tg.getFacade().CreateTransaction(txArgs) @@ -382,8 +347,8 @@ func (tg *transactionGroup) sendTransaction(c *gin.Context) { // sendMultipleTransactions will receive a number of transactions and will propagate them for processing func (tg *transactionGroup) sendMultipleTransactions(c *gin.Context) { - var gtx []SendTxRequest - err := c.ShouldBindJSON(>x) + var ftxs []transaction.FrontendTransaction + err := c.ShouldBindJSON(&ftxs) if err != nil { c.JSON( http.StatusBadRequest, @@ -404,7 +369,7 @@ func (tg *transactionGroup) sendMultipleTransactions(c *gin.Context) { var start time.Time txsHashes := make(map[int]string) - for idx, receivedTx := range gtx { + for idx, receivedTx := range ftxs { txArgs := &external.ArgsCreateTransaction{ Nonce: receivedTx.Nonce, Value: receivedTx.Value, @@ -520,8 +485,8 @@ func (tg *transactionGroup) getTransaction(c *gin.Context) { // computeTransactionGasLimit returns how many gas units a transaction wil consume func (tg *transactionGroup) computeTransactionGasLimit(c *gin.Context) { - var gtx SendTxRequest - err := c.ShouldBindJSON(>x) + var ftx transaction.FrontendTransaction + err := c.ShouldBindJSON(&ftx) if err != nil { c.JSON( http.StatusBadRequest, @@ -535,21 +500,21 @@ func (tg *transactionGroup) computeTransactionGasLimit(c *gin.Context) { } txArgs := &external.ArgsCreateTransaction{ - Nonce: gtx.Nonce, - Value: gtx.Value, - Receiver: gtx.Receiver, - ReceiverUsername: gtx.ReceiverUsername, - Sender: gtx.Sender, - SenderUsername: gtx.SenderUsername, - GasPrice: gtx.GasPrice, - GasLimit: gtx.GasLimit, - DataField: gtx.Data, - SignatureHex: gtx.Signature, - ChainID: gtx.ChainID, - Version: gtx.Version, - Options: gtx.Options, - Guardian: gtx.GuardianAddr, - GuardianSigHex: gtx.GuardianSignature, + Nonce: ftx.Nonce, + Value: ftx.Value, + Receiver: ftx.Receiver, + ReceiverUsername: ftx.ReceiverUsername, + Sender: ftx.Sender, + SenderUsername: ftx.SenderUsername, + GasPrice: ftx.GasPrice, + GasLimit: ftx.GasLimit, + DataField: ftx.Data, + SignatureHex: ftx.Signature, + ChainID: ftx.ChainID, + Version: ftx.Version, + Options: ftx.Options, + Guardian: ftx.GuardianAddr, + GuardianSigHex: ftx.GuardianSignature, } start := time.Now() tx, _, err := tg.getFacade().CreateTransaction(txArgs) diff --git a/api/groups/transactionGroup_test.go b/api/groups/transactionGroup_test.go index 43433a8b943..1f8f6bffbd4 100644 --- a/api/groups/transactionGroup_test.go +++ b/api/groups/transactionGroup_test.go @@ -239,7 +239,7 @@ func TestTransactionsGroup_getTransaction(t *testing.T) { func TestTransactionGroup_sendTransaction(t *testing.T) { t.Parallel() - t.Run("number of go routines exceeded", testExceededNumGoRoutines("/transaction/send", &groups.SendTxRequest{})) + t.Run("number of go routines exceeded", testExceededNumGoRoutines("/transaction/send", &dataTx.FrontendTransaction{})) t.Run("invalid params should error", testTransactionGroupErrorScenario("/transaction/send", "POST", jsonTxStr, http.StatusBadRequest, apiErrors.ErrValidation)) t.Run("CreateTransaction error should error", func(t *testing.T) { t.Parallel() @@ -258,7 +258,7 @@ func TestTransactionGroup_sendTransaction(t *testing.T) { facade, "/transaction/send", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusBadRequest, expectedErr, ) @@ -283,7 +283,7 @@ func TestTransactionGroup_sendTransaction(t *testing.T) { facade, "/transaction/send", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusBadRequest, expectedErr, ) @@ -307,7 +307,7 @@ func TestTransactionGroup_sendTransaction(t *testing.T) { facade, "/transaction/send", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusInternalServerError, expectedErr, ) @@ -345,7 +345,7 @@ func TestTransactionGroup_sendTransaction(t *testing.T) { func TestTransactionGroup_sendMultipleTransactions(t *testing.T) { t.Parallel() - t.Run("number of go routines exceeded", testExceededNumGoRoutines("/transaction/send-multiple", &groups.SendTxRequest{})) + t.Run("number of go routines exceeded", testExceededNumGoRoutines("/transaction/send-multiple", &dataTx.FrontendTransaction{})) t.Run("invalid params should error", testTransactionGroupErrorScenario("/transaction/send-multiple", "POST", jsonTxStr, http.StatusBadRequest, apiErrors.ErrValidation)) t.Run("CreateTransaction error should continue, error on SendBulkTransactions", func(t *testing.T) { t.Parallel() @@ -368,7 +368,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) { facade, "/transaction/send-multiple", "POST", - []*groups.SendTxRequest{{}}, + []*dataTx.FrontendTransaction{{}}, http.StatusInternalServerError, expectedErr, ) @@ -393,7 +393,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) { facade, "/transaction/send-multiple", "POST", - []*groups.SendTxRequest{{}}, + []*dataTx.FrontendTransaction{{}}, http.StatusInternalServerError, expectedErr, ) @@ -418,7 +418,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) { facade, "/transaction/send-multiple", "POST", - []*groups.SendTxRequest{{}}, + []*dataTx.FrontendTransaction{{}}, http.StatusInternalServerError, expectedErr, ) @@ -443,7 +443,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) { }, } - tx0 := groups.SendTxRequest{ + tx0 := dataTx.FrontendTransaction{ Sender: "sender1", Receiver: "receiver1", Value: "100", @@ -455,7 +455,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) { } tx1 := tx0 tx1.Sender = "sender2" - txs := []*groups.SendTxRequest{&tx0, &tx1} + txs := []*dataTx.FrontendTransaction{&tx0, &tx1} jsonBytes, _ := json.Marshal(txs) @@ -494,7 +494,7 @@ func TestTransactionGroup_computeTransactionGasLimit(t *testing.T) { facade, "/transaction/cost", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusInternalServerError, expectedErr, ) @@ -515,7 +515,7 @@ func TestTransactionGroup_computeTransactionGasLimit(t *testing.T) { facade, "/transaction/cost", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusInternalServerError, expectedErr, ) @@ -537,7 +537,7 @@ func TestTransactionGroup_computeTransactionGasLimit(t *testing.T) { }, } - tx0 := groups.SendTxRequest{ + tx0 := dataTx.FrontendTransaction{ Sender: "sender1", Receiver: "receiver1", Value: "100", @@ -566,9 +566,9 @@ func TestTransactionGroup_computeTransactionGasLimit(t *testing.T) { func TestTransactionGroup_simulateTransaction(t *testing.T) { t.Parallel() - t.Run("number of go routines exceeded", testExceededNumGoRoutines("/transaction/simulate", &groups.SendTxRequest{})) + t.Run("number of go routines exceeded", testExceededNumGoRoutines("/transaction/simulate", &dataTx.FrontendTransaction{})) t.Run("invalid param transaction should error", testTransactionGroupErrorScenario("/transaction/simulate", "POST", jsonTxStr, http.StatusBadRequest, apiErrors.ErrValidation)) - t.Run("invalid param checkSignature should error", testTransactionGroupErrorScenario("/transaction/simulate?checkSignature=not-bool", "POST", &groups.SendTxRequest{}, http.StatusBadRequest, apiErrors.ErrValidation)) + t.Run("invalid param checkSignature should error", testTransactionGroupErrorScenario("/transaction/simulate?checkSignature=not-bool", "POST", &dataTx.FrontendTransaction{}, http.StatusBadRequest, apiErrors.ErrValidation)) t.Run("CreateTransaction error should error", func(t *testing.T) { t.Parallel() @@ -586,7 +586,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) { facade, "/transaction/simulate", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusBadRequest, expectedErr, ) @@ -611,7 +611,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) { facade, "/transaction/simulate", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusBadRequest, expectedErr, ) @@ -635,7 +635,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) { facade, "/transaction/simulate", "POST", - &groups.SendTxRequest{}, + &dataTx.FrontendTransaction{}, http.StatusInternalServerError, expectedErr, ) @@ -666,7 +666,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) { }, } - tx := groups.SendTxRequest{ + tx := dataTx.FrontendTransaction{ Sender: "sender1", Receiver: "receiver1", Value: "100", diff --git a/integrationTests/api/transaction_test.go b/integrationTests/api/transaction_test.go index 22434ba37c0..c4267676343 100644 --- a/integrationTests/api/transaction_test.go +++ b/integrationTests/api/transaction_test.go @@ -7,7 +7,7 @@ import ( "net/http" "testing" - "github.com/multiversx/mx-chain-go/api/groups" + "github.com/multiversx/mx-chain-core-go/data/transaction" "github.com/multiversx/mx-chain-go/integrationTests" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -21,7 +21,7 @@ func TestTransactionGroup(t *testing.T) { func testTransactionGasCostWithMissingFields(tb testing.TB, node *integrationTests.TestProcessorNodeWithTestWebServer) { // this is an example found in the wild, should not add more fields in order to pass the tests - tx := groups.SendTxRequest{ + tx := transaction.FrontendTransaction{ Sender: "erd1vxy22x0fj4zv6hktmydg8vpfh6euv02cz4yg0aaws6rrad5a5awqgqky80", Receiver: "erd188anxz35atlef7cucszypmvx88lhz4m7a7t7lhcwt6sfphpsqlkswfhcx2", Value: "100",