Fixed confirmations tests and bootstrap fast blockNumber.

This commit is contained in:
Richard Moore 2018-10-04 16:44:29 -04:00
parent 9797b36186
commit 908c2c1096
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
2 changed files with 23 additions and 11 deletions

@ -690,7 +690,7 @@ export class BaseProvider extends Provider {
console.log('re-poll fbn');
this._fastQueryDate = now;
this._fastBlockNumberPromise = this.getBlockNumber().then((blockNumber) => {
if (blockNumber > this._fastBlockNumber) {
if (this._fastBlockNumber == null || blockNumber > this._fastBlockNumber) {
this._fastBlockNumber = blockNumber;
}
return this._fastBlockNumber;
@ -933,7 +933,7 @@ export class BaseProvider extends Provider {
getTransaction(transactionHash: string): Promise<TransactionResponse> {
return this.ready.then(() => {
return resolveProperties({ transactionHash: transactionHash }).then(({ transactionHash }) => {
var params = { transactionHash: checkHash(transactionHash) };
let params = { transactionHash: checkHash(transactionHash) };
return poll(() => {
return this.perform('getTransaction', params).then((result) => {
if (result == null) {
@ -943,18 +943,24 @@ export class BaseProvider extends Provider {
return undefined;
}
let seq = Promise.resolve();
if (result.blockNumber != null && result.confirmations == null) {
seq = this._getFastBlockNumber().then((blockNumber) => {
let confirmations = (blockNumber - result.blockNumber) + 1;
let tx = BaseProvider.checkTransactionResponse(result);
if (tx.blockNumber == null) {
tx.confirmations = 0;
} else if (tx.confirmations == null) {
return this._getFastBlockNumber().then((blockNumber) => {
// Add the confirmations using the fast block number (pessimistic)
let confirmations = (blockNumber - tx.blockNumber) + 1;
if (confirmations <= 0) { confirmations = 1; }
result.confirmations = confirmations;
tx.confirmations = confirmations;
return tx;
});
}
return seq.then(() => {
return BaseProvider.checkTransactionResponse(result);
});
return tx;
});
}, { onceBlock: this });
});
@ -964,7 +970,7 @@ export class BaseProvider extends Provider {
getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt> {
return this.ready.then(() => {
return resolveProperties({ transactionHash: transactionHash }).then(({ transactionHash }) => {
var params = { transactionHash: checkHash(transactionHash) };
let params = { transactionHash: checkHash(transactionHash) };
return poll(() => {
return this.perform('getTransactionReceipt', params).then((result) => {
if (result == null) {
@ -973,6 +979,7 @@ export class BaseProvider extends Provider {
}
return undefined;
}
return checkTransactionReceipt(result);
});
}, { onceBlock: this });

@ -326,6 +326,11 @@ function testProvider(providerName, networkName) {
function testTransaction(expected) {
var title = ('Transaction ' + expected.hash.substring(0, 10) + ' - ');
return provider.getTransaction(expected.hash).then(function(tx) {
// This changes with every block
assert.equal(typeof(tx.confirmations), 'number', 'confirmations is a number');
delete tx.confirmations;
for (var key in tx) {
equals((title + key), tx[key], expected[key]);
}