diff --git a/package-lock.json b/package-lock.json index b5c0b48..3b20339 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8395,7 +8395,7 @@ }, "packages/abstract-chain": { "name": "@rosen-chains/abstract-chain", - "version": "1.0.0", + "version": "1.0.1", "license": "GPL-3.0", "dependencies": { "@rosen-bridge/json-bigint": "^0.1.0", @@ -8421,14 +8421,14 @@ }, "packages/chains/cardano": { "name": "@rosen-chains/cardano", - "version": "1.0.0", + "version": "1.0.1", "license": "GPL-3.0", "dependencies": { "@emurgo/cardano-serialization-lib-nodejs": "^11.3.1", "@rosen-bridge/json-bigint": "^0.1.0", "@rosen-bridge/logger-interface": "^0.1.0", "@rosen-bridge/rosen-extractor": "^0.1.11", - "@rosen-chains/abstract-chain": "^1.0.0", + "@rosen-chains/abstract-chain": "^1.0.1", "bech32": "^2.0.0", "blake2b": "^2.1.3" }, @@ -8451,13 +8451,13 @@ }, "packages/chains/ergo": { "name": "@rosen-chains/ergo", - "version": "1.0.1", + "version": "1.0.3", "license": "GPL-3.0", "dependencies": { "@rosen-bridge/json-bigint": "^0.1.0", "@rosen-bridge/minimum-fee": "^0.1.13", "@rosen-bridge/rosen-extractor": "^0.1.11", - "@rosen-chains/abstract-chain": "^1.0.0", + "@rosen-chains/abstract-chain": "^1.0.1", "blakejs": "^1.2.1", "ergo-lib-wasm-nodejs": "^0.24.1" }, @@ -8479,7 +8479,7 @@ }, "packages/networks/cardano-koios": { "name": "@rosen-chains/cardano-koios-network", - "version": "1.0.0", + "version": "1.0.1", "license": "GPL-3.0", "dependencies": { "@emurgo/cardano-serialization-lib-nodejs": "^11.3.1", @@ -8487,8 +8487,8 @@ "@rosen-bridge/logger-interface": "^0.1.0", "@rosen-bridge/rosen-extractor": "^0.1.11", "@rosen-bridge/tokens": "^0.2.0", - "@rosen-chains/abstract-chain": "^1.0.0", - "@rosen-chains/cardano": "^1.0.0", + "@rosen-chains/abstract-chain": "^1.0.1", + "@rosen-chains/cardano": "^1.0.1", "@rosen-clients/cardano-koios": "^1.0.3" }, "devDependencies": { @@ -8526,15 +8526,15 @@ }, "packages/networks/ergo-explorer": { "name": "@rosen-chains/ergo-explorer-network", - "version": "1.0.1", + "version": "1.0.3", "license": "GPL-3.0", "dependencies": { "@rosen-bridge/json-bigint": "^0.1.0", "@rosen-bridge/logger-interface": "^0.1.0", "@rosen-bridge/rosen-extractor": "^0.1.11", "@rosen-bridge/tokens": "^0.2.0", - "@rosen-chains/abstract-chain": "^1.0.0", - "@rosen-chains/ergo": "^1.0.1", + "@rosen-chains/abstract-chain": "^1.0.1", + "@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" @@ -8568,15 +8568,15 @@ }, "packages/networks/ergo-node": { "name": "@rosen-chains/ergo-node-network", - "version": "1.0.1", + "version": "1.0.3", "license": "GPL-3.0", "dependencies": { "@rosen-bridge/json-bigint": "^0.1.0", "@rosen-bridge/logger-interface": "^0.1.0", "@rosen-bridge/rosen-extractor": "^0.1.11", "@rosen-bridge/tokens": "^0.2.0", - "@rosen-chains/abstract-chain": "^1.0.0", - "@rosen-chains/ergo": "^1.0.1", + "@rosen-chains/abstract-chain": "^1.0.1", + "@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" diff --git a/packages/chains/ergo/lib/ErgoChain.ts b/packages/chains/ergo/lib/ErgoChain.ts index 38c1993..3ce6d49 100644 --- a/packages/chains/ergo/lib/ErgoChain.ts +++ b/packages/chains/ergo/lib/ErgoChain.ts @@ -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'; @@ -454,6 +454,17 @@ class ErgoChain extends AbstractUtxoChain { ); 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') ); diff --git a/packages/chains/ergo/lib/constants.ts b/packages/chains/ergo/lib/constants.ts index 6f778c6..2baa032 100644 --- a/packages/chains/ergo/lib/constants.ts +++ b/packages/chains/ergo/lib/constants.ts @@ -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 }; diff --git a/packages/chains/ergo/package.json b/packages/chains/ergo/package.json index 4f4ac8d..4d0a8fd 100644 --- a/packages/chains/ergo/package.json +++ b/packages/chains/ergo/package.json @@ -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", diff --git a/packages/chains/ergo/tests/ErgoChain.spec.ts b/packages/chains/ergo/tests/ErgoChain.spec.ts index fda3786..7609b8a 100644 --- a/packages/chains/ergo/tests/ErgoChain.spec.ts +++ b/packages/chains/ergo/tests/ErgoChain.spec.ts @@ -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, @@ -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', () => { diff --git a/packages/networks/ergo-explorer/package.json b/packages/networks/ergo-explorer/package.json index 1d5f17f..c970813 100644 --- a/packages/networks/ergo-explorer/package.json +++ b/packages/networks/ergo-explorer/package.json @@ -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", @@ -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" diff --git a/packages/networks/ergo-node/package.json b/packages/networks/ergo-node/package.json index 8f3f8d3..fc2890c 100644 --- a/packages/networks/ergo-node/package.json +++ b/packages/networks/ergo-node/package.json @@ -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", @@ -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"