Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Solana RPC Values on Authorize #2515

Open
wants to merge 4 commits into
base: nitro-payments-dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions services/payments/statemachine_solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ type SolanaMachine struct {
}

func (sm *SolanaMachine) Authorize(ctx context.Context) (*paymentLib.AuthenticatedPaymentState, error) {
var err error
var (
err error
blockHash string
slotTarget uint64
)
Comment on lines +64 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move these closer to where it's needed.


if !sm.IsAuthorized(ctx) {
return sm.transaction, &InsufficientAuthorizationsError{}
}
Expand All @@ -71,21 +76,38 @@ func (sm *SolanaMachine) Authorize(ctx context.Context) (*paymentLib.Authenticat
if sm.transaction.ExternalIdempotency != nil {
return sm.transaction, nil
}

// If the base Authorize method indicates we can proceed, generate, sign, and persist the
// transaction
latestBlockhashResult, err := sm.solanaRpcClient.GetLatestBlockhashAndContextWithConfig(
ctx,
// Defaults to Finalized, which decreases our available time to retry. Prefer Confirmed
solanaClient.GetLatestBlockhashConfig{
Commitment: rpc.CommitmentProcessed,
},
)
if err != nil {
return sm.transaction, fmt.Errorf("get recent block hash error, err: %w with result: %#v", err, latestBlockhashResult)
// Only get the latest block from the RPC if the one we cached in the context is older than 10
// seconds
cachedBlockTime, ok := ctx.Value("solanaSlotTargetTime").(time.Time)
// If the cache is bad or more than 10 seconds old, refresh it
if !ok || time.Now().Add(-10*time.Second).After(cachedBlockTime) {
// If the base Authorize method indicates we can proceed, generate, sign, and persist the
// transaction
latestBlockhashResult, err := sm.solanaRpcClient.GetLatestBlockhashAndContextWithConfig(
ctx,
// Defaults to Finalized, which decreases our available time to retry. Prefer Confirmed
solanaClient.GetLatestBlockhashConfig{
Commitment: rpc.CommitmentProcessed,
},
)
if err != nil {
return sm.transaction, fmt.Errorf("get recent block hash error, err: %w with result: %#v", err, latestBlockhashResult)
}
blockHash = latestBlockhashResult.Value.Blockhash
slotTarget = latestBlockhashResult.Value.LatestValidBlockHeight + 150
ctx = context.WithValue(ctx, "solanaSlotTarget", slotTarget)
ctx = context.WithValue(ctx, "solanaBlockHash", blockHash)
ctx = context.WithValue(ctx, "solanaSlotTargetTime", time.Now())
} else {
blockHash, ok = ctx.Value("solanaBlockHash").(string)
if !ok {
return sm.transaction, fmt.Errorf("cached solana blockHash was of the wrong type", err)
}
slotTarget, ok = ctx.Value("solanaSlotTarget").(uint64)
if !ok {
return sm.transaction, fmt.Errorf("cached solana slotTarget was of the wrong type", err)
}
}
blockHash := latestBlockhashResult.Value.Blockhash
slotTarget := latestBlockhashResult.Value.LatestValidBlockHeight + 150

var signer types.Account
signer, err = types.AccountFromSeed(sm.signingKey)
Expand Down