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

Fix lint error in /methods and remove usage of internal error package #285

Merged
merged 9 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/soroban-rpc/internal/integrationtest/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"runtime"
"testing"

"github.com/pkg/errors"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stellar/go/support/errors"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/integrationtest/infrastructure"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"time"

"github.com/creachadair/jrpc2"
"github.com/pkg/errors"

"github.com/stellar/go/strkey"
"github.com/stellar/go/support/collections/set"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"

Expand Down Expand Up @@ -119,7 +119,7 @@ func (g *GetEventsRequest) Valid(maxLimit uint) error {
// Validate the paging limit (if it exists)
if g.Pagination != nil && g.Pagination.Cursor != nil {
if g.StartLedger != 0 || g.EndLedger != 0 {
return errors.New("ledger ranges and cursor cannot both be set") //nolint:forbidigo
return errors.New("ledger ranges and cursor cannot both be set")
}
} else if g.StartLedger <= 0 {
return errors.New("startLedger must be positive")
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-rpc/internal/methods/get_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGetTransactions_DefaultLimit(t *testing.T) {
assert.Equal(t, toid.New(5, 2, 1).String(), response.Cursor)

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

func TestGetTransactions_DefaultLimitExceedsLatestLedger(t *testing.T) {
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestGetTransactions_CustomLimitAndCursor(t *testing.T) {
assert.Equal(t, toid.New(3, 1, 1).String(), response.Cursor)

// assert transactions result
assert.Equal(t, 3, len(response.Transactions))
assert.Len(t, 3, len(response.Transactions))
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
21 changes: 11 additions & 10 deletions cmd/soroban-rpc/internal/methods/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type Request struct {
Expand All @@ -15,7 +16,7 @@ type Request struct {

func TestNewHandlerNoArrayParameters(t *testing.T) {
callCount := 0
f := func(ctx context.Context, request Request) error {
f := func(_ context.Context, request Request) error {
callCount++
assert.Equal(t, "bar", request.Parameter)
return nil
Expand All @@ -27,15 +28,15 @@ func TestNewHandlerNoArrayParameters(t *testing.T) {
"params": { "parameter": "bar" }
}`
requests, err := jrpc2.ParseRequests([]byte(objectRequest))
assert.NoError(t, err)
require.NoError(t, err)
assert.Len(t, requests, 1)
finalObjectRequest := requests[0].ToRequest()

// object parameters should work with our handlers
customHandler := NewHandler(f)
_, err = customHandler(context.Background(), finalObjectRequest)
assert.NoError(t, err)
assert.Equal(t, 1, callCount)
require.NoError(t, err)
require.Equal(t, 1, callCount)

arrayRequest := `{
"jsonrpc": "2.0",
Expand All @@ -44,17 +45,17 @@ func TestNewHandlerNoArrayParameters(t *testing.T) {
"params": ["bar"]
}`
requests, err = jrpc2.ParseRequests([]byte(arrayRequest))
assert.NoError(t, err)
assert.Len(t, requests, 1)
require.NoError(t, err)
require.Len(t, requests, 1)
finalArrayRequest := requests[0].ToRequest()

// Array requests should work with the normal handler, but not with our handlers
stdHandler := handler.New(f)
_, err = stdHandler(context.Background(), finalArrayRequest)
assert.NoError(t, err)
assert.Equal(t, 2, callCount)
require.NoError(t, err)
require.Equal(t, 2, callCount)

_, err = customHandler(context.Background(), finalArrayRequest)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid parameters")
require.Error(t, err)
require.Contains(t, err.Error(), "invalid parameters")
}
24 changes: 15 additions & 9 deletions cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type SimulateTransactionCost struct {
MemoryBytes uint64 `json:"memBytes,string"`
}

// SimulateHostFunctionResult contains the simulation result of each HostFunction within the single InvokeHostFunctionOp allowed in a Transaction
// SimulateHostFunctionResult contains the simulation result of each HostFunction
// within the single InvokeHostFunctionOp allowed in a Transaction
type SimulateHostFunctionResult struct {
AuthXDR []string `json:"auth,omitempty"`
AuthJSON []json.RawMessage `json:"authJson,omitempty"`
Expand Down Expand Up @@ -213,12 +214,16 @@ type SimulateTransactionResponse struct {
EventsXDR []string `json:"events,omitempty"` // DiagnosticEvent XDR in base64
EventsJSON []json.RawMessage `json:"eventsJson,omitempty"`

MinResourceFee int64 `json:"minResourceFee,string,omitempty"`
Results []SimulateHostFunctionResult `json:"results,omitempty"` // an array of the individual host function call results
Cost SimulateTransactionCost `json:"cost,omitempty"` // the effective cpu and memory cost of the invoked transaction execution.
RestorePreamble *RestorePreamble `json:"restorePreamble,omitempty"` // If present, it indicates that a prior RestoreFootprint is required
StateChanges []LedgerEntryChange `json:"stateChanges,omitempty"` // If present, it indicates how the state (ledger entries) will change as a result of the transaction execution.
LatestLedger uint32 `json:"latestLedger"`
MinResourceFee int64 `json:"minResourceFee,string,omitempty"`
// an array of the individual host function call results
Results []SimulateHostFunctionResult `json:"results,omitempty"`
// the effective cpu and memory cost of the invoked transaction execution.
Cost SimulateTransactionCost `json:"cost,omitempty"`
// If present, it indicates that a prior RestoreFootprint is required
RestorePreamble *RestorePreamble `json:"restorePreamble,omitempty"`
// If present, it indicates how the state (ledger entries) will change as a result of the transaction execution.
StateChanges []LedgerEntryChange `json:"stateChanges,omitempty"`
LatestLedger uint32 `json:"latestLedger"`
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
}

type PreflightGetter interface {
Expand Down Expand Up @@ -260,7 +265,8 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge
case xdr.OperationTypeExtendFootprintTtl, xdr.OperationTypeRestoreFootprint:
if txEnvelope.Type != xdr.EnvelopeTypeEnvelopeTypeTx && txEnvelope.V1.Tx.Ext.V != 1 {
return SimulateTransactionResponse{
Error: "To perform a SimulateTransaction for ExtendFootprintTtl or RestoreFootprint operations, SorobanTransactionData must be provided",
Error: "To perform a SimulateTransaction for ExtendFootprintTtl or RestoreFootprint operations," +
" SorobanTransactionData must be provided",
}
}
footprint = txEnvelope.V1.Tx.Ext.SorobanData.Resources.Footprint
Expand Down Expand Up @@ -374,7 +380,7 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge
}

stateChanges := make([]LedgerEntryChange, len(result.LedgerEntryDiff))
for i := 0; i < len(stateChanges); i++ {
for i := range stateChanges {
if err := stateChanges[i].FromXDRDiff(result.LedgerEntryDiff[i], request.Format); err != nil {
return SimulateTransactionResponse{
Error: err.Error(),
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-rpc/internal/methods/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func createMockLedgerCloseMeta(ledgerSequence uint32) xdr.LedgerCloseMeta {
func NewTestDB(tb testing.TB) *db.DB {
tmp := tb.TempDir()
dbPath := path.Join(tmp, "db.sqlite")
db, err := db.OpenSQLiteDB(dbPath)
dbConn, err := db.OpenSQLiteDB(dbPath)
require.NoError(tb, err)
tb.Cleanup(func() {
require.NoError(tb, db.Close())
require.NoError(tb, dbConn.Close())
})
return db
return dbConn
}
Loading