Skip to content
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
1 change: 1 addition & 0 deletions packages/crypto-messages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@mainsail/crypto-key-pair-ecdsa": "workspace:*",
"@mainsail/crypto-signature-ecdsa": "workspace:*",
"@mainsail/crypto-transaction": "workspace:*",
"@mainsail/crypto-transaction-evm-call": "workspace:*",
"@mainsail/crypto-validation": "workspace:*",
"@mainsail/crypto-wif": "workspace:*",
"@mainsail/serializer": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe<{
};

context.sandbox.app.bind(Identifiers.ValidatorSet.Service).toConstantValue(validatorSet);
context.sandbox.app.bind(Identifiers.State.Service).toConstantValue({});
context.sandbox.app.bind(Identifiers.CryptoWorker.WorkerPool).toConstantValue(workerPool);

context.factory = context.sandbox.app.resolve(MessageFactory);
Expand All @@ -71,22 +70,20 @@ describe<{
});

it("#makeProposal - should correctly make signed proposal", async ({ blockFactory, factory, identity }) => {
const data: Contracts.Crypto.ProposedData = {
block: await blockFactory.fromData(blockData),
serialized: serializedBlock,
};

const proposal = await factory.makeProposal(
{
data,
data: {
serialized: serializedBlock,
},
round: 1,
validatorIndex: 0,
},
identity.keys,
);

assert.equal(
proposal.signature,
"b25fd16693a2246d3e9dff3d7ae3da1473c1b24f6ef8b33c69e38e21e32c9ca307ae5f7d4573e2679ec48ecc2f1ff89d16115d8e7f6ffcba72cc9a746bcc40cdd0f9120d87c20addb55baceea2cb8cdb75faaf036e6a4221d28dc8f6558d8cc1",
"82841e0f25ac4bd6745ea2b80c314742e0e4b98073fa78c45e648c0f36037255ce5c2eb588fe4f4078ed9079202b427e0dad5008a81abb51573cdd50eebfda7ce7342700b8c319ef6dbf6adcbe96067584fe408b9d17fdb82e9787e71dc823e7",
);
});

Expand All @@ -112,7 +109,7 @@ describe<{

assert.equal(
proposal.signature,
"88276dd41eb9fc2b5cde4320b1c6dfc12bf5d71de1d82b79b8d6d00e78d2abfe6f242a823502edfe34ada370e38be22d093f974a720e01cbe21e867f1fd93d0fbb234f75a5d4a42a54516d616917fed46d2d5c2088665d3207b352360c167b34",
"86c1fde96bb08ea505276c8db238ab95a8f463c325cfec7789a5f7891d62306f998b2698177d1cd1a2b0800fb628b88519ef65e2d54a017425be273be0ac8275ce1fa256b6592de55a0eacec9032b7ca1b3df98c115ec743669eb8ed30c721bb",
);
});

Expand All @@ -126,7 +123,7 @@ describe<{
const precommit = await factory.makePrecommit(
{
blockHash: undefined,
height: 1,
blockNumber: 1,
round: 1,
type: Contracts.Crypto.MessageType.Precommit,
validatorIndex: 0,
Expand All @@ -150,7 +147,7 @@ describe<{
const prevote = await factory.makePrevote(
{
blockHash: undefined,
height: 1,
blockNumber: 1,
round: 1,
type: Contracts.Crypto.MessageType.Prevote,
validatorIndex: 0,
Expand Down
14 changes: 7 additions & 7 deletions packages/crypto-messages/source/keywords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe<{
$id: "test",
type: "object",
properties: {
block: {
data: {
type: "object",
properties: {
serialized: {
Expand All @@ -153,10 +153,10 @@ describe<{
};

for (let index = 0; index < activeValidators; index++) {
assert.undefined(context.validator.validate("test", { block: block1, validatorIndex: index }).error);
assert.undefined(context.validator.validate("test", { data: block1, validatorIndex: index }).error);
}

assert.defined(context.validator.validate("test", { block: block1, validatorIndex: activeValidators }).error);
assert.defined(context.validator.validate("test", { data: block1, validatorIndex: activeValidators }).error);

// change milestone to 15 validators at height 15
context.sandbox.app
Expand All @@ -173,16 +173,16 @@ describe<{
};

for (let index = 0; index < 15; index++) {
assert.undefined(context.validator.validate("test", { block: block2, validatorIndex: index }).error);
assert.undefined(context.validator.validate("test", { data: block2, validatorIndex: index }).error);
}

assert.defined(context.validator.validate("test", { block: block2, validatorIndex: 15 }).error);
assert.defined(context.validator.validate("test", { data: block2, validatorIndex: 15 }).error);

// block 1 still accepted
for (let index = 0; index < activeValidators; index++) {
assert.undefined(context.validator.validate("test", { block: block1, validatorIndex: index }).error);
assert.undefined(context.validator.validate("test", { data: block1, validatorIndex: index }).error);
}

assert.defined(context.validator.validate("test", { block: block1, validatorIndex: 53 }).error);
assert.defined(context.validator.validate("test", { data: block1, validatorIndex: 53 }).error);
});
});
8 changes: 4 additions & 4 deletions packages/crypto-messages/source/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const parseBlockNumber = (parentSchema): number | undefined => {
return parentSchema.parentData.blockNumber;
}

if (!parentSchema.parentData.block) {
if (!parentSchema.parentData.data) {
return undefined;
}

// Proposals contain the block only in serialized form (hex).
// We can extract the block numuber at a fixed offset here, without needing to deserialize the whole block.
// We can extract the block number at a fixed offset here, without needing to deserialize the whole block.

// See packages/crypto-block/source/serializer.ts#serializeProposed for reference.
// See packages/crypto-messages/source/serializer.ts#serializeProposed for reference.

const serialized = parentSchema.parentData.block.serialized;
const serialized = parentSchema.parentData.data.serialized;
if (!serialized) {
return undefined;
}
Expand Down
28 changes: 20 additions & 8 deletions packages/crypto-messages/source/proposal.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Contracts, Identifiers } from "@mainsail/contracts";

import { describe, Sandbox } from "../../test-framework/source";
import { blockData, proposalData, proposalDataWithValidRound, serializedBlock } from "../test/fixtures/proposal";
import {
blockData,
proposalData,
proposalDataWithValidRound,
serializedBlock,
serializedProposal,
} from "../test/fixtures/proposal";
import { prepareSandbox } from "../test/helpers/prepare-sandbox";
import { Proposal } from "./proposal";
import { assertProposedData } from "../test/helpers/asserts";

describe<{
sandbox: Sandbox;
Expand All @@ -12,8 +19,8 @@ describe<{
const data: Contracts.Crypto.ProposedData = {
block: {
data: blockData,
header: { ...blockData, transactions: [] },
serialized: serializedBlock,
header: { ...blockData },
serialized: serializedBlock.slice(2),
transactions: [],
},
serialized: serializedBlock,
Expand All @@ -35,6 +42,12 @@ describe<{
context.sandbox.app.bind(Identifiers.State.Store).toConstantValue({});
context.sandbox.app.bind(Identifiers.CryptoWorker.WorkerPool).toConstantValue(workerPool);

const blockInstance = await context.sandbox.app
.get<Contracts.Crypto.BlockFactory>(Identifiers.Cryptography.Block.Factory)
.fromData(blockData);

(data.block as any).transactions = blockInstance.transactions;

context.proposal = context.sandbox.app.resolve(Proposal).initialize({
...proposalData,
dataSerialized: data.serialized,
Expand Down Expand Up @@ -76,22 +89,21 @@ describe<{
});

// User assert block data
it.skip("#getData - should be ok", async ({ proposal }) => {
it("#getData - should be ok", async ({ proposal }) => {
await proposal.deserializeData();
assert.equal(proposal.getData(), data);
assertProposedData(assert, proposal.getData(), data);
});

it("#toString - should be ok", ({ proposal }) => {
assert.equal(proposal.toString(), `{"blockNumber":2,"round":1,"validatorIndex":0}`);
});

// TODO: update fixture
it.skip("#toString - should include block id after deserialization", async ({ proposal }) => {
it("#toString - should include block id after deserialization", async ({ proposal }) => {
await proposal.deserializeData();

assert.equal(
proposal.toString(),
`{"block":"3b76ae07ded37bbab2d56302f7ab09f302ec1a815a53c80ee9805d9c8c8eca19","blockNumber":2,"round":1,"validatorIndex":0}`,
`{"block":"10ee5958aa6a97c60830a75f4c0f49ca02da76e5f13851567534a6837e0fa69d","blockNumber":2,"round":1,"validatorIndex":0}`,
);
});

Expand Down
Loading
Loading