Skip to content

Commit

Permalink
soroban-rpc: Fix simulation sequence number for bump/restore operatio…
Browse files Browse the repository at this point in the history
…ns (stellar#877)
  • Loading branch information
2opremio authored Aug 21, 2023
1 parent 3f13cd0 commit 86bc457
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions cmd/soroban-rpc/internal/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func getFootprintExpirationPreflight(params PreflightParameters) (Preflight, err
handle := cgo.NewHandle(snapshotSourceHandle{params.LedgerEntryReadTx, params.Logger})
defer handle.Delete()

latestLedger, err := params.LedgerEntryReadTx.GetLatestLedgerSequence()
simulationLedgerSeq, err := getSimulationLedgerSeq(params.LedgerEntryReadTx)
if err != nil {
return Preflight{}, err
}
Expand All @@ -141,7 +141,7 @@ func getFootprintExpirationPreflight(params PreflightParameters) (Preflight, err
C.uint64_t(params.BucketListSize),
opBodyCString,
footprintCString,
C.uint32_t(latestLedger),
C.uint32_t(simulationLedgerSeq),
)

C.free(unsafe.Pointer(opBodyCString))
Expand All @@ -150,6 +150,18 @@ func getFootprintExpirationPreflight(params PreflightParameters) (Preflight, err
return GoPreflight(res)
}

func getSimulationLedgerSeq(readTx db.LedgerEntryReadTx) (uint32, error) {
latestLedger, err := readTx.GetLatestLedgerSequence()
if err != nil {
return 0, err
}
// It's of utmost importance to simulate the transactions like we were on the next ledger.
// Otherwise, users would need to wait for an extra ledger to close in order to observe the effects of the latest ledger
// transaction submission.
sequenceNumber := latestLedger + 1
return sequenceNumber, nil
}

func getInvokeHostFunctionPreflight(params PreflightParameters) (Preflight, error) {
invokeHostFunctionB64, err := xdr.MarshalBase64(params.OpBody.MustInvokeHostFunctionOp())
if err != nil {
Expand All @@ -160,10 +172,6 @@ func getInvokeHostFunctionPreflight(params PreflightParameters) (Preflight, erro
if err != nil {
return Preflight{}, err
}
latestLedger, err := params.LedgerEntryReadTx.GetLatestLedgerSequence()
if err != nil {
return Preflight{}, err
}

hasConfig, stateExpirationConfig, err := params.LedgerEntryReadTx.GetLedgerEntry(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeConfigSetting,
Expand All @@ -178,14 +186,15 @@ func getInvokeHostFunctionPreflight(params PreflightParameters) (Preflight, erro
return Preflight{}, errors.New("state expiration config setting missing in ledger storage")
}

simulationLedgerSeq, err := getSimulationLedgerSeq(params.LedgerEntryReadTx)
if err != nil {
return Preflight{}, err
}

stateExpiration := stateExpirationConfig.Data.MustConfigSetting().MustStateExpirationSettings()
// It's of utmost importance to simulate the transactions like we were on the next ledger.
// Otherwise, users would need to wait for an extra ledger to close in order to observe the effects of the latest ledger
// transaction submission.
sequenceNumber := latestLedger + 1
li := C.CLedgerInfo{
network_passphrase: C.CString(params.NetworkPassphrase),
sequence_number: C.uint32_t(sequenceNumber),
sequence_number: C.uint32_t(simulationLedgerSeq),
protocol_version: 20,
timestamp: C.uint64_t(time.Now().Unix()),
// Current base reserve is 0.5XLM (in stroops)
Expand Down

0 comments on commit 86bc457

Please sign in to comment.