Skip to content

Commit

Permalink
Obtain simulation protocol version dynamically (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Apr 16, 2024
1 parent 7306ca7 commit 638c62d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 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
16 changes: 9 additions & 7 deletions cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
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 uint32
}

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

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 638c62d

Please sign in to comment.