Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added transaction processed status #386

Merged
merged 13 commits into from
Jun 26, 2023
17 changes: 17 additions & 0 deletions api/groups/baseTransactionGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewTransactionGroup(facadeHandler data.FacadeHandler) (*transactionGroup, e
{Path: "/send-user-funds", Handler: tg.sendUserFunds, Method: http.MethodPost},
{Path: "/cost", Handler: tg.requestTransactionCost, Method: http.MethodPost},
{Path: "/:txhash/status", Handler: tg.getTransactionStatus, Method: http.MethodGet},
{Path: "/:txhash/process-status", Handler: tg.getProcessStatus, Method: http.MethodGet},
{Path: "/:txhash", Handler: tg.getTransaction, Method: http.MethodGet},
{Path: "/pool", Handler: tg.getTransactionsPool, Method: http.MethodGet},
}
Expand Down Expand Up @@ -247,6 +248,22 @@ func (group *transactionGroup) getTransaction(c *gin.Context) {
shared.RespondWith(c, http.StatusOK, gin.H{"transaction": tx}, "", data.ReturnCodeSuccess)
}

func (group *transactionGroup) getProcessStatus(c *gin.Context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

processingStatus vs. processStatus

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I think it's better getProcessedTransactionStatus to align with the facade

txHash := c.Param("txhash")
if txHash == "" {
shared.RespondWith(c, http.StatusBadRequest, nil, errors.ErrTransactionHashMissing.Error(), data.ReturnCodeRequestError)
return
}

status, err := group.facade.GetProcessedTransactionStatus(txHash)
if err != nil {
shared.RespondWith(c, http.StatusInternalServerError, nil, err.Error(), data.ReturnCodeInternalError)
return
}

shared.RespondWith(c, http.StatusOK, gin.H{"transaction": status}, "", data.ReturnCodeSuccess)
}

func getTransactionByHashAndSenderAddress(c *gin.Context, ef TransactionFacadeHandler, txHash string, sndAddr string, withEvents bool) {
tx, statusCode, err := ef.GetTransactionByHashAndSenderAddress(txHash, sndAddr, withEvents)
if err != nil {
Expand Down
82 changes: 82 additions & 0 deletions api/groups/baseTransactionGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ type nonceGapsResp struct {
Data nonceGaps
}

type txProcessedStatusResp struct {
GeneralResponse
Data struct {
TransactionHash string `json:"transaction"`
} `json:"data"`
}

func TestNewTransactionGroup_WrongFacadeShouldErr(t *testing.T) {
wrongFacade := &mock.WrongFacade{}
group, err := groups.NewTransactionGroup(wrongFacade)
Expand Down Expand Up @@ -741,3 +748,78 @@ func TestGetTransactionsPoolPoolNonceGapsForSender_ReturnsSuccessfully(t *testin
assert.Equal(t, response.Error, "")
assert.Equal(t, providedNonceGaps, &response.Data.NonceGaps)
}

func TestTransactionGroup_getProcessStatus(t *testing.T) {
t.Parallel()

expectedErr := errors.New("expected error")
hash := "hash"
t.Run("no tx hash provided, should error", func(t *testing.T) {
t.Parallel()

transactionsGroup, err := groups.NewTransactionGroup(&mock.FacadeStub{})
require.NoError(t, err)
ws := startProxyServer(transactionsGroup, transactionsPath)

req, _ := http.NewRequest("GET", "/transaction//process-status", nil)

resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := GeneralResponse{}
loadResponse(resp.Body, &response)

assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.Equal(t, apiErrors.ErrTransactionHashMissing.Error(), response.Error)
})
t.Run("GetProcessedTransactionStatus errors, should error", func(t *testing.T) {
t.Parallel()

facade := &mock.FacadeStub{
GetProcessedTransactionStatusHandler: func(txHash string) (string, error) {
assert.Equal(t, hash, txHash)
return "", expectedErr
},
}
transactionsGroup, err := groups.NewTransactionGroup(facade)
require.NoError(t, err)
ws := startProxyServer(transactionsGroup, transactionsPath)

req, _ := http.NewRequest("GET", "/transaction/"+hash+"/process-status", nil)

resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := GeneralResponse{}
loadResponse(resp.Body, &response)

assert.Equal(t, http.StatusInternalServerError, resp.Code)
assert.Equal(t, expectedErr.Error(), response.Error)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

status := "status"
facade := &mock.FacadeStub{
GetProcessedTransactionStatusHandler: func(txHash string) (string, error) {
assert.Equal(t, hash, txHash)
return status, nil
},
}
transactionsGroup, err := groups.NewTransactionGroup(facade)
require.NoError(t, err)
ws := startProxyServer(transactionsGroup, transactionsPath)

req, _ := http.NewRequest("GET", "/transaction/"+hash+"/process-status", nil)

resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := txProcessedStatusResp{}
loadResponse(resp.Body, &response)

assert.Equal(t, http.StatusOK, resp.Code)
assert.Empty(t, response.Error)
assert.Equal(t, status, response.Data.TransactionHash)
})
}
7 changes: 4 additions & 3 deletions api/groups/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package groups
import (
"math/big"

"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data/vm"
"github.com/multiversx/mx-chain-proxy-go/common"
"github.com/multiversx/mx-chain-proxy-go/data"
Expand Down Expand Up @@ -96,9 +97,9 @@ type TransactionFacadeHandler interface {
SendUserFunds(receiver string, value *big.Int) error
TransactionCostRequest(tx *data.Transaction) (*data.TxCostResponseData, error)
GetTransactionStatus(txHash string, sender string) (string, error)
GetProcessedTransactionStatus(txHash string, sender string) (string, error)
GetTransaction(txHash string, withResults bool) (*data.ExtendedApiTransactionResult, error)
GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*data.ExtendedApiTransactionResult, int, error)
GetProcessedTransactionStatus(txHash string) (string, error)
GetTransaction(txHash string, withResults bool) (*transaction.ApiTransactionResult, error)
GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*transaction.ApiTransactionResult, int, error)
GetTransactionsPool(fields string) (*data.TransactionsPool, error)
GetTransactionsPoolForShard(shardID uint32, fields string) (*data.TransactionsPool, error)
GetTransactionsPoolForSender(sender, fields string) (*data.TransactionsPoolForSender, error)
Expand Down
15 changes: 8 additions & 7 deletions api/mock/facadeStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data/vm"
"github.com/multiversx/mx-chain-proxy-go/common"
"github.com/multiversx/mx-chain-proxy-go/data"
Expand All @@ -22,7 +23,7 @@ type FacadeStub struct {
GetNFTTokenIDsRegisteredByAddressCalled func(address string, options common.AccountQueryOptions) (*data.GenericAPIResponse, error)
GetAllESDTTokensCalled func(address string, options common.AccountQueryOptions) (*data.GenericAPIResponse, error)
GetTransactionsHandler func(address string) ([]data.DatabaseTransaction, error)
GetTransactionHandler func(txHash string, withResults bool) (*data.ExtendedApiTransactionResult, error)
GetTransactionHandler func(txHash string, withResults bool) (*transaction.ApiTransactionResult, error)
GetTransactionsPoolHandler func(fields string) (*data.TransactionsPool, error)
GetTransactionsPoolForShardHandler func(shardID uint32, fields string) (*data.TransactionsPool, error)
GetTransactionsPoolForSenderHandler func(sender, fields string) (*data.TransactionsPoolForSender, error)
Expand All @@ -37,7 +38,7 @@ type FacadeStub struct {
ValidatorStatisticsHandler func() (map[string]*data.ValidatorApiResponse, error)
TransactionCostRequestHandler func(tx *data.Transaction) (*data.TxCostResponseData, error)
GetTransactionStatusHandler func(txHash string, sender string) (string, error)
GetProcessedTransactionStatusHandler func(txHash string, sender string) (string, error)
GetProcessedTransactionStatusHandler func(txHash string) (string, error)
GetConfigMetricsHandler func() (*data.GenericAPIResponse, error)
GetNetworkMetricsHandler func(shardID uint32) (*data.GenericAPIResponse, error)
GetAllIssuedESDTsHandler func(tokenType string) (*data.GenericAPIResponse, error)
Expand All @@ -47,7 +48,7 @@ type FacadeStub struct {
GetDelegatedInfoCalled func() (*data.GenericAPIResponse, error)
GetRatingsConfigCalled func() (*data.GenericAPIResponse, error)
GetBlockByShardIDAndNonceHandler func(shardID uint32, nonce uint64) (data.AtlasBlock, error)
GetTransactionByHashAndSenderAddressHandler func(txHash string, sndAddr string, withResults bool) (*data.ExtendedApiTransactionResult, int, error)
GetTransactionByHashAndSenderAddressHandler func(txHash string, sndAddr string, withResults bool) (*transaction.ApiTransactionResult, int, error)
GetBlockByHashCalled func(shardID uint32, hash string, options common.BlockQueryOptions) (*data.BlockApiResponse, error)
GetBlockByNonceCalled func(shardID uint32, nonce uint64, options common.BlockQueryOptions) (*data.BlockApiResponse, error)
GetBlocksByRoundCalled func(round uint64, options common.BlockQueryOptions) (*data.BlocksApiResponse, error)
Expand Down Expand Up @@ -316,12 +317,12 @@ func (f *FacadeStub) GetTransactions(address string) ([]data.DatabaseTransaction
}

// GetTransactionByHashAndSenderAddress -
func (f *FacadeStub) GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*data.ExtendedApiTransactionResult, int, error) {
func (f *FacadeStub) GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*transaction.ApiTransactionResult, int, error) {
return f.GetTransactionByHashAndSenderAddressHandler(txHash, sndAddr, withEvents)
}

// GetTransaction -
func (f *FacadeStub) GetTransaction(txHash string, withResults bool) (*data.ExtendedApiTransactionResult, error) {
func (f *FacadeStub) GetTransaction(txHash string, withResults bool) (*transaction.ApiTransactionResult, error) {
return f.GetTransactionHandler(txHash, withResults)
}

Expand Down Expand Up @@ -401,8 +402,8 @@ func (f *FacadeStub) GetTransactionStatus(txHash string, sender string) (string,
}

// GetProcessedTransactionStatus -
func (f *FacadeStub) GetProcessedTransactionStatus(txHash string, sender string) (string, error) {
return f.GetProcessedTransactionStatusHandler(txHash, sender)
func (f *FacadeStub) GetProcessedTransactionStatus(txHash string) (string, error) {
return f.GetProcessedTransactionStatusHandler(txHash)
}

// SendUserFunds -
Expand Down
1 change: 1 addition & 0 deletions cmd/proxy/config/apiConfig/v1_0.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Routes = [
{ Name = "/cost", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/:txhash", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/:txhash/status", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/:txhash/process-status", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/pool", Open = true, Secured = false, RateLimit = 0 }
]

Expand Down
1 change: 1 addition & 0 deletions cmd/proxy/config/apiConfig/v_next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Routes = [
{ Name = "/cost", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/:txhash", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/:txhash/status", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/:txhash/process-status", Open = true, Secured = false, RateLimit = 0 },
{ Name = "/pool", Open = true, Secured = false, RateLimit = 0 }
]

Expand Down
8 changes: 0 additions & 8 deletions data/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,6 @@ type ExtendedApiSmartContractResult struct {
Used bool `json:"-"`
}

// ExtendedApiTransactionResult extends the original transaction.ApiTransactionResult with extra fields
// ProcessedStatus: will contain the transaction status after local processing
// TODO: move this in mx-chain-core-go so it can be reused in other projects.
type ExtendedApiTransactionResult struct {
*transaction.ApiTransactionResult
ProcessingStatus transaction.TxStatus `json:"processingStatus,omitempty"`
}

// ResponseTxCost defines a response from the node holding the transaction cost
type ResponseTxCost struct {
Data TxCostResponseData `json:"data"`
Expand Down
9 changes: 5 additions & 4 deletions facade/baseFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data/vm"
"github.com/multiversx/mx-chain-proxy-go/api/groups"
"github.com/multiversx/mx-chain-proxy-go/common"
Expand Down Expand Up @@ -216,12 +217,12 @@ func (epf *ProxyFacade) GetTransactionStatus(txHash string, sender string) (stri
}

// GetProcessedTransactionStatus should return transaction status after internal processing of the transaction results
func (epf *ProxyFacade) GetProcessedTransactionStatus(txHash string, sender string) (string, error) {
return epf.txProc.GetProcessedTransactionStatus(txHash, sender)
func (epf *ProxyFacade) GetProcessedTransactionStatus(txHash string) (string, error) {
return epf.txProc.GetProcessedTransactionStatus(txHash)
}

// GetTransaction should return a transaction by hash
func (epf *ProxyFacade) GetTransaction(txHash string, withResults bool) (*data.ExtendedApiTransactionResult, error) {
func (epf *ProxyFacade) GetTransaction(txHash string, withResults bool) (*transaction.ApiTransactionResult, error) {
return epf.txProc.GetTransaction(txHash, withResults)
}

Expand All @@ -236,7 +237,7 @@ func (epf *ProxyFacade) ReloadFullHistoryObservers() data.NodesReloadResponse {
}

// GetTransactionByHashAndSenderAddress should return a transaction by hash and sender address
func (epf *ProxyFacade) GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*data.ExtendedApiTransactionResult, int, error) {
func (epf *ProxyFacade) GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*transaction.ApiTransactionResult, int, error) {
return epf.txProc.GetTransactionByHashAndSenderAddress(txHash, sndAddr, withEvents)
}

Expand Down
7 changes: 4 additions & 3 deletions facade/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package facade
import (
"math/big"

"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/data/vm"
crypto "github.com/multiversx/mx-chain-crypto-go"
"github.com/multiversx/mx-chain-proxy-go/common"
Expand Down Expand Up @@ -39,9 +40,9 @@ type TransactionProcessor interface {
SimulateTransaction(tx *data.Transaction, checkSignature bool) (*data.GenericAPIResponse, error)
TransactionCostRequest(tx *data.Transaction) (*data.TxCostResponseData, error)
GetTransactionStatus(txHash string, sender string) (string, error)
GetTransaction(txHash string, withEvents bool) (*data.ExtendedApiTransactionResult, error)
GetProcessedTransactionStatus(txHash string, sender string) (string, error)
GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*data.ExtendedApiTransactionResult, int, error)
GetTransaction(txHash string, withEvents bool) (*transaction.ApiTransactionResult, error)
GetProcessedTransactionStatus(txHash string) (string, error)
GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*transaction.ApiTransactionResult, int, error)
ComputeTransactionHash(tx *data.Transaction) (string, error)
GetTransactionsPool(fields string) (*data.TransactionsPool, error)
GetTransactionsPoolForShard(shardID uint32, fields string) (*data.TransactionsPool, error)
Expand Down
17 changes: 9 additions & 8 deletions facade/mock/transactionProcessorStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"errors"
"math/big"

"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-proxy-go/data"
)

var errNotImplemented = errors.New("not implement")
var errNotImplemented = errors.New("not implemented")

// TransactionProcessorStub -
type TransactionProcessorStub struct {
Expand All @@ -17,9 +18,9 @@ type TransactionProcessorStub struct {
SendUserFundsCalled func(receiver string, value *big.Int) error
TransactionCostRequestCalled func(tx *data.Transaction) (*data.TxCostResponseData, error)
GetTransactionStatusCalled func(txHash string, sender string) (string, error)
GetProcessedTransactionStatusCalled func(txHash string, sender string) (string, error)
GetTransactionCalled func(txHash string, withEvents bool) (*data.ExtendedApiTransactionResult, error)
GetTransactionByHashAndSenderAddressCalled func(txHash string, sndAddr string, withEvents bool) (*data.ExtendedApiTransactionResult, int, error)
GetProcessedTransactionStatusCalled func(txHash string) (string, error)
GetTransactionCalled func(txHash string, withEvents bool) (*transaction.ApiTransactionResult, error)
GetTransactionByHashAndSenderAddressCalled func(txHash string, sndAddr string, withEvents bool) (*transaction.ApiTransactionResult, int, error)
ComputeTransactionHashCalled func(tx *data.Transaction) (string, error)
GetTransactionsPoolCalled func(fields string) (*data.TransactionsPool, error)
GetTransactionsPoolForShardCalled func(shardID uint32, fields string) (*data.TransactionsPool, error)
Expand Down Expand Up @@ -83,16 +84,16 @@ func (tps *TransactionProcessorStub) GetTransactionStatus(txHash string, sender
}

// GetProcessedTransactionStatus -
func (tps *TransactionProcessorStub) GetProcessedTransactionStatus(txHash string, sender string) (string, error) {
func (tps *TransactionProcessorStub) GetProcessedTransactionStatus(txHash string) (string, error) {
if tps.GetProcessedTransactionStatusCalled != nil {
return tps.GetProcessedTransactionStatusCalled(txHash, sender)
return tps.GetProcessedTransactionStatusCalled(txHash)
}

return "", errNotImplemented
}

// GetTransaction -
func (tps *TransactionProcessorStub) GetTransaction(txHash string, withEvents bool) (*data.ExtendedApiTransactionResult, error) {
func (tps *TransactionProcessorStub) GetTransaction(txHash string, withEvents bool) (*transaction.ApiTransactionResult, error) {
if tps.GetTransactionCalled != nil {
return tps.GetTransactionCalled(txHash, withEvents)
}
Expand All @@ -101,7 +102,7 @@ func (tps *TransactionProcessorStub) GetTransaction(txHash string, withEvents bo
}

// GetTransactionByHashAndSenderAddress -
func (tps *TransactionProcessorStub) GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*data.ExtendedApiTransactionResult, int, error) {
func (tps *TransactionProcessorStub) GetTransactionByHashAndSenderAddress(txHash string, sndAddr string, withEvents bool) (*transaction.ApiTransactionResult, int, error) {
if tps.GetTransactionByHashAndSenderAddressCalled != nil {
return tps.GetTransactionByHashAndSenderAddressCalled(txHash, sndAddr, withEvents)
}
Expand Down
Loading