Skip to content

Commit

Permalink
feat: update off chain code for compatibility decouple validator
Browse files Browse the repository at this point in the history
  • Loading branch information
cuonglv-smartosc committed May 4, 2024
1 parent 15a8669 commit e0a0b4b
Show file tree
Hide file tree
Showing 30 changed files with 618 additions and 201 deletions.
6 changes: 6 additions & 0 deletions cardano/gateway/src/constant/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export const ORDER_MAPPING_CHANNEL = {

export const CHANNEL_ID_PREFIX = 'channel';

export const KEY_CHANNEL_PREFIX = 'channels';

export const KEY_CHANNEL_END_PREFIX = 'channelEnds';

export const PORT_ID_PREFIX = 'port';

export const KEY_PORT_PREFIX = 'ports';

export const TRANSFER_MODULE_PORT = 100;
2 changes: 2 additions & 0 deletions cardano/gateway/src/constant/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export const ATTRIBUTE_KEY_CLIENT = {
title: 'title',
};
export const CLIENT_ID_PREFIX = 'ibc_client';
export const KEY_CLIENT_PREFIX = 'clients';
export const KEY_CLIENT_STATE = 'clientState';
1 change: 1 addition & 0 deletions cardano/gateway/src/constant/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export const STATE_MAPPING_CONNECTION = {
};

export const CONNECTION_ID_PREFIX = 'connection';
export const KEY_CONNECTION_PREFIX = 'connections';
31 changes: 29 additions & 2 deletions cardano/gateway/src/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ import {
ATTRIBUTE_KEY_CONNECTION,
STATE_MAPPING_CONNECTION,
CONNECTION_ID_PREFIX,
KEY_CONNECTION_PREFIX,
} from './connection';
import {
EVENT_TYPE_CHANNEL,
ATTRIBUTE_KEY_CHANNEL,
CHANNEL_ID_PREFIX,
PORT_ID_PREFIX,
TRANSFER_MODULE_PORT,
KEY_CHANNEL_PREFIX,
KEY_PORT_PREFIX,
KEY_CHANNEL_END_PREFIX,
} from './channel';
import { EVENT_TYPE_CLIENT, ATTRIBUTE_KEY_CLIENT, CLIENT_ID_PREFIX } from './client';
import { EVENT_TYPE_CLIENT, ATTRIBUTE_KEY_CLIENT, CLIENT_ID_PREFIX, KEY_CLIENT_PREFIX, KEY_CLIENT_STATE } from './client';
import { EVENT_TYPE_SPO } from './spo';
import { REDEEMER_EMPTY_DATA, REDEEMER_TYPE } from './redeemer';
import { EVENT_TYPE_PACKET, ATTRIBUTE_KEY_PACKET } from './packet';
import {
EVENT_TYPE_PACKET,
ATTRIBUTE_KEY_PACKET,
KEY_PACKET_ACK_PREFIX,
KEY_PACKET_COMMITMENT_PREFIX,
KEY_PACKET_RECEIPT_PREFIX,
KEY_SEQUENCE_PREFIX,
} from './packet';
import {
BLOCKID_FLAG_ABSENT,
BLOCKID_FLAG_COMMIT,
Expand All @@ -32,29 +43,45 @@ export const CHANNEL_TOKEN_PREFIX = '6368616e6e656c'; // fromText("channel")
export const DEFAULT_IDENTIFIER_VERSION = '31'; // fromText("1")
export const DEFAULT_FEATURES_VERSION_ORDER_ORDERED = '4f524445525f4f524445524544'; // fromText("ORDER_ORDERED")
export const DEFAULT_FEATURES_VERSION_ORDER_UNORDERED = '4f524445525f554e4f524445524544'; // fromText("connection")
export const DEFAULT_MERKLE_PREFIX = "696263"; // fromText("ibc")
export const ACK_RESULT = '01';

export const LOVELACE = 'lovelace';

export const EMULATOR_ENV = 'emulator';

export const MAX_EXPECTED_TIME_PER_BLOCK = 4000000000;

export {
EVENT_TYPE_CONNECTION,
ATTRIBUTE_KEY_CONNECTION,
STATE_MAPPING_CONNECTION,
CONNECTION_ID_PREFIX,
KEY_CONNECTION_PREFIX,

EVENT_TYPE_CHANNEL,
ATTRIBUTE_KEY_CHANNEL,
CHANNEL_ID_PREFIX,
PORT_ID_PREFIX,
TRANSFER_MODULE_PORT,
KEY_CHANNEL_PREFIX,
KEY_CHANNEL_END_PREFIX,

KEY_PORT_PREFIX,
REDEEMER_TYPE,
REDEEMER_EMPTY_DATA,
EVENT_TYPE_CLIENT,
ATTRIBUTE_KEY_CLIENT,
CLIENT_ID_PREFIX,
KEY_CLIENT_PREFIX,
KEY_CLIENT_STATE,
EVENT_TYPE_SPO,
EVENT_TYPE_PACKET,
ATTRIBUTE_KEY_PACKET,
KEY_PACKET_ACK_PREFIX,
KEY_PACKET_COMMITMENT_PREFIX,
KEY_PACKET_RECEIPT_PREFIX,
KEY_SEQUENCE_PREFIX,
// block
BLOCKID_FLAG_ABSENT,
BLOCKID_FLAG_COMMIT,
Expand Down
5 changes: 5 additions & 0 deletions cardano/gateway/src/constant/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ export const ATTRIBUTE_KEY_PACKET = {
PACKET_CHANNEL_ORDERING: 'packet_channel_ordering',
PACKET_CONNECTION: 'packet_connection',
};

export const KEY_PACKET_COMMITMENT_PREFIX = 'commitments';
export const KEY_PACKET_ACK_PREFIX = 'acks';
export const KEY_PACKET_RECEIPT_PREFIX = 'receipts';
export const KEY_SEQUENCE_PREFIX = 'sequences';
11 changes: 10 additions & 1 deletion cardano/gateway/src/shared/helpers/channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { KEY_CHANNEL_END_PREFIX, KEY_CHANNEL_PREFIX, KEY_PORT_PREFIX } from '../../constant';
import { AuthToken } from '../types/auth-token';
import { hashSha3_256, hexToBytes } from './hex';
import { convertString2Hex, hashSha3_256, hexToBytes } from './hex';

export function getChannelIdByTokenName(tokenName: string, baseToken: AuthToken, prefix: string): string {
const baseTokenPart = hashSha3_256(baseToken.policyId + baseToken.name).slice(0, 40);
Expand All @@ -15,3 +16,11 @@ export function getChannelIdByTokenName(tokenName: string, baseToken: AuthToken,
export function getConnectionIdFromConnectionHops(channelHops: string): string {
return Buffer.from(hexToBytes(channelHops)).toString();
}

export function channelPathForPacket(portId: string, channelId: string) {
return `${KEY_PORT_PREFIX}/${portId}/${KEY_CHANNEL_PREFIX}/${channelId}`;
}

export function channelPath(portId: string, channelId: string): string {
return `${KEY_CHANNEL_END_PREFIX}/${channelPathForPacket(portId, channelId)}`;
}
55 changes: 55 additions & 0 deletions cardano/gateway/src/shared/helpers/client-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { ClientState } from '../types/client-state-types';
import { convertHex2String, convertString2Hex } from './hex';
import { convertToProofType } from './proof_types';
import { GrpcInvalidArgumentException } from 'nestjs-grpc-exceptions';
import { KEY_CLIENT_PREFIX, KEY_CLIENT_STATE } from '~@/constant';
import { ClientState as CardanoClientState } from '@plus/proto-types/build/ibc/lightclients/ouroboros/ouroboros';

export function normalizeClientStateFromDatum(clientState: ClientState): ClientStateTendermint {
const clientStateTendermint: ClientStateTendermint = {
chain_id: clientState.chainId,
Expand Down Expand Up @@ -170,3 +173,55 @@ export function isExpired(cs: ClientState, latestTimestamp: bigint, now: bigint)
const expirationTime = latestTimestamp + cs.trustingPeriod;
return expirationTime < now;
}

export function clientStatePath(clientId: string): string {
return `${KEY_CLIENT_PREFIX}/${clientId}/${KEY_CLIENT_STATE}`;
}

export function getCardanoClientStateForVerifyProofRedeemer(cardanoClientState: CardanoClientState): CardanoClientState {
return {
/** Chain id */
chain_id: convertHex2String(cardanoClientState.chain_id),
/** Latest height the client was updated to */
latest_height: cardanoClientState.latest_height,
/** Block height when the client was frozen due to a misbehaviour */
frozen_height: cardanoClientState.frozen_height,
/** To support finality, this state will be mark as finality after `valid_after` slots, default 0, unit: slot */
valid_after: cardanoClientState.valid_after,
/** Time when chain start */
genesis_time: cardanoClientState.genesis_time,
/** Epoch number of current chain state */
current_epoch: cardanoClientState.current_epoch,
/** Number of slots of this current epoch */
epoch_length: cardanoClientState.epoch_length,
/** Number of slots of per KES period */
slot_per_kes_period: cardanoClientState.slot_per_kes_period,
/** Current epoch validator set */
current_validator_set: cardanoClientState.current_validator_set.map((validator) => ({
/** vrf key hash of pool operator */
vrf_key_hash: convertHex2String(validator.vrf_key_hash),
/** pool id of operator */
pool_id: convertHex2String(validator.pool_id),
})),
/** Next epoch validator set */
next_validator_set: cardanoClientState.next_validator_set.map((validator) => ({
/** vrf key hash of pool operator */
vrf_key_hash: convertHex2String(validator.vrf_key_hash),
/** pool id of operator */
pool_id: convertHex2String(validator.pool_id),
})),
trusting_period: cardanoClientState.trusting_period,
/** Path at which next upgraded client will be committed. */
upgrade_path: cardanoClientState.upgrade_path.map((path) => convertHex2String(path)),
/** IBC related auth token policy configs */
token_configs: {/** IBC handler token uint (policyID + name), in hex format */
handler_token_unit: convertHex2String(cardanoClientState.token_configs.handler_token_unit),
/** IBC client token policyID, in hex format */
client_policy_id: convertHex2String(cardanoClientState.token_configs.client_policy_id),
/** IBC connection token policyID, in hex format */
connection_policy_id: convertHex2String(cardanoClientState.token_configs.connection_policy_id),
/** IBC channel token policyID, in hex format */
channel_policy_id: convertHex2String(cardanoClientState.token_configs.channel_policy_id),
},
}
}
5 changes: 5 additions & 0 deletions cardano/gateway/src/shared/helpers/connection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { KEY_CONNECTION_PREFIX } from '../../constant';
import { AuthToken } from '../types/auth-token';
import { hashSha3_256, hexToBytes } from './hex';

Expand All @@ -11,3 +12,7 @@ export function getConnectionIdByTokenName(tokenName: string, baseToken: AuthTok

return Buffer.from(hexToBytes(connIdHex)).toString();
}

export function connectionPath(connectionId: string): string {
return `${KEY_CONNECTION_PREFIX}/${connectionId}`;
}
9 changes: 9 additions & 0 deletions cardano/gateway/src/shared/helpers/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ export function fromHex(hex) {
export function decodeString(s) {
return decode(new TextEncoder().encode(s));
}

/** Convert a Hex encoded string to a Utf-8 encoded string. */
export function toText(hex) {
return new TextDecoder().decode(decode(new TextEncoder().encode(hex)));
}
/** Convert a Utf-8 encoded string to a Hex encoded string. */
export function fromText(text) {
return toHex(new TextEncoder().encode(text));
}
/**
* Decode decodes `src` into `decodedLen(src.length)` bytes
* If the input is malformed an error will be thrown
Expand Down
19 changes: 19 additions & 0 deletions cardano/gateway/src/shared/helpers/packet-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
KEY_PACKET_ACK_PREFIX,
KEY_PACKET_COMMITMENT_PREFIX,
KEY_PACKET_RECEIPT_PREFIX,
KEY_SEQUENCE_PREFIX,
} from '../../constant';
import { channelPathForPacket } from './channel';

export function packetReceiptPath(portId: string, channelId: string, sequence: bigint): string {
return `${KEY_PACKET_RECEIPT_PREFIX}/${channelPathForPacket(portId, channelId)}/${KEY_SEQUENCE_PREFIX}/${sequence.toString()}`;
}

export function packetCommitmentPath(portId: string, channelId: string, sequence: bigint): string {
return `${KEY_PACKET_COMMITMENT_PREFIX}/${channelPathForPacket(portId, channelId)}/${KEY_SEQUENCE_PREFIX}/${sequence.toString()}`;
}

export function packetAcknowledgementPath(portId: string, channelId: string, sequence: bigint): string {
return `${KEY_PACKET_ACK_PREFIX}/${channelPathForPacket(portId, channelId)}/${KEY_SEQUENCE_PREFIX}/${sequence.toString()}`;
}
6 changes: 6 additions & 0 deletions cardano/gateway/src/shared/helpers/verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MAX_EXPECTED_TIME_PER_BLOCK } from '../../constant';

export function getBlockDelay(timeDelay: bigint): number {
const expectedTimePerBlock = BigInt(MAX_EXPECTED_TIME_PER_BLOCK);
return Math.ceil(Number(timeDelay / expectedTimePerBlock));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PolicyId, UTxO } from "@dinhbx/lucid-custom";
import { AuthToken } from "~@/shared/types/auth-token";
import { PolicyId, UTxO } from '@dinhbx/lucid-custom';
import { AuthToken } from '~@/shared/types/auth-token';

export type UnsignedConnectionOpenAckDto = {
export type UnsignedChannelOpenAckDto = {
channelUtxo: UTxO;
connectionUtxo: UTxO;
clientUtxo: UTxO;
Expand All @@ -15,5 +15,9 @@ export type UnsignedConnectionOpenAckDto = {
constructedAddress: string;
chanOpenAckPolicyId: PolicyId;
chanOpenAckRefUtxo: UTxO;
channelToken: AuthToken
channelToken: AuthToken;

verifyProofPolicyId: PolicyId;
verifyProofRefUTxO: UTxO;
encodedVerifyProofRedeemer: string;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UTxO } from '@dinhbx/lucid-custom';

export type UnsignedConnectionOpenInitDto = {
export type UnsignedChannelOpenInitDto = {
handlerUtxo: UTxO;
connectionUtxo: UTxO;
clientUtxo: UTxO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UTxO } from '@dinhbx/lucid-custom';

export type UnsignedConnectionOpenTryDto = {
export type UnsignedChannelOpenTryDto = {
handlerUtxo: UTxO;
connectionUtxo: UTxO;
clientUtxo: UTxO;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PolicyId, UTxO } from '@dinhbx/lucid-custom';

export type UnsignedConnectionOpenAckDto = {
connectionUtxo: UTxO;
clientUtxo: UTxO;
constructedAddress: string;

spendConnectionRefUtxo: UTxO;
verifyProofRefUTxO: UTxO;

connectionTokenUnit: string;
encodedSpendConnectionRedeemer: string;
encodedUpdatedConnectionDatum: string;

verifyProofPolicyId: PolicyId;
encodedVerifyProofRedeemer: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export type UnsignedAckPacketMintDto = {
ackPacketRefUTxO: UTxO;
ackPacketPolicyId: PolicyId;
channelToken: AuthToken;

verifyProofPolicyId: PolicyId;
verifyProofRefUTxO: UTxO;
encodedVerifyProofRedeemer: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export type UnsignedAckPacketSucceedDto = {
ackPacketRefUTxO: UTxO;
ackPacketPolicyId: PolicyId;
channelToken: AuthToken;

verifyProofPolicyId: PolicyId;
verifyProofRefUTxO: UTxO;
encodedVerifyProofRedeemer: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ export type UnsignedAckPacketUnescrowDto = {
ackPacketRefUTxO: UTxO;
ackPacketPolicyId: PolicyId;
channelToken: AuthToken;

verifyProofPolicyId: PolicyId;
verifyProofRefUTxO: UTxO;
encodedVerifyProofRedeemer: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export type UnsignedRecvPacketMintDto = {
recvPacketRefUTxO: UTxO;
recvPacketPolicyId: PolicyId;
channelToken: AuthToken;

verifyProofPolicyId: PolicyId;
verifyProofRefUTxO: UTxO;
encodedVerifyProofRedeemer: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export type UnsignedRecvPacketUnescrowDto = {
recvPacketRefUTxO: UTxO;
recvPacketPolicyId: PolicyId;
channelToken: AuthToken;

verifyProofPolicyId: PolicyId;
verifyProofRefUTxO: UTxO;
encodedVerifyProofRedeemer: string;
};

0 comments on commit e0a0b4b

Please sign in to comment.