Skip to content

Commit

Permalink
Merge branch '125-creation-height-verification' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "add creation_height verification in ErgoChain.verifyTransactionExtraConditions"

Closes #125

See merge request ergo/rosen-bridge/rosen-chains!153
  • Loading branch information
vorujack committed Nov 11, 2024
2 parents 08c6fab + 4a0e449 commit e0969cf
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 12 deletions.
File renamed without changes.
2 changes: 0 additions & 2 deletions .changeset/violet-bottles-double.md

This file was deleted.

10 changes: 5 additions & 5 deletions package-lock.json

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

6 changes: 6 additions & 0 deletions packages/chains/ergo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @rosen-chains/ergo

## 10.1.1

### Patch Changes

- Add creation height verification

## 10.1.0

### Minor Changes
Expand Down
20 changes: 20 additions & 0 deletions packages/chains/ergo/lib/ErgoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {
/**
* verifies additional conditions for a PaymentTransaction
* 1. change boxes should not have register
* 2. output boxes should have higher creation height than inputs
* @param transaction the PaymentTransaction
* @param signingStatus the signing status of transaction
* @returns true if the transaction verified
Expand All @@ -578,6 +579,13 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {
tx = Serializer.deserialize(transaction.txBytes).unsigned_tx();
}

const ergoTx = transaction as ErgoTransaction;
const maxCreationHeight = Math.max(
...ergoTx.inputBoxes.map((serializedBox) =>
wasm.ErgoBox.sigma_parse_bytes(serializedBox).creation_height()
)
);

const outputBoxes = tx.output_candidates();
const lockErgoTree = wasm.Address.from_base58(this.configs.addresses.lock)
.to_ergo_tree()
Expand All @@ -601,6 +609,18 @@ class ErgoChain extends AbstractUtxoChain<wasm.Transaction, wasm.ErgoBox> {
}
}

for (let i = 0; i < outputBoxes.len(); i++) {
const output = outputBoxes.get(i);
if (output.creation_height() < maxCreationHeight) {
this.logger.warn(
`Tx [${
transaction.txId
}] is not verified: Output box at index [${i}] has low creation height. Expected more than [${maxCreationHeight}] found [${output.creation_height()}]`
);
return false;
}
}

return true;
};

Expand Down
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": "10.1.0",
"version": "10.1.1",
"description": "this project contains ergo chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
48 changes: 48 additions & 0 deletions packages/chains/ergo/tests/ErgoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,54 @@ describe('ErgoChain', () => {
// check returned value
expect(result).toEqual(false);
});

/**
* @target ErgoChain.verifyTransactionExtraConditions should return false
* when output creation height is less than an input
* @dependencies
* @scenario
* - mock valid PaymentTransaction
* - mock a config with valid lockAddress
* - run test
* - check returned value
* @expected
* - it should return false
*/
it('should return false when output creation height is less than an input', async () => {
// mock PaymentTransaction
const paymentTx = ErgoTransaction.fromJson(
transactionTestData.transaction7PaymentTransaction
);

// mock a config with valid lockAddress
const config: ErgoConfigs = {
fee: 1200000n,
confirmations: ergoTestUtils.defaultConfirmations,
addresses: {
lock: 'nB3L2PD3LG4ydEj62n9aymRyPCEbkBdzaubgvCWDH2oxHxFBfAUy9GhWDvteDbbUh5qhXxnW8R46qmEiZfkej8gt4kZYvbeobZJADMrWXwFJTsZ17euEcoAp3KDk31Q26okFpgK9SKdi4',
cold: 'cold_addr',
permit: 'permit_addr',
fraud: 'fraud_addr',
},
rwtId: ergoTestUtils.rwtId,
minBoxValue: 1000000n,
eventTxConfirmation: 18,
};

// run test
const ergoChain = new ErgoChain(
network,
config,
ergoTestUtils.testTokenMap,
ergoTestUtils.defaultSignFunction
);
const result = await ergoChain.verifyTransactionExtraConditions(
paymentTx
);

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

describe('isTxValid', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/chains/ergo/tests/transactionTestData.ts

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions packages/networks/ergo-explorer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @rosen-chains/ergo-explorer-network

## 9.0.5

### Patch Changes

- Update dependencies
- @rosen-chains/ergo@10.1.1

## 9.0.4

### Patch Changes
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": "9.0.4",
"version": "9.0.5",
"description": "ergo explorer network package for rosen ergo chain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -23,7 +23,7 @@
"@rosen-bridge/abstract-logger": "^2.0.1",
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-chains/abstract-chain": "^11.0.0",
"@rosen-chains/ergo": "^10.1.0",
"@rosen-chains/ergo": "^10.1.1",
"@rosen-clients/ergo-explorer": "^1.1.1",
"ergo-lib-wasm-nodejs": "^0.24.1",
"it-all": "^3.0.1"
Expand Down
7 changes: 7 additions & 0 deletions packages/networks/ergo-node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @rosen-chains/ergo-node-network

## 9.0.5

### Patch Changes

- Update dependencies
- @rosen-chains/ergo@10.1.1

## 9.0.4

### Patch Changes
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": "9.0.4",
"version": "9.0.5",
"description": "ergo node network package for rosen ergo chain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -23,7 +23,7 @@
"@rosen-bridge/abstract-logger": "^2.0.1",
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-chains/abstract-chain": "^11.0.0",
"@rosen-chains/ergo": "^10.1.0",
"@rosen-chains/ergo": "^10.1.1",
"@rosen-clients/ergo-node": "^1.1.1",
"ergo-lib-wasm-nodejs": "^0.24.1",
"it-all": "^3.0.1"
Expand Down

0 comments on commit e0969cf

Please sign in to comment.