Skip to content

score/game: Allow participant that called register to use their account VRF until their next attendance. - #270

Open
gui1117 wants to merge 5 commits into
mainfrom
gui-newly-promoted-registration-fix
Open

gui1117 wants to merge 5 commits into
mainfrom
gui-newly-promoted-registration-fix

Conversation

@gui1117

@gui1117 gui1117 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #249

I chose to allow a player to still use its account VRF until the next game, even after having called register. It doesn't change anything in regards to security, a player could have signed up for the game and then call register, now he can also do it in the other order.

I'm also thinking we could just always allow to use the account VRF, but I don't want to change the design.

This would require a migration, but I think nothing is live yet, otherwise I can implement using a new storage to avoid the migration, or do a migration.

@gui1117
gui1117 requested a review from a team November 23, 2026 23:38
@andrzejSulkowski
andrzejSulkowski self-requested a review November 24, 2026 04:57
Comment thread pallets/game/src/lib.rs
Comment on lines +2323 to +2326
ensure!(
!recognized || newly_recognized,
AirdropShapeError::RecognitionMismatch
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this lets one person enter a draw twice, once with the account VRF and once as their person with the alias VRF. Here is the runtime test to confirm it:

use super::*;
use indiv_pallet_game::GameSchedule;
use indiv_pallet_score::Recognition;

const MAX_WINNERS: u32 = 4;
const MAX_RING_BUILD_BLOCKS: u32 = 1_000;

fn schedule_one_game(airdrop: bool) -> GameSchedule<<Runtime as pallet_assets::Config>::AssetId, Balance> {
	let now = (pallet_timestamp::Now::<Runtime>::get() / 1000) as u32;
	let schedule = GameSchedule {
		game_play_time: now + IN_BETWEEN_GAMES,
		rounds: 1,
		max_group_size: 3,
		airdrops: if airdrop { game_airdrops(&[0], MAX_WINNERS) } else { Default::default() },
		tenure_claim_window: 0,
	};
	Game::schedule_games(RuntimeOrigin::root(), vec![schedule.clone()]).unwrap();
	schedule
}

/// Bob signs up, reports and wins the game alone.
fn bob_plays(schedule: &GameSchedule<<Runtime as pallet_assets::Config>::AssetId, Balance>) {
	let bob_pair = Sr25519Keyring::Bob.pair();
	advance_until_time(GameTimes::<Runtime>::registration_start(schedule));
	let signup = indiv_pallet_game::Call::<Runtime>::sign_up_with_account {
		identifier_key: [1u8; 65],
		airdrops: None,
	};
	exec_signed(&bob_pair, signup.into());
	advance_until_time(GameTimes::<Runtime>::game_play_time(schedule));
	let report =
		indiv_pallet_game::Call::<Runtime>::report { full_report: vec![BoundedVec::new()].try_into().unwrap() };
	exec_signed_as_score_participant(&bob_pair, report.into());
	advance_until_time(GameTimes::<Runtime>::player_process_end(schedule));
}

fn bob_recognition() -> Recognition {
	indiv_pallet_score::Participants::<Runtime>::get(AccountOrPerson::Account(
		Sr25519Keyring::Bob.to_account_id(),
	))
	.unwrap()
	.recognition
}

/// Bob reaches personhood, calls `Score::register` with `secret`'s member key and waits until
/// the key is in a built ring.
fn bob_registers(secret: &VrfSecret) {
	let bob_pair = Sr25519Keyring::Bob.pair();
	let bob = Sr25519Keyring::Bob.to_account_id();
	Balances::mint_into(&bob, 100 * PlayDepositDefault::get()).unwrap();

	reduce_game_phase_durations();
	bob_plays(&schedule_one_game(false));

	let member = Crypto::member_from_secret(secret);
	let msg = (b"pop register using", &bob).encode();
	let sig = Crypto::sign(secret, &msg[..]).unwrap();
	let register = indiv_pallet_score::Call::<Runtime>::register { key: Some((member.clone(), sig)) };
	exec_signed_as_score_participant(&bob_pair, register.into());

	for _ in 0..MAX_RING_BUILD_BLOCKS {
		let ring_index = indiv_pallet_members::Pallet::<Runtime>::member_status(
			indiv_pallet_people::PEOPLE_MEMBER_IDENTIFIER,
			&member,
		)
		.and_then(|status| status.ring_index());
		if let Some(ring_index) = ring_index {
			if indiv_pallet_members::Root::<Runtime>::get(
				indiv_pallet_people::PEOPLE_MEMBER_IDENTIFIER,
				ring_index,
			)
			.is_some()
			{
				return;
			}
		}
		advance_block();
	}
	panic!("the member key never joined a built ring");
}

/// The person behind `secret` binds an alias account and returns it with a
/// `sign_up_with_alias` call that enters `event_id` with the alias VRF.
fn person_sign_up(secret: &VrfSecret, event_id: [u8; 32]) -> (sr25519::Pair, RuntimeCall) {
	let alias_acc = sr25519::Pair::from_seed(&[50u8; 32]);
	let stmt_acc = sr25519::Pair::from_seed(&[51u8; 32]);
	let set_alias = indiv_pallet_people::Call::<Runtime>::set_alias_account {
		account: pair_to_account_id(&alias_acc),
		call_valid_at: frame_system::Pallet::<Runtime>::block_number(),
	};
	exec_as_alias_with_proof(secret, score_context(), set_alias.into());

	let alias = Crypto::alias_in_context(secret, &score_context()[..]).unwrap();
	let stmt_msg =
		(b"pop:game:stmt_account_for_alias:", alias).using_encoded(sp_io::hashing::blake2_256);
	let sign_up = indiv_pallet_game::Call::<Runtime>::sign_up_with_alias {
		identifier_key: [2u8; 65],
		statement_account: pair_to_account_id(&stmt_acc),
		sig: stmt_acc
			.sign(&indiv_pallet_game::Pallet::<Runtime>::account_signing_payload(&stmt_msg))
			.into(),
		airdrops: Some(build_alias_airdrop_vrfs(
			secret,
			&[event_id],
			RegistrationEntry::Alias { alias },
		)),
	};
	(alias_acc, sign_up.into())
}

#[test]
fn newly_recognized_account_and_its_person_both_enter_one_draw() {
	new_test_ext().execute_with(|| {
		let bob_pair = Sr25519Keyring::Bob.pair();
		let bob = Sr25519Keyring::Bob.to_account_id();
		let secret = Crypto::new_secret([7u8; 32]);
		setup_airdrop_prize_asset(MAX_WINNERS);
		bob_registers(&secret);

		// Game #2 carries the airdrop. Bob has not attended a game since `register`.
		let schedule = schedule_one_game(true);
		let event_id = airdrop_event_id_for(2, 0);
		advance_until_time(GameTimes::<Runtime>::registration_start(&schedule));
		drive_airdrop_to_registering(event_id);
		let (alias_acc, person_sign_up) = person_sign_up(&secret, event_id);
		assert!(matches!(bob_recognition(), Recognition::Recognized { newly_recognized: true, .. }));

		// Bob enters with the account VRF.
		let signup = indiv_pallet_game::Call::<Runtime>::sign_up_with_account {
			identifier_key: [1u8; 65],
			airdrops: Some(build_account_airdrop_vrfs(&bob_pair, &[event_id])),
		};
		exec_signed_as_score_participant(&bob_pair, signup.into());

		// The same human enters again as a person with the alias VRF.
		let outcome = Executive::apply_extrinsic(build_signed_as_alias_with_account_ext(
			&alias_acc,
			person_sign_up,
		))
		.expect("transaction is valid");
		assert_eq!(outcome, Ok(()));

		let entries = indiv_pallet_airdrop::Registrations::<Runtime>::iter_prefix(event_id)
			.map(|(_, entry)| entry)
			.collect::<Vec<_>>();
		let alias = Crypto::alias_in_context(&secret, &score_context()[..]).unwrap();
		assert_eq!(entries.len(), 2);
		assert!(entries.contains(&RegistrationEntry::Account { account_id: bob }));
		assert!(entries.contains(&RegistrationEntry::Alias { alias }));
	});
}

@andrzejSulkowski andrzejSulkowski Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was checking this and this is a pre-existing issue in another order. Somebody reaches personhood, registers with the Account, calls register, and now enters the draw again with the Person path.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what we might do is fn awaits_ring_inclusion(who: PersonalId) -> bool; on the PeopleTrait. As soon as the MemberKey gets queued, we forbid using the AirdropVrfs::Alias path.

This might allow us to keep Participant unchanged (no migration and sdk break).

The open door stays: sign up with the account variant, the key joins a ring, then take a Person(alias) seat, all within one registration phase. But that's already possible on main.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the idea so much because the old transaction fails as soon as the person is included which is not predictable. So I submit my transaction, it fails because I just got included and because it failed it consumed my free allowance.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. In this case I'd probably just accept the double entering of the draw.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, about the double entering I opened an issue, I don't think it is critical at the moment: #273

/// A participant may have reached personhood score, it is not recognized until he submit his
/// key and calls `register`.
Recognized(PersonalId),
Recognized {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paseo Next and PreviewNet already have Recognized entries, so those stop decoding without a migration. Here is the test to confirm it:

/// Writes `p` for `who` in the old `Recognized(id)` layout and reads it back.
fn read_back_old_recognized_entry(
	who: &AccountOrPerson<u64>,
	mut p: Participant<BalanceOf<Test>>,
	last_attended_game: Option<u32>,
) -> Option<Participant<BalanceOf<Test>>> {
	p.recognition = Recognized { id: 101, newly_recognized: false };
	p.last_attended_game = last_attended_game;
	let new = p.encode();
	let tail = (p.last_attended_game, p.attended_games_while_recognized).encode();
	// Drop the `newly_recognized` byte, which sits just before the tail.
	let old = [&new[..new.len() - tail.len() - 1], &tail[..]].concat();
	frame_support::storage::unhashed::put_raw(&Participants::<Test>::hashed_key_for(who), &old);
	Participants::<Test>::get(who)
}

#[test]
fn old_recognized_entry_does_not_decode() {
	new_test_ext().execute_with(|| {
		let (who, _) = setup_recognised_person(99);
		let p = || Participants::<Test>::get(&who).unwrap();
		// Last game 256 decodes, but into the wrong values.
		let read = read_back_old_recognized_entry(&who, p(), Some(256)).unwrap();
		assert_eq!(read.recognition, Recognized { id: 101, newly_recognized: true });
		assert_eq!(read.last_attended_game, None);
		// Same tail as a live Paseo Next entry: id 101, last game 321.
		assert!(read_back_old_recognized_entry(&who, p(), Some(321)).is_none());
	});
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for a migration

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the migration 1d11586

Comment thread pallets/score/src/types.rs Resolved
@andrzejSulkowski andrzejSulkowski added the C0.5-breaking-client-api This PR breaks an API used by off-chain clients (e.g. product-sdk); clients must be updated. label Nov 24, 2026
@andrzejSulkowski

Copy link
Copy Markdown
Collaborator

This would require a migration, but I think nothing is live yet, otherwise I can implement using a new storage to avoid the migration, or do a migration.

There is nothing in prod. but for our testnets it probably is valuable to not require a wipe. Of course if its not too complicated.

@andrzejSulkowski andrzejSulkowski added the D5-nice to have audit optional to have, meant to express "audit it if there is bandwidth" label Nov 24, 2026
Comment thread pallets/score/src/tests.rs Resolved
Comment thread pallets/game/src/types.rs Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C0.5-breaking-client-api This PR breaks an API used by off-chain clients (e.g. product-sdk); clients must be updated. D5-nice to have audit optional to have, meant to express "audit it if there is bandwidth"

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let a newly promoted member register for the next game

3 participants