Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Petri: sign a proposed block hash only if it matches our own candidate
The Petri branch of manageProposeBlockHash signed any hash from any shard
member (accept-and-sign). Block compilation is now deterministic, so the
branch can use verify-then-sign instead:

- refuse when the claimed proposer differs from the authenticated sender
- refuse when the proposer is not the elected secretary
- fall through to the existing candidate comparison, so we only sign a
  hash equal to the block we compiled ourselves

getSecretaryIdentity is exported so the election logic is shared.
  • Loading branch information
hackobi committed Oct 11, 2026
commit d85522f0cfaa60b078c5a49af4aa5c696cff5c03
2 changes: 1 addition & 1 deletion src/libs/consensus/petri/coordination/petriSecretary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function resetCollection(): void {
* getShard() is seeded deterministically and we add ourselves to the
* sorted list so every node agrees on who is secretary.
*/
function getSecretaryIdentity(shard: Peer[]): string {
export function getSecretaryIdentity(shard: Peer[]): string {
const allIdentities = [
...shard.map(p => p.identity),
getSharedState.publicKeyHex,
Expand Down
53 changes: 32 additions & 21 deletions src/libs/consensus/v2/routines/manageProposeBlockHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { hexToUint8Array, ucrypto, uint8ArrayToHex } from "@kynesyslabs/demosdk/
import PeerManager from "@/libs/peer/PeerManager"
import getCommonValidatorSeed from "./getCommonValidatorSeed"
import getShard from "./getShard"
import { getSecretaryIdentity } from "@/libs/consensus/petri/coordination/petriSecretary"

export default async function manageProposeBlockHash(
blockHash: string,
validationData: ValidationData,
peerId: string,
sender?: string,
): Promise<RPCResponse> {
const response = _.cloneDeep(emptyResponse)
log.info("[Consensus Message Received] Propose Block Hash")
Expand Down Expand Up @@ -42,34 +44,43 @@ export default async function manageProposeBlockHash(
"[manageProposeBlockHash] Validator is in the shard: voting for the block hash",
)

// REVIEW: Petri Consensus — accept-and-sign model (Phase 9)
// In Petri, the elected secretary compiles the block and broadcasts its hash.
// Members trust the secretary's hash and sign it directly (accept-and-sign).
// This is safe because only one deterministically-elected secretary proposes per round.
// REVIEW: Petri Consensus — verify-then-sign (secretary model)
// In Petri, every shard member compiles the block deterministically
// (PetriBlockCompiler) and only the elected secretary broadcasts its hash.
// We refuse proposals that do not come from the secretary (or whose claimed
// proposer differs from the authenticated sender) and then fall through to
// the candidate comparison below, so we only ever sign a hash that matches
// the block we compiled ourselves.
if (getSharedState.petriConsensus) {
log.info(
"[manageProposeBlockHash] Petri active — accept-and-sign (secretary model)",
)
if (sender && sender !== peerId) {
log.error(
`[manageProposeBlockHash] Petri: claimed proposer ${peerId} ` +
`does not match authenticated sender ${sender}: refusing the block hash`,
)
response.result = 401
response.response = getSharedState.publicKeyHex
response.extra = "Proposer does not match authenticated sender"
return response
}

const blockSignature = await ucrypto.sign(
getSharedState.signingAlgorithm,
new TextEncoder().encode(blockHash),
)
const secretaryId = getSecretaryIdentity(shard)
if (peerId !== secretaryId) {
log.error(
`[manageProposeBlockHash] Petri: proposer ${peerId} is not the ` +
`elected secretary (${secretaryId}): refusing the block hash`,
)
response.result = 401
response.response = getSharedState.publicKeyHex
response.extra = "Proposer is not the elected secretary"
return response
}

log.info(
"[manageProposeBlockHash] Petri: signing secretary's block hash",
"[manageProposeBlockHash] Petri: proposer is the secretary — verifying against our candidate block",
)
response.result = 200
response.response = getSharedState.publicKeyHex
response.extra = {
signatures: {
[getSharedState.publicKeyHex]: uint8ArrayToHex(blockSignature.signature),
},
}
return response
}

// PoRBFT v2 path: compare with our own candidate block
// Compare with our own candidate block (PoRBFT v2 and Petri)
// ? Should we check for the block number as well? Or we cancel the candidateBlock at the end of the consensus?
// Vote for the block hash
// We must ensure we generated a block indeed
Expand Down
1 change: 1 addition & 0 deletions src/libs/network/manageConsensusRoutines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export default async function manageConsensusRoutines(
payload.params[0],
payload.params[1] as ValidationData,
payload.params[2] as string,
sender,
)
} catch (error) {
log.error(
Expand Down