fix: repair key handling in _getSignData and add Nonce live test
This commit is contained in:
committed by
Michael Vines
parent
bd0a9348f4
commit
aea0e83a40
@@ -1224,59 +1224,63 @@ export class Connection {
|
||||
transaction: Transaction,
|
||||
...signers: Array<Account>
|
||||
): Promise<TransactionSignature> {
|
||||
for (;;) {
|
||||
// Attempt to use a recent blockhash for up to 30 seconds
|
||||
const seconds = new Date().getSeconds();
|
||||
if (
|
||||
this._blockhashInfo.recentBlockhash != null &&
|
||||
this._blockhashInfo.seconds < seconds + 30
|
||||
) {
|
||||
transaction.recentBlockhash = this._blockhashInfo.recentBlockhash;
|
||||
transaction.sign(...signers);
|
||||
if (!transaction.signature) {
|
||||
throw new Error('!signature'); // should never happen
|
||||
}
|
||||
|
||||
// If the signature of this transaction has not been seen before with the
|
||||
// current recentBlockhash, all done.
|
||||
const signature = transaction.signature.toString();
|
||||
if (!this._blockhashInfo.transactionSignatures.includes(signature)) {
|
||||
this._blockhashInfo.transactionSignatures.push(signature);
|
||||
if (this._disableBlockhashCaching) {
|
||||
this._blockhashInfo.seconds = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch a new blockhash
|
||||
let attempts = 0;
|
||||
const startTime = Date.now();
|
||||
if (transaction.nonceInfo) {
|
||||
transaction.sign(...signers);
|
||||
} else {
|
||||
for (;;) {
|
||||
const [
|
||||
recentBlockhash,
|
||||
//feeCalculator,
|
||||
] = await this.getRecentBlockhash();
|
||||
// Attempt to use a recent blockhash for up to 30 seconds
|
||||
const seconds = new Date().getSeconds();
|
||||
if (
|
||||
this._blockhashInfo.recentBlockhash != null &&
|
||||
this._blockhashInfo.seconds < seconds + 30
|
||||
) {
|
||||
transaction.recentBlockhash = this._blockhashInfo.recentBlockhash;
|
||||
transaction.sign(...signers);
|
||||
if (!transaction.signature) {
|
||||
throw new Error('!signature'); // should never happen
|
||||
}
|
||||
|
||||
if (this._blockhashInfo.recentBlockhash != recentBlockhash) {
|
||||
this._blockhashInfo = {
|
||||
// If the signature of this transaction has not been seen before with the
|
||||
// current recentBlockhash, all done.
|
||||
const signature = transaction.signature.toString();
|
||||
if (!this._blockhashInfo.transactionSignatures.includes(signature)) {
|
||||
this._blockhashInfo.transactionSignatures.push(signature);
|
||||
if (this._disableBlockhashCaching) {
|
||||
this._blockhashInfo.seconds = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch a new blockhash
|
||||
let attempts = 0;
|
||||
const startTime = Date.now();
|
||||
for (;;) {
|
||||
const [
|
||||
recentBlockhash,
|
||||
seconds: new Date().getSeconds(),
|
||||
transactionSignatures: [],
|
||||
};
|
||||
break;
|
||||
}
|
||||
if (attempts === 50) {
|
||||
throw new Error(
|
||||
`Unable to obtain a new blockhash after ${Date.now() -
|
||||
startTime}ms`,
|
||||
);
|
||||
}
|
||||
//feeCalculator,
|
||||
] = await this.getRecentBlockhash();
|
||||
|
||||
// Sleep for approximately half a slot
|
||||
await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
|
||||
if (this._blockhashInfo.recentBlockhash != recentBlockhash) {
|
||||
this._blockhashInfo = {
|
||||
recentBlockhash,
|
||||
seconds: new Date().getSeconds(),
|
||||
transactionSignatures: [],
|
||||
};
|
||||
break;
|
||||
}
|
||||
if (attempts === 50) {
|
||||
throw new Error(
|
||||
`Unable to obtain a new blockhash after ${Date.now() -
|
||||
startTime}ms`,
|
||||
);
|
||||
}
|
||||
|
||||
++attempts;
|
||||
// Sleep for approximately half a slot
|
||||
await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
|
||||
|
||||
++attempts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -13,7 +13,7 @@ import {PublicKey} from './publickey';
|
||||
const NonceAccountLayout = BufferLayout.struct([
|
||||
BufferLayout.u32('state'),
|
||||
Layout.publicKey('authorizedPubkey'),
|
||||
Layout.publicKey('hash'),
|
||||
Layout.publicKey('nonce'),
|
||||
]);
|
||||
|
||||
/**
|
||||
|
@@ -184,7 +184,7 @@ export class Transaction {
|
||||
*/
|
||||
_getSignData(): Buffer {
|
||||
const {nonceInfo} = this;
|
||||
if (nonceInfo) {
|
||||
if (nonceInfo && this.instructions[0] != nonceInfo.nonceInstruction) {
|
||||
this.recentBlockhash = nonceInfo.nonce;
|
||||
this.instructions.unshift(nonceInfo.nonceInstruction);
|
||||
}
|
||||
@@ -203,25 +203,10 @@ export class Transaction {
|
||||
|
||||
const programIds = [];
|
||||
|
||||
const allKeys = [];
|
||||
this.instructions.forEach(instruction => {
|
||||
instruction.keys.forEach(keySignerPair => {
|
||||
const keyStr = keySignerPair.pubkey.toString();
|
||||
if (!keys.includes(keyStr)) {
|
||||
if (keySignerPair.isSigner) {
|
||||
this.signatures.push({
|
||||
signature: null,
|
||||
publicKey: keySignerPair.pubkey,
|
||||
});
|
||||
if (!keySignerPair.isWritable) {
|
||||
numReadonlySignedAccounts += 1;
|
||||
}
|
||||
} else {
|
||||
if (!keySignerPair.isWritable) {
|
||||
numReadonlyUnsignedAccounts += 1;
|
||||
}
|
||||
}
|
||||
keys.push(keyStr);
|
||||
}
|
||||
allKeys.push(keySignerPair);
|
||||
});
|
||||
|
||||
const programId = instruction.programId.toString();
|
||||
@@ -230,6 +215,32 @@ export class Transaction {
|
||||
}
|
||||
});
|
||||
|
||||
allKeys.sort(function(x, y) {
|
||||
const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1;
|
||||
const checkWritable = x.isWritable === y.isWritable ? 0 : x.isWritable ? -1 : 1;
|
||||
return checkSigner || checkWritable;
|
||||
});
|
||||
|
||||
allKeys.forEach(keySignerPair => {
|
||||
const keyStr = keySignerPair.pubkey.toString();
|
||||
if (!keys.includes(keyStr)) {
|
||||
if (keySignerPair.isSigner) {
|
||||
this.signatures.push({
|
||||
signature: null,
|
||||
publicKey: keySignerPair.pubkey,
|
||||
});
|
||||
if (!keySignerPair.isWritable) {
|
||||
numReadonlySignedAccounts += 1;
|
||||
}
|
||||
} else {
|
||||
if (!keySignerPair.isWritable) {
|
||||
numReadonlyUnsignedAccounts += 1;
|
||||
}
|
||||
}
|
||||
keys.push(keyStr);
|
||||
}
|
||||
});
|
||||
|
||||
programIds.forEach(programId => {
|
||||
if (!keys.includes(programId)) {
|
||||
keys.push(programId);
|
||||
|
Reference in New Issue
Block a user