Skip to content

Commit

Permalink
Merge pull request #5649 from multiversx/remove_sendtxrequest
Browse files Browse the repository at this point in the history
Replace SendTxRequest with FrontendTransaction
  • Loading branch information
sstanculeanu authored Oct 16, 2023
2 parents 9ddf388 + 16e5d20 commit 46f6022
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 111 deletions.
145 changes: 55 additions & 90 deletions api/groups/transactionGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package groups
import (
"encoding/hex"
"fmt"
"math/big"
"net/http"
"strconv"
"sync"
Expand Down Expand Up @@ -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"`
Expand All @@ -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(&gtx)
var ftx = transaction.FrontendTransaction{}
err := c.ShouldBindJSON(&ftx)
if err != nil {
c.JSON(
http.StatusBadRequest,
Expand All @@ -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)
Expand Down Expand Up @@ -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(&gtx)
var ftx = transaction.FrontendTransaction{}
err := c.ShouldBindJSON(&ftx)
if err != nil {
c.JSON(
http.StatusBadRequest,
Expand All @@ -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)
Expand Down Expand Up @@ -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(&gtx)
var ftxs []transaction.FrontendTransaction
err := c.ShouldBindJSON(&ftxs)
if err != nil {
c.JSON(
http.StatusBadRequest,
Expand All @@ -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,
Expand Down Expand Up @@ -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(&gtx)
var ftx transaction.FrontendTransaction
err := c.ShouldBindJSON(&ftx)
if err != nil {
c.JSON(
http.StatusBadRequest,
Expand All @@ -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)
Expand Down
38 changes: 19 additions & 19 deletions api/groups/transactionGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -258,7 +258,7 @@ func TestTransactionGroup_sendTransaction(t *testing.T) {
facade,
"/transaction/send",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusBadRequest,
expectedErr,
)
Expand All @@ -283,7 +283,7 @@ func TestTransactionGroup_sendTransaction(t *testing.T) {
facade,
"/transaction/send",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusBadRequest,
expectedErr,
)
Expand All @@ -307,7 +307,7 @@ func TestTransactionGroup_sendTransaction(t *testing.T) {
facade,
"/transaction/send",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusInternalServerError,
expectedErr,
)
Expand Down Expand Up @@ -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()
Expand All @@ -368,7 +368,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) {
facade,
"/transaction/send-multiple",
"POST",
[]*groups.SendTxRequest{{}},
[]*dataTx.FrontendTransaction{{}},
http.StatusInternalServerError,
expectedErr,
)
Expand All @@ -393,7 +393,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) {
facade,
"/transaction/send-multiple",
"POST",
[]*groups.SendTxRequest{{}},
[]*dataTx.FrontendTransaction{{}},
http.StatusInternalServerError,
expectedErr,
)
Expand All @@ -418,7 +418,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) {
facade,
"/transaction/send-multiple",
"POST",
[]*groups.SendTxRequest{{}},
[]*dataTx.FrontendTransaction{{}},
http.StatusInternalServerError,
expectedErr,
)
Expand All @@ -443,7 +443,7 @@ func TestTransactionGroup_sendMultipleTransactions(t *testing.T) {
},
}

tx0 := groups.SendTxRequest{
tx0 := dataTx.FrontendTransaction{
Sender: "sender1",
Receiver: "receiver1",
Value: "100",
Expand All @@ -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)

Expand Down Expand Up @@ -494,7 +494,7 @@ func TestTransactionGroup_computeTransactionGasLimit(t *testing.T) {
facade,
"/transaction/cost",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusInternalServerError,
expectedErr,
)
Expand All @@ -515,7 +515,7 @@ func TestTransactionGroup_computeTransactionGasLimit(t *testing.T) {
facade,
"/transaction/cost",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusInternalServerError,
expectedErr,
)
Expand All @@ -537,7 +537,7 @@ func TestTransactionGroup_computeTransactionGasLimit(t *testing.T) {
},
}

tx0 := groups.SendTxRequest{
tx0 := dataTx.FrontendTransaction{
Sender: "sender1",
Receiver: "receiver1",
Value: "100",
Expand Down Expand Up @@ -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()

Expand All @@ -586,7 +586,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) {
facade,
"/transaction/simulate",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusBadRequest,
expectedErr,
)
Expand All @@ -611,7 +611,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) {
facade,
"/transaction/simulate",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusBadRequest,
expectedErr,
)
Expand All @@ -635,7 +635,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) {
facade,
"/transaction/simulate",
"POST",
&groups.SendTxRequest{},
&dataTx.FrontendTransaction{},
http.StatusInternalServerError,
expectedErr,
)
Expand Down Expand Up @@ -666,7 +666,7 @@ func TestTransactionGroup_simulateTransaction(t *testing.T) {
},
}

tx := groups.SendTxRequest{
tx := dataTx.FrontendTransaction{
Sender: "sender1",
Receiver: "receiver1",
Value: "100",
Expand Down
4 changes: 2 additions & 2 deletions integrationTests/api/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down

0 comments on commit 46f6022

Please sign in to comment.