Skip to content

Commit

Permalink
refactor(configuration-generator): generate genesis block witout sign…
Browse files Browse the repository at this point in the history
…ature (#295)

* Create empty commit proof

* Regenerate genesis

* Fix integration tests
  • Loading branch information
sebastijankuzner authored Nov 3, 2023
1 parent 4a81555 commit 81550b4
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 592 deletions.
4 changes: 2 additions & 2 deletions packages/api-http/test/fixtures/node_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"maxPayload": 2097152,
"maxTransactions": 150
},
"epoch": "2023-08-25T00:00:00.000Z",
"epoch": "2023-11-03T00:00:00.000Z",
"height": 0,
"reward": "0",
"address": {
Expand All @@ -26,7 +26,7 @@
"version": "0.0.1"
},
"explorer": "",
"nethash": "0728980a880b99f78bacfc17ba7a21938b448f32bfe137804ff4c3e619330bb7",
"nethash": "4382077d9a97531ee3c048fffd1831aa2662e1a1948bd1255ecf97884a82ee5f",
"ports": {},
"slip44": 1,
"symbol": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { inject, injectable, tagged } from "@mainsail/container";
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { TransferBuilder } from "@mainsail/crypto-transaction-transfer";
import { ValidatorRegistrationBuilder } from "@mainsail/crypto-transaction-validator-registration";
Expand All @@ -12,16 +12,9 @@ import { Generator } from "./generator";

@injectable()
export class GenesisBlockGenerator extends Generator {
@inject(Identifiers.Cryptography.Signature)
@tagged("type", "consensus")
private readonly signatureFactory!: Contracts.Crypto.ISignature;

@inject(Identifiers.Cryptography.Block.Serializer)
private readonly blockSerializer!: Contracts.Crypto.IBlockSerializer;

@inject(Identifiers.Cryptography.Message.Serializer)
private readonly messageSerializer!: Contracts.Crypto.IMessageSerializer;

async generate(
genesisMnemonic: string,
validatorsMnemonics: string[],
Expand Down Expand Up @@ -62,7 +55,7 @@ export class GenesisBlockGenerator extends Generator {
...(await this.#buildVoteTransactions(validators, options.pubKeyHash)),
);

const genesis = await this.#createCommittedGenesisBlock(validators, premineWallet.keys, transactions, options);
const genesis = await this.#createCommittedGenesisBlock(premineWallet.keys, transactions, options);

return {
block: genesis.block.data,
Expand Down Expand Up @@ -175,17 +168,15 @@ export class GenesisBlockGenerator extends Generator {
}

async #createCommittedGenesisBlock(
validators: Wallet[],
premineKeys: Contracts.Crypto.IKeyPair,
transactions: Contracts.Crypto.ITransaction[],
options: Contracts.NetworkGenerator.GenesisBlockOptions,
): Promise<Contracts.Crypto.ICommittedBlock> {
const genesisBlock = await this.#createGenesisBlock(premineKeys, transactions, options);

const proof = await this.#createCommitProof(validators, genesisBlock.block.data);
const commitBlock: Contracts.Crypto.ICommittedBlockSerializable = {
block: genesisBlock.block,
commit: proof,
commit: { round: 0, signature: "", validators: [] },
};

const serialized = await this.blockSerializer.serializeFull(commitBlock);
Expand Down Expand Up @@ -254,32 +245,4 @@ export class GenesisBlockGenerator extends Generator {
transactions: transactionData,
};
}

async #createCommitProof(
validators: Wallet[],
genesisBlock: Contracts.Crypto.IBlockData,
): Promise<Contracts.Crypto.IBlockCommit> {
const signatures: Buffer[] = [];

for (const wallet of validators) {
const bytes = await this.messageSerializer.serializePrecommitForSignature({
blockId: genesisBlock.id,
height: genesisBlock.height,
round: 0,
type: Contracts.Crypto.MessageType.Precommit,
});
const signature: string = await this.signatureFactory.sign(
bytes,
Buffer.from(wallet.consensusKeys.privateKey, "hex"),
);

signatures.push(Buffer.from(signature, "hex"));
}

return {
round: 0,
signature: await this.signatureFactory.aggregate(signatures),
validators: validators.map((v) => true),
};
}
}
Loading

0 comments on commit 81550b4

Please sign in to comment.