Skip to content

Commit 31aea5e

Browse files
committed
finalize mining logic and responsive game experience
1 parent 194a58c commit 31aea5e

8 files changed

Lines changed: 85 additions & 22 deletions

File tree

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Block Mining Game</title>
8-
<script type="module" crossorigin src="/assets/index-C9mi4mZc.js"></script>
9-
<link rel="stylesheet" crossorigin href="/assets/index-DTY38IHq.css">
8+
<script type="module" crossorigin src="/assets/index-CbyRdsZX.js"></script>
9+
<link rel="stylesheet" crossorigin href="/assets/index-DBLnAlOx.css">
1010
</head>
1111
<body>
1212
<div id="root"></div>

‎scripts/simulate-blocks.mjs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ for (const scenario of scenarios) {
5555
if (state.mempool.length !== 15) throw new Error(`${difficulty} seed ${seedIndex} block ${block}: expected 15 transactions`);
5656
const valid = getValidBlockSelections(state.mempool, state.balances);
5757
const maximum = Math.max(...valid.map((item) => item.totalFees));
58-
if (valid.length < 2 || valid.filter((item) => item.totalFees === maximum).length !== 1) {
58+
const optimalCount = valid.filter((item) => item.totalFees === maximum).length;
59+
const fees = state.mempool.map((tx) => tx.fee);
60+
const minimumFee = Math.min(...fees);
61+
const minimumFeeCount = fees.filter((fee) => fee === minimumFee).length;
62+
if (valid.length < 2 || optimalCount < 1 || optimalCount > 3
63+
|| minimumFee < 2 || minimumFeeCount > 2 || new Set(fees).size < 7) {
5964
throw new Error(`${difficulty} seed ${seedIndex} block ${block}: mempool quality failed`);
6065
}
6166
const naive = [...state.mempool]

‎src/games/HardGame.jsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ export default function HardGame({
556556
rejectedId={rejectedTxId}
557557
rejectionMessage={rejectedTxId != null && messageKey ? tr(messageKey) : ''}
558558
disabled={gameOver || rollingDice || isSubmitting}
559-
showUserIcons={false}
560559
/>
561560
</PanelCard>
562561
<PanelCard

‎src/lib/mempool.js‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ import { USERS } from './gameConstants.js';
22
import { createSeededRandom } from './seededRandom.js';
33
import { getValidBlockSelections, isSelectionAffordable } from './txSelection.js';
44

5-
const CORE_FEES = [8, 7, 6, 4];
6-
const FILLER_FEES = [5, 4, 3, 3, 2, 2, 1, 1, 1];
5+
const FEE_PROFILES = [0, 1].map((offset) => ({
6+
core: [8, 7, 6, 4].map((fee) => fee + offset),
7+
conflict: [10 + offset],
8+
fillers: [9, 8, 5, 5, 4, 3, 3, 2, 2].map((fee) => fee + offset),
9+
invalid: 6 + offset,
10+
}));
11+
12+
function feeProfile(rng) {
13+
return FEE_PROFILES[Math.floor(rng() * FEE_PROFILES.length)];
14+
}
715

816
function txDate(blockNum) {
917
return `2026/05/${String(blockNum).padStart(2, '0')}`;
@@ -62,7 +70,8 @@ function strategicQuality(pool, balances) {
6270
const valid = getValidBlockSelections(pool, balances);
6371
if (valid.length < 2) return false;
6472
const maximum = Math.max(...valid.map((selection) => selection.totalFees));
65-
if (valid.filter((selection) => selection.totalFees === maximum).length !== 1) return false;
73+
const optimalCount = valid.filter((selection) => selection.totalFees === maximum).length;
74+
if (optimalCount < 1 || optimalCount > 3) return false;
6675
const topThree = [...pool]
6776
.sort((a, b) => b.fee - a.fee || a.id - b.id)
6877
.slice(0, 3);
@@ -94,38 +103,39 @@ function conflictPair({ sender, balance, fees, startId, blockNum, rng, conflicts
94103
function buildInitialPool({ balances, blockNum, roomSeed }) {
95104
const rng = createSeededRandom(`${roomSeed || 'solo'}-mempool-${blockNum}`);
96105
const startId = blockNum * 100;
106+
const profile = feeProfile(rng);
97107
const orderedUsers = shuffled(USERS, rng)
98108
.sort((a, b) => (balances[b] ?? 0) - (balances[a] ?? 0));
99-
const core = CORE_FEES.map((fee, index) => {
109+
const core = profile.core.map((fee, index) => {
100110
const sender = orderedUsers[index % orderedUsers.length];
101111
const available = Math.max(1, (balances[sender] ?? 0) - fee);
102112
return {
103113
id: startId + index,
104114
sender,
105115
receiver: receiverFor(sender, balances, rng),
106-
amount: Math.min(8, Math.max(1, Math.floor(available / 6))),
116+
amount: Math.min(available, 6 + Math.floor(rng() * 11)),
107117
fee,
108118
date: txDate(blockNum),
109119
};
110120
});
111121
const conflict = conflictPair({
112122
sender: core[0].sender,
113123
balance: balances[core[0].sender] ?? 0,
114-
fees: [10],
124+
fees: profile.conflict,
115125
startId: startId + core.length,
116126
blockNum,
117127
rng,
118128
conflictsWithCost: core[0].amount + core[0].fee,
119129
});
120-
const fillers = FILLER_FEES.map((fee, index) => {
130+
const fillers = profile.fillers.map((fee, index) => {
121131
const sender = USERS[(index + Math.floor(rng() * USERS.length)) % USERS.length];
122132
const receiver = receiverFor(sender, balances, rng);
123133
const maxAmount = Math.max(1, (balances[sender] ?? 0) - fee);
124134
return {
125135
id: startId + core.length + conflict.length + index,
126136
sender,
127137
receiver,
128-
amount: Math.min(10, Math.max(1, Math.floor(maxAmount / 5))),
138+
amount: Math.min(maxAmount, 6 + Math.floor(rng() * 15)),
129139
fee,
130140
date: txDate(blockNum),
131141
};
@@ -136,18 +146,19 @@ function buildInitialPool({ balances, blockNum, roomSeed }) {
136146
sender: invalidSender,
137147
receiver: receiverFor(invalidSender, balances, rng),
138148
amount: (balances[invalidSender] ?? 0) + 5,
139-
fee: 1,
149+
fee: profile.invalid,
140150
date: txDate(blockNum),
141151
};
142152
const pool = finalizePool([...core, ...conflict, ...fillers, invalid], rng);
143153
if (strategicQuality(pool, balances)) return pool;
144-
const fallback = allocateTransactions({ balances, fees: [10, 8, 7, 6, 4, 3], rng, startId, blockNum });
154+
const fallbackFees = [...profile.core, ...profile.conflict, ...profile.fillers, profile.invalid];
155+
const fallback = allocateTransactions({ balances, fees: fallbackFees, rng, startId, blockNum });
145156
return finalizePool(fallback, rng);
146157
}
147158

148159
/**
149-
* Build a deterministic, playable mempool. The two highest-fee choices share
150-
* a sender and cannot be combined, so the best block requires reasoning about
160+
* Build a deterministic, playable mempool. A high-fee decoy conflicts with
161+
* another leading candidate, so the best block requires reasoning about
151162
* cumulative balances rather than simply taking the three largest fees.
152163
*/
153164
export function generateMempool({ balances, blockNum = 1, roomSeed = '' }) {
@@ -171,21 +182,22 @@ export function replenishMempool(mempool, balances, blockNum, roomSeed) {
171182
if (!alternative) return buildInitialPool({ balances, blockNum, roomSeed });
172183

173184
const startId = Math.max(blockNum * 100, ...mempool.map((tx) => tx.id)) + 1;
185+
const profile = feeProfile(rng);
174186
const conflictSender = [...USERS]
175187
.filter((name) => name !== alternative.sender)
176188
.sort((a, b) => (balances[b] ?? 0) - (balances[a] ?? 0))[0] ?? USERS[0];
177189
const pair = conflictPair({
178190
sender: conflictSender,
179191
balance: balances[conflictSender] ?? 0,
180-
fees: [10, 9],
192+
fees: [profile.conflict[0], profile.core[0]],
181193
startId,
182194
blockNum,
183195
rng,
184196
});
185197
const support = allocateTransactions({
186198
blockNum,
187199
balances,
188-
fees: [7],
200+
fees: [profile.core[2]],
189201
rng,
190202
startId: startId + pair.length,
191203
reserved: [alternative, ...pair],

‎src/styles/game-layout.css‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,13 +936,15 @@
936936
.mempool-amount-value,
937937
.mempool-fee-mobile {
938938
display: inline-flex;
939-
align-items: baseline;
939+
align-items: center;
940+
justify-content: center;
940941
gap: 4px;
941942
min-height: 25px;
942943
padding: 4px 7px;
943944
border: 1px solid var(--bp-200);
944945
border-radius: 999px;
945946
background: var(--bp-50);
947+
line-height: 1;
946948
}
947949

948950
.mempool-fee-mobile {
@@ -3147,7 +3149,7 @@ button.bp-candidate-slot--filled:focus-visible {
31473149
grid-template-columns: auto minmax(0, 1fr) auto;
31483150
grid-template-areas:
31493151
"avatar name value"
3150-
"avatar reserved reserved";
3152+
"avatar name reserved";
31513153
align-items: center;
31523154
column-gap: 6px;
31533155
min-width: 0;
@@ -3164,20 +3166,26 @@ button.bp-candidate-slot--filled:focus-visible {
31643166

31653167
.bp-balance-chip__name {
31663168
grid-area: name;
3169+
align-self: center;
31673170
overflow: hidden;
31683171
color: var(--bp-text);
31693172
font-size: 0.72rem;
31703173
font-weight: 700;
3174+
line-height: 1.1;
31713175
text-overflow: ellipsis;
31723176
white-space: nowrap;
31733177
}
31743178

31753179
.bp-balance-chip__value {
31763180
grid-area: value;
3181+
align-self: center;
3182+
justify-self: end;
31773183
color: var(--bp-blue);
31783184
font-family: var(--bp-mono);
31793185
font-size: 0.88rem;
31803186
font-variant-numeric: tabular-nums;
3187+
line-height: 1;
3188+
text-align: right;
31813189
}
31823190

31833191
.bp-balance-chip__reserved {

‎tests/gameEngine.test.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
evaluateBlockSelection,
1414
getAvailableBalances,
1515
getMaximumFeeTotal,
16+
getValidBlockSelections,
1617
getTransactionsInSelectionOrder,
1718
} from '../src/lib/txSelection.js';
1819
import { compactToTargetHash, isProofOfWorkValid } from '../src/lib/targetHash.js';
@@ -141,6 +142,44 @@ describe('authoritative game engine', () => {
141142
expect(accounted).toBe(400);
142143
});
143144

145+
it('keeps 100 twelve-block races playable, varied and strategically non-trivial', async () => {
146+
for (let seedIndex = 0; seedIndex < 100; seedIndex += 1) {
147+
const roomSeed = `MEMPOOL-QUALITY-${seedIndex}`;
148+
let state = createInitialGameState('easy', roomSeed);
149+
150+
for (let block = 0; block < 12; block += 1) {
151+
expect(state.mempool).toHaveLength(15);
152+
const fees = state.mempool.map((tx) => tx.fee);
153+
const minimumFee = Math.min(...fees);
154+
expect(minimumFee).toBeGreaterThanOrEqual(2);
155+
expect(fees.filter((fee) => fee === minimumFee).length).toBeLessThanOrEqual(2);
156+
expect(new Set(fees).size).toBeGreaterThanOrEqual(7);
157+
158+
const valid = getValidBlockSelections(state.mempool, state.balances)
159+
.sort((a, b) => b.totalFees - a.totalFees);
160+
expect(valid.length).toBeGreaterThanOrEqual(2);
161+
const maximumFees = valid[0].totalFees;
162+
expect(valid.filter((selection) => selection.totalFees === maximumFees).length)
163+
.toBeLessThanOrEqual(3);
164+
165+
const naive = [...state.mempool]
166+
.sort((a, b) => b.fee - a.fee || a.id - b.id)
167+
.slice(0, 3)
168+
.map((tx) => tx.id);
169+
expect(evaluateBlockSelection(state.mempool, naive, state.balances).ok).toBe(false);
170+
171+
const result = await validateAndApplyMine({
172+
difficulty: 'easy',
173+
roomSeed,
174+
state,
175+
proof: easyProof(state, valid[0].ids),
176+
});
177+
expect(result.ok).toBe(true);
178+
state = result.state;
179+
}
180+
}
181+
});
182+
144183
it('rejects malformed selection, wrong proof and replayed block index', async () => {
145184
const state = createInitialGameState('easy', 'TEST-SEED-0002');
146185
expect(validateSelection(state, [1, 1, 1]).error).toBe('INVALID_SELECTION');

0 commit comments

Comments
 (0)