Skip to content

Commit

Permalink
Merge branch '55-verify-creation_height-and-inclusion_height-compatib…
Browse files Browse the repository at this point in the history
…ility' into 'dev'

consider box creation_height while verifying event

Closes #55

See merge request ergo/rosen-bridge/rosen-chains!62
  • Loading branch information
vorujack committed Sep 30, 2023
2 parents b345827 + ad3b429 commit 5545da0
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 22 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion packages/chains/ergo/lib/ErgoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '@rosen-chains/abstract-chain';
import { blake2b } from 'blakejs';
import * as wasm from 'ergo-lib-wasm-nodejs';
import { ERGO_CHAIN } from './constants';
import { ERGO_CHAIN, NUMBER_OF_BLOCKS_PER_YEAR } from './constants';
import ErgoTransaction from './ErgoTransaction';
import ErgoUtils from './ErgoUtils';
import AbstractErgoNetwork from './network/AbstractErgoNetwork';
Expand Down Expand Up @@ -454,6 +454,17 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
);
const blockHeight = (await this.network.getBlockInfo(event.sourceBlockId))
.height;
for (let i = 0; i < tx.outputs().len(); i++) {
const box = tx.outputs().get(i);
if (blockHeight - box.creation_height() > NUMBER_OF_BLOCKS_PER_YEAR) {
this.logger.info(
`Event [${eventId}] is not valid, box [${box
.box_id()
.to_str()}] creation_height [${box.creation_height()}] is more than a year ago`
);
return false;
}
}
const data = this.network.extractor.get(
Buffer.from(tx.sigma_serialize_bytes()).toString('hex')
);
Expand Down
4 changes: 3 additions & 1 deletion packages/chains/ergo/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const ERGO_CHAIN = 'ergo';
const ERG = 'erg';

export { ERGO_CHAIN, ERG };
const NUMBER_OF_BLOCKS_PER_YEAR = 30 * 24 * 365;

export { ERGO_CHAIN, ERG, NUMBER_OF_BLOCKS_PER_YEAR };
2 changes: 1 addition & 1 deletion packages/chains/ergo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/ergo",
"version": "1.0.2",
"version": "1.0.3",
"description": "this project contains ergo chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
67 changes: 66 additions & 1 deletion packages/chains/ergo/tests/ErgoChain.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as boxTestData from './boxTestData';
import * as transactionTestData from './transactionTestData';
import * as ergoTestUtils from './ergoTestUtils';
import { ErgoChain } from '../lib';
import { ErgoChain, NUMBER_OF_BLOCKS_PER_YEAR } from '../lib';
import {
AssetBalance,
BlockInfo,
Expand Down Expand Up @@ -1217,6 +1217,71 @@ describe('ErgoChain', () => {
// check returned value
expect(result).toEqual(false);
});

/**
* @target ErgoChain.verifyEvent should return false when
* output box creation height is more than a year ago
* @dependencies
* @scenario
* - mock an event
* - mock a network object
* - mock 'getBlockTransactionIds'
* - mock 'getBlockInfo'
* - mock 'getTransaction'
* - mock network extractor to return event data
* - run test
* - check returned value
* @expected
* - it should return false
*/
it('should return false when output box creation height is more than a year ago', async () => {
// mock an event
const event = boxTestData.validEvent;

// mock a network object
const network = new TestErgoNetwork();
// mock 'getBlockTransactionIds'
const getBlockTransactionIdsSpy = spyOn(
network,
'getBlockTransactionIds'
);
when(getBlockTransactionIdsSpy)
.calledWith(event.sourceBlockId)
.mockResolvedValueOnce([
ergoTestUtils.generateRandomId(),
event.sourceTxId,
ergoTestUtils.generateRandomId(),
]);
// mock 'getBlockInfo'
const blockInfo: BlockInfo = {
hash: event.sourceBlockId,
parentHash: ergoTestUtils.generateRandomId(),
height: 2000000,
};
const getBlockInfoSpy = spyOn(network, 'getBlockInfo');
when(getBlockInfoSpy)
.calledWith(event.sourceBlockId)
.mockResolvedValueOnce(blockInfo);
// mock 'getTransaction' (the tx itself doesn't matter)
const mockedTx = ergoTestUtils.deserializeTransaction(
transactionTestData.transaction2SignedSerialized
);
const getTransactionSpy = spyOn(network, 'getTransaction');
when(getTransactionSpy)
.calledWith(event.sourceTxId, event.sourceBlockId)
.mockResolvedValueOnce(mockedTx);

// mock network extractor to return event data
const extractorSpy = spyOn(network.extractor, 'get');
extractorSpy.mockReturnValueOnce(event as unknown as RosenData);

// run test
const ergoChain = generateChainObject(network);
const result = await ergoChain.verifyEvent(event, feeConfig);

// check returned value
expect(result).toEqual(false);
});
});

describe('verifyTransactionExtraConditions', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/ergo-explorer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/ergo-explorer-network",
"version": "1.0.2",
"version": "1.0.3",
"description": "ergo explorer network package for rosen ergo chain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -25,7 +25,7 @@
"@rosen-bridge/rosen-extractor": "^0.1.11",
"@rosen-bridge/tokens": "^0.2.0",
"@rosen-chains/abstract-chain": "^1.0.1",
"@rosen-chains/ergo": "^1.0.2",
"@rosen-chains/ergo": "^1.0.3",
"@rosen-clients/ergo-explorer": "^1.0.2",
"ergo-lib-wasm-nodejs": "^0.24.1",
"it-all": "^3.0.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/ergo-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/ergo-node-network",
"version": "1.0.2",
"version": "1.0.3",
"description": "ergo node network package for rosen ergo chain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -25,7 +25,7 @@
"@rosen-bridge/rosen-extractor": "^0.1.11",
"@rosen-bridge/tokens": "^0.2.0",
"@rosen-chains/abstract-chain": "^1.0.1",
"@rosen-chains/ergo": "^1.0.2",
"@rosen-chains/ergo": "^1.0.3",
"@rosen-clients/ergo-node": "^1.0.3",
"ergo-lib-wasm-nodejs": "^0.24.1",
"it-all": "^3.0.1"
Expand Down

0 comments on commit 5545da0

Please sign in to comment.