Skip to content

Commit

Permalink
Merge branch '73-fix-wasm-output_candidates-iteration' into 'dev'
Browse files Browse the repository at this point in the history
fix wasm box iteration

Closes #73

See merge request ergo/rosen-bridge/rosen-chains!83
  • Loading branch information
vorujack committed Dec 23, 2023
2 parents 2da1905 + 6bf8fbd commit 786a703
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
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.

62 changes: 37 additions & 25 deletions packages/chains/ergo/lib/ErgoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
const forbiddenBoxIds = unsignedTransactions.flatMap((paymentTx) => {
const tx = Serializer.deserialize(paymentTx.txBytes).unsigned_tx();
const ids: string[] = [];
for (let i = 0; i < tx.inputs().len(); i++)
ids.push(tx.inputs().get(i).box_id().to_str());
const inputs = tx.inputs();
for (let i = 0; i < inputs.len(); i++)
ids.push(inputs.get(i).box_id().to_str());
return ids;
});

Expand Down Expand Up @@ -338,21 +339,23 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
tokens: [],
};
// extract output boxes assets
for (let i = 0; i < tx.output_candidates().len(); i++) {
const output = tx.output_candidates().get(i);
const outputCandidates = tx.output_candidates();
for (let i = 0; i < outputCandidates.len(); i++) {
const output = outputCandidates.get(i);
outputAssets.nativeToken += BigInt(output.value().as_i64().to_str());
for (let j = 0; j < output.tokens().len(); j++) {
const outputTokens = output.tokens();
for (let j = 0; j < outputTokens.len(); j++) {
const targetToken = outputAssets.tokens.find(
(item) => item.id === output.tokens().get(j).id().to_str()
(item) => item.id === outputTokens.get(j).id().to_str()
);
if (targetToken)
targetToken.value += BigInt(
output.tokens().get(j).amount().as_i64().to_str()
outputTokens.get(j).amount().as_i64().to_str()
);
else
outputAssets.tokens.push({
id: output.tokens().get(j).id().to_str(),
value: BigInt(output.tokens().get(j).amount().as_i64().to_str()),
id: outputTokens.get(j).id().to_str(),
value: BigInt(outputTokens.get(j).amount().as_i64().to_str()),
});
}
}
Expand All @@ -372,15 +375,17 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
const tx = Serializer.deserialize(transaction.txBytes).unsigned_tx();

const order: PaymentOrder = [];
for (let i = 0; i < tx.output_candidates().len(); i++) {
const output = tx.output_candidates().get(i);
const outputCandidates = tx.output_candidates();
const outputCandidatesLength = outputCandidates.len();
for (let i = 0; i < outputCandidatesLength; i++) {
const output = outputCandidates.get(i);
const assets = ErgoUtils.getBoxAssets(output);
const r4Value = output.register_value(4)?.to_coll_coll_byte()[0];

// skip change box and fee box
if (
output.ergo_tree().to_base16_bytes() === ErgoChain.feeBoxErgoTree ||
(tx.output_candidates().len() - i === 2 &&
(outputCandidatesLength - i === 2 &&
output.ergo_tree().to_base16_bytes() ===
wasm.Address.from_base58(this.configs.addresses.lock)
.to_ergo_tree()
Expand Down Expand Up @@ -454,8 +459,9 @@ 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);
const outputs = tx.outputs();
for (let i = 0; i < outputs.len(); i++) {
const box = outputs.get(i);
if (blockHeight - box.creation_height() > NUMBER_OF_BLOCKS_PER_YEAR) {
this.logger.info(
`Event [${eventId}] is not valid, box [${box
Expand Down Expand Up @@ -591,8 +597,9 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
: Serializer.deserialize(transaction.txBytes).unsigned_tx();
// check if any input is spent or invalid
let valid = true;
for (let i = 0; i < tx.inputs().len(); i++) {
const box = tx.inputs().get(i);
const inputs = tx.inputs();
for (let i = 0; i < inputs.len(); i++) {
const box = inputs.get(i);
valid =
valid &&
(await this.network.isBoxUnspentAndValid(box.box_id().to_str()));
Expand Down Expand Up @@ -859,17 +866,20 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
// deserialize transaction

// iterate over tx inputs
for (let i = 0; i < tx.inputs().len(); i++) {
const inputs = tx.inputs();
const outputs = tx.outputs();
for (let i = 0; i < inputs.len(); i++) {
let trackedBox: wasm.ErgoBox | undefined;
// iterate over tx outputs
for (let j = 0; j < tx.outputs().len(); j++) {
const output = tx.outputs().get(j);
for (let j = 0; j < outputs.len(); j++) {
const output = outputs.get(j);
// check if box satisfy conditions
if (output.ergo_tree().to_base16_bytes() !== ergoTree) continue;
if (tokenId) {
const tokenIds: Array<string> = [];
for (let k = 0; k < output.tokens().len(); k++)
tokenIds.push(output.tokens().get(k).id().to_str());
const outputTokens = output.tokens();
for (let k = 0; k < outputTokens.len(); k++)
tokenIds.push(outputTokens.get(k).id().to_str());

if (!tokenIds.includes(tokenId)) continue;
}
Expand All @@ -880,7 +890,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
}

// add input box with serialized tracked box to trackMap
const input = tx.inputs().get(i);
const input = inputs.get(i);
trackMap.set(input.box_id().to_str(), trackedBox);
}
});
Expand Down Expand Up @@ -995,8 +1005,10 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
);

const order: PaymentOrder = [];
for (let i = 0; i < tx.outputs().len(); i++) {
const output = tx.outputs().get(i);
const outputs = tx.outputs();
const outputsLength = outputs.len();
for (let i = 0; i < outputsLength; i++) {
const output = outputs.get(i);
const boxErgoTree = output.ergo_tree().to_base16_bytes();
const lockErgoTree = wasm.Address.from_base58(this.configs.addresses.lock)
.to_ergo_tree()
Expand All @@ -1005,7 +1017,7 @@ class ErgoChain extends AbstractUtxoChain<wasm.ErgoBox> {
// skip change box and fee box
if (
boxErgoTree === ErgoChain.feeBoxErgoTree ||
(tx.outputs().len() - i === 2 && boxErgoTree === lockErgoTree)
(outputsLength - i === 2 && boxErgoTree === lockErgoTree)
)
continue;

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": "3.2.1",
"version": "3.2.2",
"description": "this project contains ergo chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
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": "3.2.1",
"version": "3.2.2",
"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": "^3.0.0",
"@rosen-bridge/tokens": "^1.0.0",
"@rosen-chains/abstract-chain": "^3.2.1",
"@rosen-chains/ergo": "^3.2.1",
"@rosen-chains/ergo": "^3.2.2",
"@rosen-clients/ergo-explorer": "^1.0.3",
"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": "3.2.1",
"version": "3.2.2",
"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": "^3.0.0",
"@rosen-bridge/tokens": "^1.0.0",
"@rosen-chains/abstract-chain": "^3.2.1",
"@rosen-chains/ergo": "^3.2.1",
"@rosen-chains/ergo": "^3.2.2",
"@rosen-clients/ergo-node": "^1.0.4",
"ergo-lib-wasm-nodejs": "^0.24.1",
"it-all": "^3.0.1"
Expand Down

0 comments on commit 786a703

Please sign in to comment.