Skip to content

Commit

Permalink
Obtain simulation protocol version dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Apr 16, 2024
1 parent 7306ca7 commit 9a865a0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"errors"
"fmt"
"strings"
"sync/atomic"

"github.com/creachadair/jrpc2"
"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"
)
Expand Down Expand Up @@ -156,7 +158,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
Expand Down Expand Up @@ -223,13 +225,20 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge
if request.ResourceConfig != nil {
resource_config = *request.ResourceConfig
}
protocolVersion, err := getProtocolVersion(ctx, daemon.CoreClient())
if err != nil {
return SimulateTransactionResponse{
Error: fmt.Sprintf("cannot obtain protocol version from core: %s", err),
}
}
params := preflight.PreflightGetterParameters{
LedgerEntryReadTx: readTx,
BucketListSize: bucketListSize,
SourceAccount: sourceAccount,
OperationBody: op.Body,
Footprint: footprint,
ResourceConfig: resource_config,
ProtocolVersion: protocolVersion,
}
result, err := getter.GetPreflight(ctx, params)
if err != nil {
Expand Down Expand Up @@ -298,3 +307,19 @@ func getBucketListSize(ctx context.Context, ledgerReader db.LedgerReader, latest
}
return uint64(closeMeta.V1.TotalByteSizeOfBucketList), nil
}

var cachedCoreProtocolVersion atomic.Int32

// getProtocolVersion obtains the memoized protocol version from a core client
func getProtocolVersion(ctx context.Context, client interfaces.CoreClient) (int, error) {
version := cachedCoreProtocolVersion.Load()
if version != 0 {
return int(version), nil
}
info, err := client.Info(ctx)
if err != nil {
return 0, err
}
cachedCoreProtocolVersion.CompareAndSwap(0, int32(info.Info.ProtocolVersion))
return info.Info.ProtocolVersion, nil
}
1 change: 1 addition & 0 deletions cmd/soroban-rpc/internal/preflight/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/soroban-rpc/internal/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type PreflightGetterParameters struct {
OperationBody xdr.OperationBody
Footprint xdr.LedgerFootprint
ResourceConfig ResourceConfig
ProtocolVersion int
}

type PreflightParameters struct {
Expand All @@ -103,6 +104,7 @@ type PreflightParameters struct {
BucketListSize uint64
ResourceConfig ResourceConfig
EnableDebug bool
ProtocolVersion int
}

type XDRDiff struct {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-rpc/internal/preflight/preflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ func getPreflightParameters(t testing.TB, dbConfig *preflightParametersDBConfig)
NetworkPassphrase: "foo",
LedgerEntryReadTx: ledgerEntryReadTx,
BucketListSize: 200,
ProtocolVersion: 20,
}
return params
}
Expand Down

0 comments on commit 9a865a0

Please sign in to comment.