Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 2x 2x 24x 24x | import semaphore from 'semaphore';
import logger from 'winston';
export default class {
constructor(initialHomeNonce, initialForeignNonce) {
this.homeNonce = Number(initialHomeNonce);
this.foreignNonce = Number(initialForeignNonce);
this.homeSem = semaphore();
this.foreignSem = semaphore();
}
obtainNonce(isHomeTx = false) {
logger.debug('Obtaining nonce isHomeTx: ', isHomeTx);
const sem = isHomeTx ? this.homeSem : this.foreignSem;
const n = isHomeTx ? this.homeNonce : this.foreignNonce;
return new Promise(resolve => {
sem.take(() => {
const n = isHomeTx ? this.homeNonce++ : this.foreignNonce++;
logger.debug('Giving nonce isHomeTx:', isHomeTx, 'nonce:', n);
resolve(n);
});
});
}
releaseNonce(nonce, isHomeTx = false, broadcasted = true) {
logger.debug('Releasing nonce:', nonce, 'isHomeTx:', isHomeTx, 'broadcasted:', broadcasted);
const n = isHomeTx ? this.homeNonce : this.foreignNonce;
// n is returned and then incremented
Iif (nonce + 1 !== n) {
throw new Error(
'attempting to release nonce, but the provided nonce should not have a lock',
);
}
if (isHomeTx) {
Iif (!broadcasted) this.homeNonce--;
this.homeSem.leave();
} else {
if (!broadcasted) this.foreignNonce--;
this.foreignSem.leave();
}
}
}
|