Skip to content

Commit

Permalink
test: add mock module for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhbx-smartosc committed May 8, 2024
1 parent 833836e commit 03b847e
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 156 deletions.
95 changes: 1 addition & 94 deletions cardano/lib/ibc/apps/mock/datum.ak
Original file line number Diff line number Diff line change
@@ -1,98 +1,5 @@
use aiken/bytearray
use aiken/dict.{Dict}
use aiken/list
use ibc/core/ics_004/types/packet.{Packet}

pub type MockModuleDatum {
opened_channels: Dict<ByteArray, Bool>,
received_packets: List<ByteArray>,
}

pub fn validate_on_chan_open_init(
old_datum: MockModuleDatum,
updated_datum: MockModuleDatum,
channel_id: ByteArray,
) -> Bool {
expect !dict.has_key(old_datum.opened_channels, channel_id)

let expected_datum =
MockModuleDatum {
..old_datum,
opened_channels: dict.insert(
old_datum.opened_channels,
channel_id,
True,
bytearray.compare,
),
}

expect expected_datum == updated_datum

True
}

pub fn validate_on_chan_open_try(
old_datum: MockModuleDatum,
updated_datum: MockModuleDatum,
channel_id: ByteArray,
) -> Bool {
expect !dict.has_key(old_datum.opened_channels, channel_id)

let expected_datum =
MockModuleDatum {
..old_datum,
opened_channels: dict.insert(
old_datum.opened_channels,
channel_id,
True,
bytearray.compare,
),
}

expect expected_datum == updated_datum

True
}

pub fn validate_on_chan_open_ack(
old_datum: MockModuleDatum,
updated_datum: MockModuleDatum,
channel_id: ByteArray,
) -> Bool {
expect Some(True) = dict.get(old_datum.opened_channels, channel_id)

expect old_datum == updated_datum

True
}

pub fn validate_on_chan_open_confirm(
old_datum: MockModuleDatum,
updated_datum: MockModuleDatum,
channel_id: ByteArray,
) -> Bool {
expect Some(True) = dict.get(old_datum.opened_channels, channel_id)

expect old_datum == updated_datum

True
}

pub fn validate_on_recv_packet(
old_datum: MockModuleDatum,
updated_datum: MockModuleDatum,
channel_id: ByteArray,
packet: Packet,
) -> Bool {
expect Some(True) = dict.get(old_datum.opened_channels, channel_id)

let expected_datum =
MockModuleDatum {
..old_datum,
received_packets: list.push(old_datum.received_packets, packet.data),
}

expect expected_datum == updated_datum

True
received_packets: List<Packet>,
}
61 changes: 0 additions & 61 deletions cardano/lib/ibc/apps/mock/ibc_module.ak

This file was deleted.

152 changes: 152 additions & 0 deletions cardano/src/create_deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {
EMULATOR_ENV,
HANDLER_TOKEN_NAME,
MOCK_MODULE_PORT,
PORT_PREFIX,
TRANSFER_MODULE_PORT,
} from "./constants.ts";
Expand All @@ -39,6 +40,7 @@ import {
OutputReferenceSchema,
} from "../lucid-types/aiken/transaction/OutputReference.ts";
import { MintPortRedeemer } from "../lucid-types/ibc/core/ics_005/port_redeemer/MintPortRedeemer.ts";
import { MockModuleDatum } from "../lucid-types/ibc/apps/mock/datum/MockModuleDatum.ts";

// deno-lint-ignore no-explicit-any
(BigInt.prototype as any).toJSON = function () {
Expand Down Expand Up @@ -175,6 +177,19 @@ export const createDeployment = async (
);
referredValidators.push(mintVoucher.validator, spendTransferModule.validator);

const {
identifierTokenUnit: mockModuleIdentifier,
spendMockModule,
} = await deployMockModule(
lucid,
handlerToken,
spendHandlerValidator,
mintPortValidator,
mintIdentifierValidator,
MOCK_MODULE_PORT,
);
referredValidators.push(spendMockModule.validator);

const refUtxosInfo = await createReferenceUtxos(
lucid,
provider,
Expand Down Expand Up @@ -275,6 +290,13 @@ export const createDeployment = async (
address: spendTransferModule.address,
refUtxo: refUtxosInfo[spendTransferModule.scriptHash],
},
spendMockModule: {
title: "spending_mock_module.spend_mock_module",
script: spendMockModule.validator.script,
scriptHash: spendMockModule.scriptHash,
address: spendMockModule.address,
refUtxo: refUtxosInfo[spendMockModule.scriptHash],
},
mintVoucher: {
title: "minting_voucher.mint_voucher",
script: mintVoucher.validator.script,
Expand Down Expand Up @@ -303,6 +325,10 @@ export const createDeployment = async (
identifier: transferModuleIdentifier,
address: spendTransferModule.address,
},
mock: {
identifier: mockModuleIdentifier,
address: spendMockModule.address,
},
},
tokens: {
mock: mockTokenPolicyId + mockTokenName,
Expand Down Expand Up @@ -720,6 +746,132 @@ const deploySpendChannel = async (
};
};

const deployMockModule = async (
lucid: Lucid,
handlerToken: AuthToken,
spendHandlerValidator: SpendingValidator,
mintPortValidator: MintingPolicy,
mintIdentifierValidator: MintingPolicy,
mockModulePort: bigint,
) => {
console.log("Create Mock Module");

const [
spendMockModuleValidator,
spendMockModuleScriptHash,
spendMockModuleAddress,
] = readValidator(
"spending_mock_module.spend_mock_module",
lucid,
);

const mintPortPolicyId = lucid.utils.mintingPolicyToId(mintPortValidator);
const spendHandlerAddress = lucid.utils.validatorToAddress(
spendHandlerValidator,
);

const handlerTokenUnit = handlerToken.policy_id + handlerToken.name;
const handlerUtxo = await lucid.utxoByUnit(handlerTokenUnit);
const currentHandlerDatum = Data.from(handlerUtxo.datum!, HandlerDatum);
const updatedHandlerPorts = [
...currentHandlerDatum.state.bound_port,
mockModulePort,
]
.sort((a, b) => Number(a - b));
const updatedHandlerDatum: HandlerDatum = {
...currentHandlerDatum,
state: {
...currentHandlerDatum.state,
bound_port: updatedHandlerPorts,
},
};
const spendHandlerRedeemer: HandlerOperator = "HandlerBindPort";

const portTokenName = generateTokenName(
handlerToken,
PORT_PREFIX,
mockModulePort,
);
const portTokenUnit = mintPortPolicyId + portTokenName;
const mintPortRedeemer: MintPortRedeemer = {
handler_token: handlerToken,
spend_module_script_hash: spendMockModuleScriptHash,
port_number: mockModulePort,
};

// load nonce UTXO
const signerUtxos = await lucid.wallet.getUtxos();
if (signerUtxos.length < 1) throw new Error("No UTXO founded");
const NONCE_UTXO = signerUtxos[0];

const outputReference: OutputReference = {
transaction_id: {
hash: NONCE_UTXO.txHash,
},
output_index: BigInt(NONCE_UTXO.outputIndex),
};

const mintIdentifierPolicyId = lucid.utils.validatorToScriptHash(
mintIdentifierValidator,
);
const identifierTokenName = generateIdentifierTokenName(outputReference);
const identifierTokenUnit = mintIdentifierPolicyId + identifierTokenName;

const initModuleDatum: MockModuleDatum = {
received_packets: [],
};

const mintModuleTx = lucid
.newTx()
.collectFrom([NONCE_UTXO], Data.void())
.collectFrom([handlerUtxo], Data.to(spendHandlerRedeemer, HandlerOperator))
.attachSpendingValidator(spendHandlerValidator)
.attachMintingPolicy(mintPortValidator)
.mintAssets(
{
[portTokenUnit]: 1n,
},
Data.to(mintPortRedeemer, MintPortRedeemer),
)
.attachMintingPolicy(mintIdentifierValidator)
.mintAssets(
{
[identifierTokenUnit]: 1n,
},
Data.to(outputReference, OutputReference),
)
.payToContract(
spendHandlerAddress,
{
inline: Data.to(updatedHandlerDatum, HandlerDatum),
},
{
[handlerTokenUnit]: 1n,
},
)
.payToContract(
spendMockModuleAddress,
{
inline: Data.to(initModuleDatum, MockModuleDatum),
},
{
[identifierTokenUnit]: 1n,
[portTokenUnit]: 1n,
},
);

await submitTx(mintModuleTx, lucid, "Mint Mock Module");

return {
identifierTokenUnit,
spendMockModule: {
validator: spendMockModuleValidator,
scriptHash: spendMockModuleScriptHash,
address: spendHandlerAddress,
},
};
};

const main = async () => {
if (Deno.args.length < 1) throw new Error("Missing script params");

Expand Down
3 changes: 2 additions & 1 deletion cardano/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ type Validator =
| "mintPort"
| "mintIdentifier"
| "spendTransferModule"
| "spendMockModule"
| "mintVoucher"
| "verifyProof";

type Module = "handler" | "transfer";
type Module = "handler" | "transfer" | "mock";

type Tokens = "mock";

Expand Down
13 changes: 13 additions & 0 deletions cardano/validators/spending_mock_module.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use aiken/transaction.{ScriptContext}
use ibc/apps/mock/datum.{MockModuleDatum}
use ibc/core/ics_005/types/ibc_module_redeemer.{IBCModuleRedeemer}

validator {
fn spend_mock_module(
_datum: MockModuleDatum,
_redeemer: IBCModuleRedeemer,
_context: ScriptContext,
) -> Bool {
True
}
}

0 comments on commit 03b847e

Please sign in to comment.