Skip to content

Commit

Permalink
Add hash to getTransactions response (#299)
Browse files Browse the repository at this point in the history
Add transaction hash in the transaction object in the getTransactions response
  • Loading branch information
aditya1702 authored Sep 24, 2024
1 parent cae1740 commit e7debbe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/soroban-rpc/internal/db/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
var ErrNoTransaction = errors.New("no transaction with this hash exists")

type Transaction struct {
TransactionHash string
Result []byte // XDR encoded xdr.TransactionResult
Meta []byte // XDR encoded xdr.TransactionMeta
Envelope []byte // XDR encoded xdr.TransactionEnvelope
Expand Down Expand Up @@ -223,6 +224,7 @@ func ParseTransaction(lcm xdr.LedgerCloseMeta, ingestTx ingest.LedgerTransaction
Sequence: lcm.LedgerSequence(),
CloseTime: lcm.LedgerCloseTime(),
}
tx.TransactionHash = ingestTx.Result.TransactionHash.HexString()

if tx.Result, err = ingestTx.Result.Result.MarshalBinary(); err != nil {
return tx, fmt.Errorf("couldn't encode transaction Result: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions cmd/soroban-rpc/internal/methods/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (req GetTransactionsRequest) isValid(maxLimit uint, ledgerRange ledgerbucke
type TransactionInfo struct {
// Status is one of: TransactionSuccess, TransactionFailed.
Status string `json:"status"`
// TransactionHash is the hex encoded hash of the transaction. Note that for fee-bump transaction
// this will be the hash of the fee-bump transaction instead of the inner transaction hash.
TransactionHash string `json:"txHash"`
// ApplicationOrder is the index of the transaction among all the transactions
// for that ledger.
ApplicationOrder int32 `json:"applicationOrder"`
Expand Down Expand Up @@ -194,6 +197,7 @@ func (h transactionsRPCHandler) processTransactionsInLedger(
}

txInfo := TransactionInfo{
TransactionHash: tx.TransactionHash,
ApplicationOrder: tx.ApplicationOrder,
FeeBump: tx.FeeBump,
Ledger: tx.Ledger.Sequence,
Expand Down
22 changes: 22 additions & 0 deletions cmd/soroban-rpc/internal/methods/get_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ const (
NetworkPassphrase string = "passphrase"
)

var expectedTransactionInfo = TransactionInfo{
Status: "SUCCESS",
TransactionHash: "b0d0b35dcaed0152d62fbbaa28ed3fa4991c87e7e169a8fca2687b17ee26ca2d",
ApplicationOrder: 1,
FeeBump: false,
Ledger: 1,
LedgerCloseTime: 125,
EnvelopeXDR: "AAAAAgAAAQCAAAAAAAAAAD8MNL+TrQ2ZcdBMzJD3BVEcg4qtlzSkovsNegP8f+iaAAAAAQAAAAD///+dAAAAAAAAAAAAAAAAAAAAAAAAAAA=", //nolint:lll
ResultMetaXDR: "AAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAA",
ResultXDR: "AAAAAAAAAGQAAAAAAAAAAAAAAAA=",
DiagnosticEventsXDR: []string{},
}

// createTestLedger Creates a test ledger with 2 transactions
func createTestLedger(sequence uint32) xdr.LedgerCloseMeta {
sequence -= 100
Expand Down Expand Up @@ -70,6 +83,9 @@ func TestGetTransactions_DefaultLimit(t *testing.T) {

// assert transactions result
assert.Len(t, response.Transactions, 10)

// assert the transaction structure. We will match only 1 tx for sanity purposes.
assert.Equal(t, expectedTransactionInfo, response.Transactions[0])
}

func TestGetTransactions_DefaultLimitExceedsLatestLedger(t *testing.T) {
Expand Down Expand Up @@ -104,6 +120,9 @@ func TestGetTransactions_DefaultLimitExceedsLatestLedger(t *testing.T) {

// assert transactions result
assert.Len(t, response.Transactions, 6)

// assert the transaction structure. We will match only 1 tx for sanity purposes.
assert.Equal(t, expectedTransactionInfo, response.Transactions[0])
}

func TestGetTransactions_CustomLimit(t *testing.T) {
Expand Down Expand Up @@ -143,6 +162,9 @@ func TestGetTransactions_CustomLimit(t *testing.T) {
assert.Len(t, response.Transactions, 2)
assert.Equal(t, uint32(1), response.Transactions[0].Ledger)
assert.Equal(t, uint32(1), response.Transactions[1].Ledger)

// assert the transaction structure. We will match only 1 tx for sanity purposes.
assert.Equal(t, expectedTransactionInfo, response.Transactions[0])
}

func TestGetTransactions_CustomLimitAndCursor(t *testing.T) {
Expand Down

0 comments on commit e7debbe

Please sign in to comment.