Skip to content

Commit

Permalink
Merge branch '42-chains-getconfirmationstatus-improvement' into 'dev'
Browse files Browse the repository at this point in the history
improve handling tx confirmations

Closes #42

See merge request ergo/rosen-bridge/rosen-chains!40
  • Loading branch information
vorujack committed Aug 14, 2023
2 parents b47cf93 + f8d81ec commit b1eccf8
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 280 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.

26 changes: 24 additions & 2 deletions packages/abstract-chain/lib/AbstractChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PaymentTransaction,
SigningStatus,
TransactionAssetBalance,
TransactionType,
} from './types';

abstract class AbstractChain {
Expand Down Expand Up @@ -40,7 +41,7 @@ abstract class AbstractChain {
*/
abstract generateTransaction: (
eventId: string,
txType: string,
txType: TransactionType,
order: PaymentOrder,
unsignedTransactions: PaymentTransaction[],
serializedSignedTransactions: string[],
Expand Down Expand Up @@ -132,6 +133,27 @@ abstract class AbstractChain {
requiredSign: number
) => Promise<PaymentTransaction>;

/**
* @param transactionType type of the transaction
* @returns required number of confirmation
*/
getTxRequiredConfirmation = (transactionType: TransactionType): number => {
switch (transactionType) {
case TransactionType.payment:
return this.configs.confirmations.payment;
case TransactionType.coldStorage:
return this.configs.confirmations.cold;
case TransactionType.lock:
return this.configs.confirmations.observation;
case TransactionType.manual:
return this.configs.confirmations.manual;
default:
throw Error(
`Confirmation for type [${transactionType}] is not defined in abstract chain`
);
}
};

/**
* extracts confirmation status for a transaction
* @param transactionId the transaction id
Expand All @@ -140,7 +162,7 @@ abstract class AbstractChain {
*/
abstract getTxConfirmationStatus: (
transactionId: string,
transactionType: string
transactionType: TransactionType
) => Promise<ConfirmationStatus>;

/**
Expand Down
26 changes: 16 additions & 10 deletions packages/abstract-chain/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
interface ConfirmationConfigs {
observation: number;
payment: number;
cold: number;
manual: number;
}
interface ChainConfigs {
fee: bigint;
observationTxConfirmation: number;
paymentTxConfirmation: number;
coldTxConfirmation: number;
confirmations: ConfirmationConfigs;
lockAddress: string;
coldStorageAddress: string;
rwtId: string;
Expand Down Expand Up @@ -74,7 +78,7 @@ interface PaymentTransaction {
txId: string;
eventId: string;
txBytes: Uint8Array;
txType: string;
txType: TransactionType;
}

interface PaymentTransactionJsonModel {
Expand All @@ -91,14 +95,16 @@ enum ConfirmationStatus {
NotFound,
}

class TransactionTypes {
static payment = 'payment';
static reward = 'reward';
static coldStorage = 'cold-storage';
static lock = 'lock';
enum TransactionType {
payment = 'payment',
reward = 'reward',
coldStorage = 'cold-storage',
lock = 'lock',
manual = 'manual',
}

export {
ConfirmationConfigs,
ChainConfigs,
CoveringBoxes,
TokenInfo,
Expand All @@ -112,6 +118,6 @@ export {
PaymentTransaction,
PaymentTransactionJsonModel,
ConfirmationStatus,
TransactionTypes,
TransactionType,
SigningStatus,
};
2 changes: 1 addition & 1 deletion packages/abstract-chain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/abstract-chain",
"version": "0.1.12",
"version": "0.1.13",
"description": "this project contains abstract classes to implement any chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
18 changes: 13 additions & 5 deletions packages/abstract-chain/tests/AbstractChain.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import TestChain from './TestChain';
import TestChainNetwork from './network/TestChainNetwork';
import { AssetBalance, ChainConfigs, PaymentTransaction } from '../lib';
import {
AssetBalance,
ChainConfigs,
PaymentTransaction,
TransactionType,
} from '../lib';
import { when } from 'jest-when';

const spyOn = jest.spyOn;
Expand All @@ -9,9 +14,12 @@ describe('AbstractChain', () => {
const generateChainObject = (network: TestChainNetwork) => {
const config: ChainConfigs = {
fee: 100n,
observationTxConfirmation: 5,
paymentTxConfirmation: 6,
coldTxConfirmation: 7,
confirmations: {
observation: 5,
payment: 6,
cold: 7,
manual: 8,
},
lockAddress: 'lock_addr',
coldStorageAddress: 'cold_addr',
rwtId: 'rwt',
Expand All @@ -25,7 +33,7 @@ describe('AbstractChain', () => {
txId: 'mockedTxId',
eventId: 'mockedNetworkId',
txBytes: Buffer.from('mockedTxBytes'),
txType: 'payment',
txType: TransactionType.payment,
};
const network = new TestChainNetwork();

Expand Down
9 changes: 6 additions & 3 deletions packages/abstract-chain/tests/AbstractUtxoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ describe('AbstractUtxoChain', () => {
const generateChainObject = (network: TestUtxoChainNetwork) => {
const config: ChainConfigs = {
fee: 100n,
observationTxConfirmation: 5,
paymentTxConfirmation: 6,
coldTxConfirmation: 7,
confirmations: {
observation: 5,
payment: 6,
cold: 7,
manual: 8,
},
lockAddress: 'lock_addr',
coldStorageAddress: 'cold_addr',
rwtId: 'rwt',
Expand Down
26 changes: 6 additions & 20 deletions packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
SigningStatus,
SinglePayment,
TransactionAssetBalance,
TransactionTypes,
TransactionType,
UnexpectedApiError,
} from '@rosen-chains/abstract-chain';
import { blake2b } from 'blakejs';
Expand Down Expand Up @@ -101,7 +101,7 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
*/
generateTransaction = async (
eventId: string,
txType: string,
txType: TransactionType,
order: PaymentOrder,
unsignedTransactions: PaymentTransaction[],
serializedSignedTransactions: string[]
Expand Down Expand Up @@ -628,26 +628,12 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
*/
getTxConfirmationStatus = async (
transactionId: string,
transactionType: string
transactionType: TransactionType
): Promise<ConfirmationStatus> => {
let expectedConfirmation = 0;
switch (transactionType) {
case TransactionTypes.lock:
expectedConfirmation = this.configs.observationTxConfirmation;
break;
case TransactionTypes.payment:
case TransactionTypes.reward:
expectedConfirmation = this.configs.paymentTxConfirmation;
break;
case TransactionTypes.coldStorage:
expectedConfirmation = this.configs.coldTxConfirmation;
break;
default:
throw new Error(`Transaction type [${transactionType}] is not defined`);
}

const requiredConfirmation =
this.getTxRequiredConfirmation(transactionType);
const confirmation = await this.network.getTxConfirmation(transactionId);
if (confirmation >= expectedConfirmation)
if (confirmation >= requiredConfirmation)
return ConfirmationStatus.ConfirmedEnough;
else if (confirmation === -1) return ConfirmationStatus.NotFound;
else return ConfirmationStatus.NotConfirmedEnough;
Expand Down
15 changes: 9 additions & 6 deletions packages/chains/cardano/lib/CardanoTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { PaymentTransaction } from '@rosen-chains/abstract-chain';
import {
PaymentTransaction,
PaymentTransactionJsonModel,
TransactionType,
} from '@rosen-chains/abstract-chain';
import { CARDANO_CHAIN } from './constants';
import * as JSONBigInt from 'json-bigint';

class CardanoTransaction implements PaymentTransaction {
eventId: string;
network: string;
txBytes: Uint8Array;
txId: string;
txType: string;
txType: TransactionType;

constructor(
eventId: string,
txBytes: Uint8Array,
txId: string,
txType: string
txType: TransactionType
) {
this.network = CARDANO_CHAIN;
this.eventId = eventId;
Expand All @@ -27,12 +30,12 @@ class CardanoTransaction implements PaymentTransaction {
* @returns CardanoTransaction object
*/
static fromJson = (jsonString: string): CardanoTransaction => {
const obj = JSON.parse(jsonString);
const obj = JSON.parse(jsonString) as PaymentTransactionJsonModel;
return new CardanoTransaction(
obj.eventId,
Buffer.from(obj.txBytes, 'hex'),
obj.txId,
obj.txType
obj.txType as TransactionType
);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/chains/cardano/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano",
"version": "0.1.12",
"version": "0.1.13",
"description": "this project contains cardano chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -23,7 +23,7 @@
"@emurgo/cardano-serialization-lib-nodejs": "^11.3.1",
"@rosen-bridge/logger-interface": "^0.1.0",
"@rosen-bridge/rosen-extractor": "^0.1.8",
"@rosen-chains/abstract-chain": "^0.1.12",
"@rosen-chains/abstract-chain": "^0.1.13",
"bech32": "^2.0.0",
"blake2b": "^2.1.3",
"json-bigint": "^1.0.0"
Expand Down
Loading

0 comments on commit b1eccf8

Please sign in to comment.