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
6 changes: 3 additions & 3 deletions contracts/src/consensus/ConsensusV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ contract ConsensusV1 is UUPSUpgradeable, OwnableUpgradeable {
}

function unvote() external {
emit Unvoted(msg.sender, _unvote());
_unvote();
}

function updateVoters(address[] calldata voters) external onlyOwner {
Expand Down Expand Up @@ -633,7 +633,7 @@ contract ConsensusV1 is UUPSUpgradeable, OwnableUpgradeable {
return _activeValidators[index] == addr;
}

function _unvote() internal returns (address) {
function _unvote() internal {
Vote storage voter = _voters[msg.sender];
if (voter.validator == address(0)) {
revert MissingVote();
Expand Down Expand Up @@ -664,7 +664,7 @@ contract ConsensusV1 is UUPSUpgradeable, OwnableUpgradeable {

_votersCount--;

return validatorAddr;
emit Unvoted(msg.sender, validatorAddr);
}

function _updateVoter(address addr) internal {
Expand Down
2 changes: 2 additions & 0 deletions contracts/test/consensus/Consensus-Vote.sol
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ contract ConsensusTest is Base {
// Swap Vote
vm.startPrank(voterAddr);
vm.expectEmit(address(consensus));
emit ConsensusV1.Unvoted(voterAddr, validatorAddr1);
vm.expectEmit(address(consensus));
emit ConsensusV1.Voted(voterAddr, validatorAddr2);
consensus.vote(validatorAddr2);
vm.stopPrank();
Expand Down
14 changes: 14 additions & 0 deletions packages/consensus/source/commit-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,18 @@ describe<{

assert.is(commitState.getAccountUpdates(), accountUpdates);
});

it("#getContractEvents - should be empty by default", ({ commitState }) => {
assert.equal(commitState.getContractEvents(), []);
});

it("#setContractEvents - should store the contract events", ({ commitState }) => {
const contractEvents = [
{ event: "Voted", txHash: "hash-0", txIndex: 0, validator: "address-1", voter: "address-0" },
] as unknown as Contracts.Evm.ContractEvent[];

commitState.setContractEvents(contractEvents);

assert.is(commitState.getContractEvents(), contractEvents);
});
});
9 changes: 9 additions & 0 deletions packages/consensus/source/commit-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class CommitState implements Contracts.Processor.ProcessableUnit {
#processorResult?: Contracts.Processor.BlockProcessorResult;
#validators = new Map<string, Contracts.State.ValidatorWallet>();
#accountUpdates: Array<Contracts.Evm.AccountUpdate> = [];
#contractEvents: Array<Contracts.Evm.ContractEvent> = [];

public get blockNumber(): number {
return this.#commit.block.number;
Expand Down Expand Up @@ -65,6 +66,14 @@ export class CommitState implements Contracts.Processor.ProcessableUnit {
this.#accountUpdates = accounts;
}

public getContractEvents(): Array<Contracts.Evm.ContractEvent> {
return this.#contractEvents;
}

public setContractEvents(events: Array<Contracts.Evm.ContractEvent>): void {
this.#contractEvents = events;
}

public async getCommit(): Promise<Contracts.Crypto.Commit> {
return this.#commit;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/consensus/source/round-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,20 @@ describe<{
assert.is(roundState.getAccountUpdates(), accountUpdates);
});

it("#getContractEvents - should be empty by default", ({ roundState }) => {
assert.equal(roundState.getContractEvents(), []);
});

it("#setContractEvents - should store the contract events", ({ roundState }) => {
const contractEvents = [
{ event: "Voted", txHash: "hash-0", txIndex: 0, validator: "address-1", voter: "address-0" },
] as unknown as Contracts.Evm.ContractEvent[];

roundState.setContractEvents(contractEvents);

assert.is(roundState.getContractEvents(), contractEvents);
});

it("#logPrevotes - should log the voters grouped by block hash", ({ roundState, logger, validatorSet }) => {
const debug = spy(logger, "debug");
const getValidator = spy(validatorSet, "getValidator");
Expand Down
9 changes: 9 additions & 0 deletions packages/consensus/source/round-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class RoundState implements Contracts.Consensus.RoundState {
#proposal?: Contracts.Crypto.Proposal;
#processorResult?: Contracts.Processor.BlockProcessorResult;
#accountUpdates: Array<Contracts.Evm.AccountUpdate> = [];
#contractEvents: Array<Contracts.Evm.ContractEvent> = [];
#prevotes = new Map<number, Contracts.Crypto.Message>();
#prevotesCount = new Map<string | undefined, number>();
#precommits = new Map<number, Contracts.Crypto.Message>();
Expand Down Expand Up @@ -159,6 +160,14 @@ export class RoundState implements Contracts.Consensus.RoundState {
this.#accountUpdates = accounts;
}

public getContractEvents(): Array<Contracts.Evm.ContractEvent> {
return this.#contractEvents;
}

public setContractEvents(events: Array<Contracts.Evm.ContractEvent>): void {
this.#contractEvents = events;
}

public hasPrevote(validatorIndex: number): boolean {
return this.#prevotes.has(validatorIndex);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/contracts/source/contracts/evm/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ export interface AccountUpdate {
readonly legacyMergeInfo?: AccountMergeInfo;
}

export type ContractEvent = {
readonly txHash: string;
readonly txIndex: number;
} & (
| { readonly event: "Voted"; readonly voter: string; readonly validator: string }
| { readonly event: "Unvoted"; readonly voter: string; readonly validator: string }
| { readonly event: "ValidatorRegistered"; readonly addr: string; readonly blsPublicKey: string }
| { readonly event: "ValidatorResigned"; readonly addr: string }
| { readonly event: "ValidatorUpdated"; readonly addr: string; readonly blsPublicKey: string }
| {
readonly event: "UsernameRegistered";
readonly addr: string;
readonly username: string;
readonly previousUsername?: string;
}
| { readonly event: "UsernameResigned"; readonly addr: string; readonly username: string }
);

export interface AccountUpdateContext {
readonly account: string;
readonly commitKey: CommitKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Block } from "../crypto/block.js";
import type { Commit } from "../crypto/commit.js";
import type { AccountUpdate } from "../evm/evm.js";
import type { AccountUpdate, ContractEvent } from "../evm/evm.js";
import type { BlockProcessorResult } from "./block-processor-result.js";

export interface ProcessableUnit {
Expand All @@ -11,6 +11,8 @@ export interface ProcessableUnit {
setProcessorResult(processorResult: BlockProcessorResult): void;
setAccountUpdates(accounts: Array<AccountUpdate>): void;
getAccountUpdates(): Array<AccountUpdate>;
setContractEvents(events: Array<ContractEvent>): void;
getContractEvents(): Array<ContractEvent>;
getBlock(): Block;
getCommit(): Promise<Commit>;
}
1 change: 1 addition & 0 deletions packages/evm-consensus/source/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class Deployer implements Contracts.EvmConsensus.Deployer {
commitKey,
getBlock: () => ({ ...commitKey, number: commitKey.blockNumber }),
setAccountUpdates: () => ({}),
setContractEvents: () => ({}),
} as unknown as Contracts.Processor.ProcessableUnit);
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/evm-contracts/source/abis/ConsensusV1.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/evm-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@mainsail/crypto-transaction": "workspace:*",
"@mainsail/crypto-validation": "workspace:*",
"@mainsail/crypto-wif": "workspace:*",
"@mainsail/evm-contracts": "workspace:*",
"@mainsail/serializer": "workspace:*",
"@mainsail/test-runner": "workspace:*",
"@mainsail/validation": "workspace:*",
Expand Down
2 changes: 2 additions & 0 deletions packages/evm-service/source/instances/evm-boundary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ describe<{
({
blockNumber: genesisCommit.block.number,
getAccountUpdates: () => [],
getContractEvents: () => [],
getBlock: () => genesisCommit.block,
getCommit: async () => genesisCommit,
round: genesisCommit.block.round,
setAccountUpdates: () => {},
setContractEvents: () => {},
}) as unknown as Contracts.Processor.ProcessableUnit;

// Valid legacy (base58check) addresses: one imported as a cold wallet by the tests
Expand Down
Loading
Loading