Skip to content

Commit

Permalink
Refactor GetPreflight parameters into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
stellarsaur committed Dec 15, 2023
1 parent 29781a9 commit cf4cf0d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
16 changes: 14 additions & 2 deletions cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type SimulateTransactionResponse struct {
}

type PreflightGetter interface {
GetPreflight(ctx context.Context, readTx db.LedgerEntryReadTx, bucketListSize uint64, sourceAccount xdr.AccountId, opBody xdr.OperationBody, footprint xdr.LedgerFootprint, resourceConfig *preflight.ResourceConfig) (preflight.Preflight, error)
GetPreflight(ctx context.Context, params preflight.PreflightGetterParameters) (preflight.Preflight, error)
}

// NewSimulateTransactionHandler returns a json rpc handler to run preflight simulations
Expand Down Expand Up @@ -114,7 +114,19 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge
}
}

result, err := getter.GetPreflight(ctx, readTx, bucketListSize, sourceAccount, op.Body, footprint, request.ResourceConfig)
resource_config := preflight.DefaultResourceConfig()
if request.ResourceConfig != nil {
resource_config = *request.ResourceConfig
}
params := preflight.PreflightGetterParameters{
LedgerEntryReadTx: readTx,
BucketListSize: bucketListSize,
SourceAccount: sourceAccount,
OperationBody: op.Body,
Footprint: footprint,
ResourceConfig: resource_config,
}
result, err := getter.GetPreflight(ctx, params)
if err != nil {
return SimulateTransactionResponse{
Error: err.Error(),
Expand Down
18 changes: 9 additions & 9 deletions cmd/soroban-rpc/internal/preflight/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,27 @@ func (m *metricsLedgerEntryWrapper) GetLedgerEntries(keys ...xdr.LedgerKey) ([]d
return entries, err
}

func (pwp *PreflightWorkerPool) GetPreflight(ctx context.Context, readTx db.LedgerEntryReadTx, bucketListSize uint64, sourceAccount xdr.AccountId, opBody xdr.OperationBody, footprint xdr.LedgerFootprint, resourceConfig *ResourceConfig) (Preflight, error) {
func (pwp *PreflightWorkerPool) GetPreflight(ctx context.Context, params PreflightGetterParameters) (Preflight, error) {
if pwp.isClosed.Load() {
return Preflight{}, errors.New("preflight worker pool is closed")
}
wrappedTx := metricsLedgerEntryWrapper{
LedgerEntryReadTx: readTx,
LedgerEntryReadTx: params.LedgerEntryReadTx,
}
params := PreflightParameters{
preflightParams := PreflightParameters{
Logger: pwp.logger,
SourceAccount: sourceAccount,
OpBody: opBody,
SourceAccount: params.SourceAccount,
OpBody: params.OperationBody,
NetworkPassphrase: pwp.networkPassphrase,
LedgerEntryReadTx: &wrappedTx,
BucketListSize: bucketListSize,
Footprint: footprint,
ResourceConfig: resourceConfig,
BucketListSize: params.BucketListSize,
Footprint: params.Footprint,
ResourceConfig: params.ResourceConfig,
EnableDebug: pwp.enableDebug,
}
resultC := make(chan workerResult)
select {
case pwp.requestChan <- workerRequest{ctx, params, resultC}:
case pwp.requestChan <- workerRequest{ctx, preflightParams, resultC}:
result := <-resultC
if wrappedTx.ledgerEntriesFetched > 0 {
status := "ok"
Expand Down
23 changes: 17 additions & 6 deletions cmd/soroban-rpc/internal/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ type ResourceConfig struct {
InstructionLeeway uint64 `json:"instructionLeeway"`
}

func DefaultResourceConfig() ResourceConfig {
return ResourceConfig{
InstructionLeeway: defaultInstructionLeeway,
}
}

type PreflightGetterParameters struct {
LedgerEntryReadTx db.LedgerEntryReadTx
BucketListSize uint64
SourceAccount xdr.AccountId
OperationBody xdr.OperationBody
Footprint xdr.LedgerFootprint
ResourceConfig ResourceConfig
}

type PreflightParameters struct {
Logger *log.Entry
SourceAccount xdr.AccountId
Expand All @@ -87,7 +102,7 @@ type PreflightParameters struct {
NetworkPassphrase string
LedgerEntryReadTx db.LedgerEntryReadTx
BucketListSize uint64
ResourceConfig *ResourceConfig
ResourceConfig ResourceConfig
EnableDebug bool
}

Expand Down Expand Up @@ -225,12 +240,8 @@ func getInvokeHostFunctionPreflight(params PreflightParameters) (Preflight, erro

handle := cgo.NewHandle(snapshotSourceHandle{params.LedgerEntryReadTx, params.Logger})
defer handle.Delete()
instructionLeeway := defaultInstructionLeeway
if params.ResourceConfig != nil {
instructionLeeway = params.ResourceConfig.InstructionLeeway
}
resourceConfig := C.resource_config_t{
instruction_leeway: C.uint64_t(instructionLeeway),
instruction_leeway: C.uint64_t(params.ResourceConfig.InstructionLeeway),
}
res := C.preflight_invoke_hf_op(
C.uintptr_t(handle),
Expand Down

0 comments on commit cf4cf0d

Please sign in to comment.