Skip to content

Commit

Permalink
Change jrpc response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed May 10, 2024
1 parent 21da905 commit 7e1a471
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
14 changes: 7 additions & 7 deletions cmd/soroban-rpc/internal/methods/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ func (h transactionsRPCHandler) getTransactionsByLedgerSequence(ctx context.Cont
ledgerRange, err := h.dbReader.GetLedgerRange(ctx)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Code: jrpc2.InternalError,
Message: err.Error(),
}
}

err = request.isValid(h.maxLimit, ledgerRange)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Code: jrpc2.InvalidRequest,
Message: err.Error(),
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ LedgerLoop:
err = errors.Errorf("ledger close meta not found: %d", ledgerSeq)
}
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Code: jrpc2.InternalError,
Message: err.Error(),
}
}
Expand All @@ -153,7 +153,7 @@ LedgerLoop:
reader, err := ingest.NewLedgerTransactionReaderFromLedgerCloseMeta(h.networkPassphrase, ledger)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Code: jrpc2.InternalError,
Message: err.Error(),
}
}
Expand All @@ -164,7 +164,7 @@ LedgerLoop:
startTxIdx = int(start.TransactionOrder)
if ierr := reader.Seek(startTxIdx - 1); ierr != nil && ierr != io.EOF {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Code: jrpc2.InternalError,
Message: err.Error(),
}
}
Expand All @@ -182,15 +182,15 @@ LedgerLoop:
break
}
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Code: jrpc2.InternalError,
Message: err.Error(),
}
}

tx, err := db.ParseTransaction(ledger, ingestTx)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Code: jrpc2.InternalError,
Message: err.Error(),
}
}
Expand Down
81 changes: 57 additions & 24 deletions cmd/soroban-rpc/internal/methods/get_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

"github.com/creachadair/jrpc2"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -58,14 +60,14 @@ func TestGetTransactions_DefaultLimit(t *testing.T) {
assert.NoError(t, err)

// assert latest ledger details
assert.Equal(t, response.LatestLedger, uint32(10))
assert.Equal(t, response.LatestLedgerCloseTime, int64(350))
assert.Equal(t, uint32(10), response.LatestLedger)
assert.Equal(t, int64(350), response.LatestLedgerCloseTime)

// assert pagination
assert.Equal(t, response.Cursor, toid.New(5, 2, 1).String())
assert.Equal(t, toid.New(5, 2, 1).String(), response.Cursor)

// assert transactions result
assert.Equal(t, len(response.Transactions), 10)
assert.Equal(t, 10, len(response.Transactions))
}

func TestGetTransactions_DefaultLimitExceedsLatestLedger(t *testing.T) {
Expand Down Expand Up @@ -93,14 +95,14 @@ func TestGetTransactions_DefaultLimitExceedsLatestLedger(t *testing.T) {
assert.NoError(t, err)

// assert latest ledger details
assert.Equal(t, response.LatestLedger, uint32(3))
assert.Equal(t, response.LatestLedgerCloseTime, int64(175))
assert.Equal(t, uint32(3), response.LatestLedger)
assert.Equal(t, int64(175), response.LatestLedgerCloseTime)

// assert pagination
assert.Equal(t, response.Cursor, toid.New(3, 2, 1).String())
assert.Equal(t, toid.New(3, 2, 1).String(), response.Cursor)

// assert transactions result
assert.Equal(t, len(response.Transactions), 6)
assert.Equal(t, 6, len(response.Transactions))
}

func TestGetTransactions_CustomLimit(t *testing.T) {
Expand Down Expand Up @@ -131,16 +133,16 @@ func TestGetTransactions_CustomLimit(t *testing.T) {
assert.NoError(t, err)

// assert latest ledger details
assert.Equal(t, response.LatestLedger, uint32(10))
assert.Equal(t, response.LatestLedgerCloseTime, int64(350))
assert.Equal(t, uint32(10), response.LatestLedger)
assert.Equal(t, int64(350), response.LatestLedgerCloseTime)

// assert pagination
assert.Equal(t, response.Cursor, toid.New(1, 2, 1).String())
assert.Equal(t, toid.New(1, 2, 1).String(), response.Cursor)

// assert transactions result
assert.Equal(t, len(response.Transactions), 2)
assert.Equal(t, response.Transactions[0].Ledger, uint32(1))
assert.Equal(t, response.Transactions[1].Ledger, uint32(1))
assert.Equal(t, 2, len(response.Transactions))
assert.Equal(t, uint32(1), response.Transactions[0].Ledger)
assert.Equal(t, uint32(1), response.Transactions[1].Ledger)
}

func TestGetTransactions_CustomLimitAndCursor(t *testing.T) {
Expand Down Expand Up @@ -171,17 +173,17 @@ func TestGetTransactions_CustomLimitAndCursor(t *testing.T) {
assert.NoError(t, err)

// assert latest ledger details
assert.Equal(t, response.LatestLedger, uint32(10))
assert.Equal(t, response.LatestLedgerCloseTime, int64(350))
assert.Equal(t, uint32(10), response.LatestLedger)
assert.Equal(t, int64(350), response.LatestLedgerCloseTime)

// assert pagination
assert.Equal(t, response.Cursor, toid.New(3, 1, 1).String())
assert.Equal(t, toid.New(3, 1, 1).String(), response.Cursor)

// assert transactions result
assert.Equal(t, len(response.Transactions), 3)
assert.Equal(t, response.Transactions[0].Ledger, uint32(2))
assert.Equal(t, response.Transactions[1].Ledger, uint32(2))
assert.Equal(t, response.Transactions[2].Ledger, uint32(3))
assert.Equal(t, 3, len(response.Transactions))
assert.Equal(t, uint32(2), response.Transactions[0].Ledger)
assert.Equal(t, uint32(2), response.Transactions[1].Ledger)
assert.Equal(t, uint32(3), response.Transactions[2].Ledger)
}

func TestGetTransactions_InvalidStartLedger(t *testing.T) {
Expand All @@ -206,7 +208,8 @@ func TestGetTransactions_InvalidStartLedger(t *testing.T) {
}

response, err := handler.getTransactionsByLedgerSequence(context.TODO(), request)
assert.Equal(t, err.Error(), "[-32602] start ledger must be between the oldest ledger: 1 and the latest ledger: 3 for this rpc instance.")
expectedErr := errors.Errorf("[%d] start ledger must be between the oldest ledger: 1 and the latest ledger: 3 for this rpc instance.", jrpc2.InvalidRequest)
assert.Equal(t, expectedErr.Error(), err.Error())
assert.Nil(t, response.Transactions)
}

Expand Down Expand Up @@ -236,7 +239,8 @@ func TestGetTransactions_LedgerNotFound(t *testing.T) {
}

response, err := handler.getTransactionsByLedgerSequence(context.TODO(), request)
assert.Equal(t, err.Error(), "[-32602] ledger close meta not found: 2")
expectedErr := errors.Errorf("[%d] ledger close meta not found: 2", jrpc2.InternalError)
assert.Equal(t, expectedErr.Error(), err.Error())
assert.Nil(t, response.Transactions)
}

Expand Down Expand Up @@ -265,5 +269,34 @@ func TestGetTransactions_LimitGreaterThanMaxLimit(t *testing.T) {
}

_, err := handler.getTransactionsByLedgerSequence(context.TODO(), request)
assert.Equal(t, err.Error(), "[-32602] limit must not exceed 100")
expectedErr := errors.Errorf("[%d] limit must not exceed 100", jrpc2.InvalidRequest)
assert.Equal(t, expectedErr.Error(), err.Error())
}

func TestGetTransactions_InvalidCursorString(t *testing.T) {
mockDbReader := db.NewMockTransactionStore(NetworkPassphrase)
mockLedgerReader := db.NewMockLedgerReader(mockDbReader)
for i := 1; i <= 3; i++ {
meta := createTestLedger(uint32(i))
err := mockDbReader.InsertTransactions(meta)
assert.NoError(t, err)
}

handler := transactionsRPCHandler{
ledgerReader: mockLedgerReader,
dbReader: mockDbReader,
maxLimit: 100,
defaultLimit: 10,
networkPassphrase: NetworkPassphrase,
}

request := GetTransactionsRequest{
Pagination: &TransactionsPaginationOptions{
Cursor: "abc",
},
}

_, err := handler.getTransactionsByLedgerSequence(context.TODO(), request)
expectedErr := errors.Errorf("[%d] strconv.ParseInt: parsing \"abc\": invalid syntax", jrpc2.InvalidParams)
assert.Equal(t, expectedErr.Error(), err.Error())
}

0 comments on commit 7e1a471

Please sign in to comment.