diff --git a/pkgs/id/src/attestation.rs b/pkgs/id/src/attestation.rs index 55b2f8d7dd..2b48b24592 100644 --- a/pkgs/id/src/attestation.rs +++ b/pkgs/id/src/attestation.rs @@ -23,6 +23,11 @@ //! and final-state hash, `Disputed` when both claims exist and differ //! (the wrong-winner negative case can never compute `Agreed`). //! +//! A malformed agreement is no agreement: the two seats must be two +//! distinct players (#2863, design #2898). [`compute_state`] returns +//! `Proposed` for a duplicate-seat agreement regardless of the genesis +//! and claim set, and [`sign_genesis`] refuses to sign one. +//! //! Reconstruction is admission, not deserialization: derive conclusions from //! signature-verified authors and compare claims with the selected agreement. //! Reject, ignore, or dispute inconsistent input according to the API's rules; @@ -153,9 +158,15 @@ pub enum MatchState { /// Signs a genesis agreement as an `attestation.genesis` event. /// +/// Refuses a duplicate-seat agreement (both seats the same player): a +/// mutual concession needs two distinct players, so no honest signer +/// should produce one. The viewer ([`compute_state`]) enforces the same +/// invariant on already-signed objects from peers. +/// /// # Errors /// -/// Returns a chain error if the extension or backlink is invalid, or a +/// Returns [`SignError::DuplicateSeat`] if both seats name the same +/// player, a chain error if the extension or backlink is invalid, or a /// JSON error if the agreement cannot be serialized. pub fn sign_genesis( key: &SigningKey, @@ -164,6 +175,10 @@ pub fn sign_genesis( agreement: &GenesisAgreement, after: Vec, ) -> Result { + let [first, second] = agreement.players; + if first == second { + return Err(SignError::DuplicateSeat); + } let body = agreement.body()?; Ok(chain::sign( key, @@ -225,21 +240,28 @@ pub fn sign_claim( /// verified every signature before this function sees an object. /// /// Rules: without both players signing the *identical* agreement the -/// match is `Proposed`. With a complete genesis, one claim is still -/// `Proposed`; two claims (one per player) that concur on outcome and -/// final-state hash are `Agreed`; two claims that differ in either are -/// `Disputed`. A claim's position is defined by its `agreement`, `outcome`, -/// and `final_state_hash`; `frontiers` are observation evidence and do not -/// distinguish positions. A player claiming twice at different positions -/// is a fork and computes `Disputed`, never `Agreed`. Claims answering a -/// different agreement, from non-players, or with an outcome other than -/// `"draw"` or a seated player's lowercase 64-character hex id are ignored. +/// match is `Proposed`. An agreement whose two seats name the same player +/// is malformed and is also `Proposed`, whatever the genesis and claim +/// set: a self-match is not a mutual concession (#2863). With a complete +/// genesis, one claim is still `Proposed`; two claims (one per player) +/// that concur on outcome and final-state hash are `Agreed`; two claims +/// that differ in either are `Disputed`. A claim's position is defined by +/// its `agreement`, `outcome`, and `final_state_hash`; `frontiers` are +/// observation evidence and do not distinguish positions. A player +/// claiming twice at different positions is a fork and computes +/// `Disputed`, never `Agreed`. Claims answering a different agreement, +/// from non-players, or with an outcome other than `"draw"` or a seated +/// player's lowercase 64-character hex id are ignored. #[must_use] pub fn compute_state( agreement: &GenesisAgreement, genesis: &[(PlayerId, GenesisAgreement)], claims: &[(PlayerId, ResultClaim)], ) -> MatchState { + let [first, second] = agreement.players; + if first == second { + return MatchState::Proposed; + } let genesis_ok = agreement.players.iter().all(|player| { genesis .iter() @@ -291,14 +313,16 @@ pub fn compute_state( } } -/// A signing failure: the object would not serialize, or the chain -/// refused the event. +/// A signing failure: the object would not serialize, the chain +/// refused the event, or the object names the same player in both seats. #[derive(Debug)] pub enum SignError { /// The object would not serialize to its canonical body. Json(serde_json::Error), /// The chain refused the event (extension or backlink rules). Chain(chain::ChainError), + /// Both seats of the agreement name the same player (#2863). + DuplicateSeat, } impl From for SignError { @@ -318,6 +342,9 @@ impl std::fmt::Display for SignError { match self { Self::Json(err) => write!(f, "attestation object would not serialize: {err}"), Self::Chain(err) => write!(f, "chain refused the attestation event: {err:?}"), + Self::DuplicateSeat => { + write!(f, "both seats of the agreement name the same player") + } } } } diff --git a/pkgs/id/tests/attestation_poc.rs b/pkgs/id/tests/attestation_poc.rs index 1cdf256ab2..fe8141fea8 100644 --- a/pkgs/id/tests/attestation_poc.rs +++ b/pkgs/id/tests/attestation_poc.rs @@ -28,8 +28,8 @@ use p2panda_core::{Hash, SigningKey}; use tempfile::TempDir; use id::attestation::{ - GenesisAgreement, MatchState, ResultClaim, author_id, compute_state, player_id, sign_claim, - sign_genesis, + GenesisAgreement, MatchState, ResultClaim, SignError, author_id, compute_state, player_id, + sign_claim, sign_genesis, }; use id::chain; use id::event_store::{EventStore, Ingest}; @@ -328,6 +328,94 @@ fn same_position_claims_with_different_frontiers_still_agree() { } } +/// A malformed agreement naming the same player in both seats (#2863). +/// +/// Only a viewer can be handed this shape now — [`sign_genesis`] refuses +/// it — so the viewer tests construct the pairs directly, the way a +/// decoded peer-authored event arrives. +fn duplicate_seat_agreement() -> (SigningKey, GenesisAgreement) { + let a = key(0); + let agreement = GenesisAgreement { + game_id: "tic-tac-toe".to_owned(), + logic_hash: Hash::digest(include_bytes!("../apps/tic-tac-toe/program/main.roc")), + players: [player_id(&a), player_id(&a)], + witness: None, + objection_window_secs: 3600, + }; + (a, agreement) +} + +fn claim_for(agreement: &GenesisAgreement, outcome: String, final_state_hash: Hash) -> ResultClaim { + ResultClaim { + agreement: agreement.agreement_hash().unwrap(), + outcome, + final_state_hash, + frontiers: vec![], + } +} + +#[test] +fn duplicate_seat_agreement_with_one_genesis_and_one_claim_stays_proposed() { + let (a, agreement) = duplicate_seat_agreement(); + let seat = player_id(&a); + let genesis = vec![(seat, agreement.clone())]; + let claims = vec![( + seat, + claim_for( + &agreement, + hex(&seat), + Hash::digest(FINAL_BOARD_JSON.as_bytes()), + ), + )]; + + assert_eq!( + compute_state(&agreement, &genesis, &claims), + MatchState::Proposed + ); +} + +#[test] +fn duplicate_seat_agreement_with_two_distinct_claims_stays_proposed() { + let (a, agreement) = duplicate_seat_agreement(); + let seat = player_id(&a); + let genesis = vec![(seat, agreement.clone())]; + let first = claim_for( + &agreement, + hex(&seat), + Hash::digest(FINAL_BOARD_JSON.as_bytes()), + ); + let mut second = first.clone(); + second.outcome = "draw".to_owned(); + + // Two differing positions from the same key would be `Disputed` for + // distinct seats; a self-match is simply not a match. + let claims = vec![(seat, first), (seat, second)]; + assert_eq!( + compute_state(&agreement, &genesis, &claims), + MatchState::Proposed + ); +} + +#[test] +fn duplicate_seat_agreement_stays_proposed_without_genesis_or_claims() { + let (_a, agreement) = duplicate_seat_agreement(); + + assert_eq!(compute_state(&agreement, &[], &[]), MatchState::Proposed); +} + +#[test] +fn sign_genesis_refuses_duplicate_seat_agreement() { + let (a, agreement) = duplicate_seat_agreement(); + + let err = sign_genesis(&a, 0, None, &agreement, vec![]) + .err() + .expect("a duplicate-seat agreement must not be signed"); + assert!( + matches!(err, SignError::DuplicateSeat), + "expected DuplicateSeat, got {err:?}" + ); +} + #[test] fn same_player_claims_with_different_position_fields_are_disputed() { let x = key(0); @@ -355,3 +443,4 @@ fn same_player_claims_with_different_position_fields_are_disputed() { ); } } +