Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(crypto-transaction): use chainId to determine network #842

Merged
merged 9 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading