-
Notifications
You must be signed in to change notification settings - Fork 911
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
refactor(experimental): add a function to assert a transaction has a blockhash lifetime #1908
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm, but for consistency I think this should be called assertIsTransactionWithBlockhashLifetime
. Wdyt?
function isBlockhashLifetimeTransaction( | ||
transaction: BaseTransaction | (BaseTransaction & ITransactionWithBlockhashLifetime) | ||
): transaction is BaseTransaction & ITransactionWithBlockhashLifetime { | ||
return ( | ||
'lifetimeConstraint' in transaction && | ||
typeof transaction.lifetimeConstraint.blockhash === 'string' && | ||
typeof transaction.lifetimeConstraint.lastValidBlockHeight === 'bigint' | ||
); | ||
} | ||
|
||
export function assertIsBlockhashLifetimeTransaction( | ||
transaction: BaseTransaction | (BaseTransaction & ITransactionWithBlockhashLifetime) | ||
): asserts transaction is BaseTransaction & ITransactionWithBlockhashLifetime { | ||
if (!isBlockhashLifetimeTransaction(transaction)) { | ||
// TODO: Coded error. | ||
throw new Error('Transaction is not a blockhash transaction'); | ||
} | ||
assertIsBlockhash(transaction.lifetimeConstraint.blockhash); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it's on purpose but the logic of the isX
and assertIsX
functions is not the same. Passing a transaction with an invalid base58 blockhash will work for isX
but fail for assertIsIx
.
To make them the same, we could push that conditional to the assertIsX
function and just do a try/catch on the isX
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks! I've refactored to move all the logic to the isX
function, which makes them symmetrical as you say and also mirrors the durable nonce equivalent.
…blockhash lifetime
e585ba9
to
3eb4a81
Compare
Agreed, renamed to that! |
): transaction is BaseTransaction & ITransactionWithBlockhashLifetime { | ||
const lifetimeConstraintShapeMatches = | ||
'lifetimeConstraint' in transaction && | ||
typeof transaction.lifetimeConstraint.blockhash === 'string' && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use assertIsBlockhash
here, but meh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up. |
This PR adds a new function
assertIsBlockhashLifetimeTransaction
which narrows the type of a transaction toBaseTransaction & ITransactionWithBlockhashLifetime
, if the transaction contains a valid blockhash lifetime constraintIt mirrors the behaviour of the existing
assertIsDurableNonceTransaction
My specific use case for this is using the default transaction confirmer with a deserialized transaction. The default transaction confirmer requires the
lastValidBlockHeight
field of the lifetime constraint:solana-web3.js/packages/library/src/transaction-confirmation.ts
Lines 40 to 47 in 8f39c90
By asserting that the deserialized transaction has a valid blockhash lifetime constraint, we can satisfy this requirement and that of any other code that requires a blockhash lifetime on the transaction.