Skip to content

Commit

Permalink
feat: allow to specify period data fallback for getProof helper (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
troggy authored Nov 22, 2019
1 parent db3106a commit 090f903
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ declare module "leap-core" {
type PeriodData = {
validatorAddress: string;
slotId: number;
casBitmap: string;
periodStart: number;
periodEnd: number;
casBitmap?: string;
periodStart?: number;
periodEnd?: number;
};

class ExtendedWeb3 extends Web3 {
Expand All @@ -352,7 +352,7 @@ declare module "leap-core" {
export function periodBlockRange(blockNumber: number): [number, number];
export function getTxWithYoungestBlock(txs: LeapTransaction[]): InputTx;
export function getYoungestInputTx(plasma: ExtendedWeb3, tx: Tx<any>): Promise<InputTx>;
export function getProof(plasma: ExtendedWeb3, tx: LeapTransaction): Promise<Proof>;
export function getProof(plasma: ExtendedWeb3, tx: LeapTransaction, fallbackPeriodData?: PeriodData): Promise<Proof>;
// Depending on plasma instance, resolves to either Web3's Transaction or Ethers' TransactionReceipt
export function sendSignedTransaction(plasma: ExtendedWeb3, tx: string): Promise<any>;
export function simulateSpendCond(plasma: ExtendedWeb3, tx: Tx<Type.SPEND_COND>): Promise<SpendCondSimResult>;
Expand Down
12 changes: 9 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,21 @@ export function getYoungestInputTx(plasma, tx) {
* @param {LeapTransaction} tx
* @returns {Promise<Proof>} promise that resolves to period inclusion proof
*/
export function getProof(plasma, tx) {
export function getProof(plasma, tx, fallbackPeriodData) {
return Promise.all([
Period.periodForTx(plasma, tx),
plasma.getPeriodByBlockHeight(tx.blockNumber)
]).then(([period, periodData]) => {
const [periodDataObj] = periodData || [fallbackPeriodData];
if (!periodData || !periodData.length) {
throw new Error(`No period data for the given tx. Height:${tx.blockHeight}`);
const msg = `No period data for the given tx. Height: ${tx.blockNumber}`;
if (!fallbackPeriodData) {
throw new Error(msg);
} else {
console.warn(msg, 'Using fallback values'); // eslint-disable-line no-console
}
}
const { slotId, validatorAddress, casBitmap } = periodData[0];
const { slotId, validatorAddress, casBitmap } = periodDataObj;

period.setValidatorData(slotId, validatorAddress, casBitmap);
return period.proof(Tx.fromRaw(tx.raw));
Expand Down
37 changes: 37 additions & 0 deletions lib/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,43 @@ describe('helpers', () => {
).to.eventually.throw(`No period data for the given tx. Height:4`);
});

it('no period data for the height, with fallback', async () => {
const value = 50000000;
const color = 1337;
const deposit1 = Tx.deposit(0, value, ADDR_1, color);

const plasma = {
getBlock: n => {
expect(n).to.be.within(0, 32);
const transactions = n === 4 ? [{ raw: deposit1.hex() }] : [];
return { number: n, timestamp: 123, transactions };
},
getPeriodByBlockHeight: () => null,
};

const fallbackData = { slotId: 0, validatorAddress: ADDR_1 };

const proof = getProof(
plasma,
{ blockNumber: 4, raw: deposit1.hex() },
fallbackData
);
return expect(proof).to.eventually.eql([
'0x29aa1b0213471dbf84175e8f688e5a63c2e5724ad6bc581a10b9521f4b8a6083',
'0x4404003c00000000000000080000000000000000000000000000000000000000',
'0x0000000002110000000000000000000000000000000000000000000000000000',
'0x00000000000002faf08005394436373705394267350db2c06613990d34621d69',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5',
'0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30',
'0x21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85',
'0xe58769b32a1beaf1ea27375a44095a0d1fb664ce2dd358e7fcbfb78c26a19344',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x99ed61756087b72bef3d352ea237450df3d0749dd2e39615b8f8e993370f4ae9'
]);
});

it('should get CAS proof', async () => {
const value = 50000000;
const color = 1337;
Expand Down

0 comments on commit 090f903

Please sign in to comment.