Skip to content

Commit

Permalink
refactor(crypto-transaction): use chainId to determine network (#842)
Browse files Browse the repository at this point in the history
* Remove utils

* Use chain ID to generate transactions

* Fix tests

* Use chainId

* Reset network

* Fix tests

* use custom crypto.json

* e2e tests
  • Loading branch information
sebastijankuzner authored Jan 28, 2025
1 parent 4e14b94 commit c876055
Show file tree
Hide file tree
Showing 25 changed files with 2,133 additions and 1,906 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Contracts, Identifiers as AppIdentifiers } from "@mainsail/contracts";
import { Application } from "@mainsail/kernel";

import { describe } from "../../../test-framework/source";
import { makeApplication } from "../application-factory";
import { Identifiers } from "../identifiers";
import { GenesisBlockGenerator } from "./genesis-block";
import { MnemonicGenerator } from "./mnemonic";
import { Application } from "@mainsail/kernel";

describe<{
app: Application;
Expand All @@ -27,19 +27,21 @@ describe<{
app.get<Contracts.Crypto.Configuration>(AppIdentifiers.Cryptography.Configuration).setConfig({
milestones: [
{
reward: "0",
address: { bech32m: "ark" },
block: { version: 1, maxGasLimit: 30_000_000, maxPayload: 2097152, maxTransactions: 150 },
block: { maxGasLimit: 30_000_000, maxPayload: 2_097_152, maxTransactions: 150, version: 1 },
blockTime: 8000,
height: 0,
evmSpec: Contracts.Evm.SpecId.SHANGHAI,
// @ts-ignore
gas: {
maximumGasLimit: 2000000,
maximumGasLimit: 2_000_000,
maximumGasPrice: 10_000,
minimumGasLimit: 21_000,
minimumGasPrice: 5,
maximumGasPrice: 10000,
minimumGasLimit: 21000,
},

height: 0,

reward: "0",
},
],
});
Expand All @@ -52,10 +54,10 @@ describe<{
const validatorsCount = 10;
assert.object(
await generator.generate(mnemonicGenerator.generate(), mnemonicGenerator.generateMany(validatorsCount), {
chainId: 123,
distribute: true,
epoch: new Date(),
premine: "2000000000",
pubKeyHash: 123,
}),
);
});
Expand Down
24 changes: 12 additions & 12 deletions packages/configuration-generator/source/generators/genesis-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class GenesisBlockGenerator extends Generator {
genesisWallet,
validators,
options.premine,
options.pubKeyHash,
options.chainId,
)),
);

Expand All @@ -72,14 +72,14 @@ export class GenesisBlockGenerator extends Generator {
genesisWallet,
genesisWallet,
options.premine,
options.pubKeyHash,
options.chainId,
),
);
}

const validatorTransactions = [
...(await this.#buildValidatorTransactions(validators, options.pubKeyHash)),
...(await this.#buildVoteTransactions(validators, options.pubKeyHash)),
...(await this.#buildValidatorTransactions(validators, options.chainId)),
...(await this.#buildVoteTransactions(validators, options.chainId)),
];

transactions = [...transactions, ...validatorTransactions];
Expand Down Expand Up @@ -114,13 +114,13 @@ export class GenesisBlockGenerator extends Generator {
sender: Wallet,
recipient: Wallet,
amount: string,
pubKeyHash: number,
chainId: number,
nonce = 0,
): Promise<Contracts.Crypto.Transaction> {
return await (
await this.app
.resolve(EvmCallBuilder)
.network(pubKeyHash)
.network(chainId)
.recipientAddress(recipient.address)
.nonce(nonce.toFixed(0))
.value(amount)
Expand All @@ -135,20 +135,20 @@ export class GenesisBlockGenerator extends Generator {
sender: Wallet,
recipients: Wallet[],
totalPremine: string,
pubKeyHash: number,
chainId: number,
): Promise<Contracts.Crypto.Transaction[]> {
const amount: string = BigNumber.make(totalPremine).dividedBy(recipients.length).toString();

const result: Contracts.Crypto.Transaction[] = [];

for (const [index, recipient] of recipients.entries()) {
result.push(await this.#createTransferTransaction(sender, recipient, amount, pubKeyHash, index));
result.push(await this.#createTransferTransaction(sender, recipient, amount, chainId, index));
}

return result;
}

async #buildValidatorTransactions(senders: Wallet[], pubKeyHash: number): Promise<Contracts.Crypto.Transaction[]> {
async #buildValidatorTransactions(senders: Wallet[], chainId: number): Promise<Contracts.Crypto.Transaction[]> {
const result: Contracts.Crypto.Transaction[] = [];

const iface = new ethers.Interface(ConsensusAbi.abi);
Expand All @@ -161,7 +161,7 @@ export class GenesisBlockGenerator extends Generator {
result[index] = await (
await this.app
.resolve(EvmCallBuilder)
.network(pubKeyHash)
.network(chainId)
.recipientAddress(this.#consensusProxyContractAddress)
.nonce("0") // validator registration tx is always the first one from sender
.payload(data)
Expand All @@ -174,7 +174,7 @@ export class GenesisBlockGenerator extends Generator {
return result;
}

async #buildVoteTransactions(senders: Wallet[], pubKeyHash: number): Promise<Contracts.Crypto.Transaction[]> {
async #buildVoteTransactions(senders: Wallet[], chainId: number): Promise<Contracts.Crypto.Transaction[]> {
const result: Contracts.Crypto.Transaction[] = [];

const iface = new ethers.Interface(ConsensusAbi.abi);
Expand All @@ -185,7 +185,7 @@ export class GenesisBlockGenerator extends Generator {
result[index] = await (
await this.app
.resolve(EvmCallBuilder)
.network(pubKeyHash)
.network(chainId)
.recipientAddress(this.#consensusProxyContractAddress)
.nonce("1") // vote transaction is always the 3rd tx from sender (1st one is validator registration)
.payload(data)
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/source/contracts/network-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type RewardOptions = {
export type GenesisBlockOptions = {
distribute: boolean;
premine: string;
pubKeyHash: number;
chainId: number;
epoch: Date;
snapshot?: SnapshotOptions;
};
Expand Down
Loading

0 comments on commit c876055

Please sign in to comment.