From 638c62d8f58707ec5e079f68b4e6dbf96b94401b Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 16 Apr 2024 21:49:40 +0200 Subject: [PATCH] Obtain simulation protocol version dynamically (#142) --- cmd/soroban-rpc/internal/jsonrpc.go | 2 +- .../internal/methods/simulate_transaction.go | 16 +++++++++------- cmd/soroban-rpc/internal/preflight/pool.go | 1 + cmd/soroban-rpc/internal/preflight/preflight.go | 4 +++- .../internal/preflight/preflight_test.go | 1 + 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/soroban-rpc/internal/jsonrpc.go b/cmd/soroban-rpc/internal/jsonrpc.go index b0ceb372..59d83420 100644 --- a/cmd/soroban-rpc/internal/jsonrpc.go +++ b/cmd/soroban-rpc/internal/jsonrpc.go @@ -208,7 +208,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { }, { methodName: "simulateTransaction", - underlyingHandler: methods.NewSimulateTransactionHandler(params.Logger, params.LedgerEntryReader, params.LedgerReader, params.PreflightGetter), + underlyingHandler: methods.NewSimulateTransactionHandler(params.Logger, params.LedgerEntryReader, params.LedgerReader, params.Daemon, params.PreflightGetter), longName: "simulate_transaction", queueLimit: cfg.RequestBacklogSimulateTransactionQueueLimit, requestDurationLimit: cfg.MaxSimulateTransactionExecutionDuration, diff --git a/cmd/soroban-rpc/internal/methods/simulate_transaction.go b/cmd/soroban-rpc/internal/methods/simulate_transaction.go index 6edc37d7..9ad98d80 100644 --- a/cmd/soroban-rpc/internal/methods/simulate_transaction.go +++ b/cmd/soroban-rpc/internal/methods/simulate_transaction.go @@ -12,6 +12,7 @@ import ( "github.com/stellar/go/support/log" "github.com/stellar/go/xdr" + "github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/daemon/interfaces" "github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db" "github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/preflight" ) @@ -156,7 +157,7 @@ type PreflightGetter interface { } // NewSimulateTransactionHandler returns a json rpc handler to run preflight simulations -func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.LedgerEntryReader, ledgerReader db.LedgerReader, getter PreflightGetter) jrpc2.Handler { +func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.LedgerEntryReader, ledgerReader db.LedgerReader, daemon interfaces.Daemon, getter PreflightGetter) jrpc2.Handler { return NewHandler(func(ctx context.Context, request SimulateTransactionRequest) SimulateTransactionResponse { var txEnvelope xdr.TransactionEnvelope @@ -212,7 +213,7 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge Error: err.Error(), } } - bucketListSize, err := getBucketListSize(ctx, ledgerReader, latestLedger) + bucketListSize, protocolVersion, err := getBucketListSizeAndProtocolVersion(ctx, ledgerReader, latestLedger) if err != nil { return SimulateTransactionResponse{ Error: err.Error(), @@ -230,6 +231,7 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge OperationBody: op.Body, Footprint: footprint, ResourceConfig: resource_config, + ProtocolVersion: protocolVersion, } result, err := getter.GetPreflight(ctx, params) if err != nil { @@ -284,17 +286,17 @@ func base64EncodeSlice(in [][]byte) []string { return result } -func getBucketListSize(ctx context.Context, ledgerReader db.LedgerReader, latestLedger uint32) (uint64, error) { +func getBucketListSizeAndProtocolVersion(ctx context.Context, ledgerReader db.LedgerReader, latestLedger uint32) (uint64, uint32, error) { // obtain bucket size var closeMeta, ok, err = ledgerReader.GetLedger(ctx, latestLedger) if err != nil { - return 0, err + return 0, 0, err } if !ok { - return 0, fmt.Errorf("missing meta for latest ledger (%d)", latestLedger) + return 0, 0, fmt.Errorf("missing meta for latest ledger (%d)", latestLedger) } if closeMeta.V != 1 { - return 0, fmt.Errorf("latest ledger (%d) meta has unexpected verion (%d)", latestLedger, closeMeta.V) + return 0, 0, fmt.Errorf("latest ledger (%d) meta has unexpected verion (%d)", latestLedger, closeMeta.V) } - return uint64(closeMeta.V1.TotalByteSizeOfBucketList), nil + return uint64(closeMeta.V1.TotalByteSizeOfBucketList), uint32(closeMeta.V1.LedgerHeader.Header.LedgerVersion), nil } diff --git a/cmd/soroban-rpc/internal/preflight/pool.go b/cmd/soroban-rpc/internal/preflight/pool.go index 4eeddb53..3ea704d8 100644 --- a/cmd/soroban-rpc/internal/preflight/pool.go +++ b/cmd/soroban-rpc/internal/preflight/pool.go @@ -156,6 +156,7 @@ func (pwp *PreflightWorkerPool) GetPreflight(ctx context.Context, params Preflig Footprint: params.Footprint, ResourceConfig: params.ResourceConfig, EnableDebug: pwp.enableDebug, + ProtocolVersion: params.ProtocolVersion, } resultC := make(chan workerResult) select { diff --git a/cmd/soroban-rpc/internal/preflight/preflight.go b/cmd/soroban-rpc/internal/preflight/preflight.go index 9a0fb94f..ec0d8fd3 100644 --- a/cmd/soroban-rpc/internal/preflight/preflight.go +++ b/cmd/soroban-rpc/internal/preflight/preflight.go @@ -91,6 +91,7 @@ type PreflightGetterParameters struct { OperationBody xdr.OperationBody Footprint xdr.LedgerFootprint ResourceConfig ResourceConfig + ProtocolVersion uint32 } type PreflightParameters struct { @@ -103,6 +104,7 @@ type PreflightParameters struct { BucketListSize uint64 ResourceConfig ResourceConfig EnableDebug bool + ProtocolVersion uint32 } type XDRDiff struct { @@ -174,7 +176,7 @@ func getLedgerInfo(params PreflightParameters) (C.ledger_info_t, error) { li := C.ledger_info_t{ network_passphrase: C.CString(params.NetworkPassphrase), sequence_number: C.uint32_t(simulationLedgerSeq), - protocol_version: 20, + protocol_version: C.uint32_t(params.ProtocolVersion), timestamp: C.uint64_t(time.Now().Unix()), // Current base reserve is 0.5XLM (in stroops) base_reserve: 5_000_000, diff --git a/cmd/soroban-rpc/internal/preflight/preflight_test.go b/cmd/soroban-rpc/internal/preflight/preflight_test.go index 4b164212..c7787bc5 100644 --- a/cmd/soroban-rpc/internal/preflight/preflight_test.go +++ b/cmd/soroban-rpc/internal/preflight/preflight_test.go @@ -361,6 +361,7 @@ func getPreflightParameters(t testing.TB, dbConfig *preflightParametersDBConfig) NetworkPassphrase: "foo", LedgerEntryReadTx: ledgerEntryReadTx, BucketListSize: 200, + ProtocolVersion: 20, } return params }