-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathmod.rs
More file actions
580 lines (522 loc) · 20.7 KB
/
mod.rs
File metadata and controls
580 lines (522 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use metrics::SeedingMetrics;
use miden_air::ExecutionProof;
use miden_node_block_producer::store::StoreClient;
use miden_node_proto::domain::batch::BatchInputs;
use miden_node_proto::generated::store::rpc_client::RpcClient;
use miden_node_store::{DataDirectory, GenesisState, Store};
use miden_node_utils::clap::{GrpcOptionsInternal, StorageOptions};
use miden_node_utils::tracing::grpc::OtelInterceptor;
use miden_protocol::account::auth::AuthScheme;
use miden_protocol::account::delta::AccountUpdateDetails;
use miden_protocol::account::{
Account,
AccountBuilder,
AccountDelta,
AccountId,
AccountStorageMode,
AccountType,
};
use miden_protocol::asset::{Asset, FungibleAsset, TokenSymbol};
use miden_protocol::batch::{BatchAccountUpdate, BatchId, ProvenBatch};
use miden_protocol::block::{
BlockHeader,
BlockInputs,
BlockNumber,
FeeParameters,
ProposedBlock,
ProvenBlock,
SignedBlock,
};
use miden_protocol::crypto::dsa::ecdsa_k256_keccak::SecretKey as EcdsaSecretKey;
use miden_protocol::crypto::dsa::falcon512_rpo::{PublicKey, SecretKey};
use miden_protocol::crypto::rand::RpoRandomCoin;
use miden_protocol::errors::AssetError;
use miden_protocol::note::{Note, NoteHeader, NoteId, NoteInclusionProof};
use miden_protocol::transaction::{
InputNote,
InputNotes,
OrderedTransactionHeaders,
OutputNote,
ProvenTransaction,
ProvenTransactionBuilder,
TransactionHeader,
};
use miden_protocol::utils::Serializable;
use miden_protocol::{Felt, ONE, Word};
use miden_standards::account::auth::AuthSingleSig;
use miden_standards::account::faucets::BasicFungibleFaucet;
use miden_standards::account::wallets::BasicWallet;
use miden_standards::note::P2idNote;
use rand::Rng;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rayon::prelude::ParallelSlice;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;
use tokio::{fs, task};
use tonic::service::interceptor::InterceptedService;
use tonic::transport::Channel;
use url::Url;
mod metrics;
// CONSTANTS
// ================================================================================================
const BATCHES_PER_BLOCK: usize = 16;
const TRANSACTIONS_PER_BATCH: usize = 16;
pub const ACCOUNTS_FILENAME: &str = "accounts.txt";
// SEED STORE
// ================================================================================================
/// Seeds the store with a given number of accounts.
pub async fn seed_store(
data_directory: PathBuf,
num_accounts: usize,
public_accounts_percentage: u8,
) {
let start = Instant::now();
// Recreate the data directory (it should be empty for store bootstrapping).
//
// Ignore the error since it will also error if it does not exist.
let _ = fs_err::remove_dir_all(&data_directory);
fs_err::create_dir_all(&data_directory).expect("created data directory");
// generate the faucet account and the genesis state
let faucet = create_faucet();
let fee_params = FeeParameters::new(faucet.id(), 0).unwrap();
let signer = EcdsaSecretKey::new();
let genesis_state = GenesisState::new(vec![faucet.clone()], fee_params, 1, 1, signer.clone());
let genesis_block = genesis_state
.clone()
.into_block()
.await
.expect("genesis block should be created");
Store::bootstrap(&genesis_block, &data_directory).expect("store should bootstrap");
// start the store
let (_, store_url) = start_store(data_directory.clone()).await;
let store_client = StoreClient::new(store_url);
// start generating blocks
let accounts_filepath = data_directory.join(ACCOUNTS_FILENAME);
let data_directory =
miden_node_store::DataDirectory::load(data_directory).expect("data directory should exist");
let genesis_header = genesis_state.into_block().await.unwrap().into_inner();
let metrics = generate_blocks(
num_accounts,
public_accounts_percentage,
faucet,
genesis_header,
&store_client,
data_directory,
accounts_filepath,
&signer,
)
.await;
println!("Total time: {:.3} seconds", start.elapsed().as_secs_f64());
println!("{metrics}");
}
/// Generates batches of transactions to be inserted into the store.
///
/// The first transaction in each batch sends assets from the faucet to 255 accounts.
/// The rest of the transactions consume the notes created by the faucet in the previous block.
#[expect(clippy::too_many_arguments)]
async fn generate_blocks(
num_accounts: usize,
public_accounts_percentage: u8,
mut faucet: Account,
genesis_block: ProvenBlock,
store_client: &StoreClient,
data_directory: DataDirectory,
accounts_filepath: PathBuf,
signer: &EcdsaSecretKey,
) -> SeedingMetrics {
// Each block is composed of [`BATCHES_PER_BLOCK`] batches, and each batch is composed of
// [`TRANSACTIONS_PER_BATCH`] txs. The first note of the block is always a send assets tx
// from the faucet to (BATCHES_PER_BLOCK * TRANSACTIONS_PER_BATCH) - 1 accounts. The rest of
// the notes are consume note txs from the (BATCHES_PER_BLOCK * TRANSACTIONS_PER_BATCH) - 1
// accounts that were minted in the previous block.
let mut metrics = SeedingMetrics::new(data_directory.database_path());
let mut account_ids = vec![];
let mut note_nullifiers = vec![];
let mut consume_notes_txs = vec![];
let consumes_per_block = TRANSACTIONS_PER_BATCH * BATCHES_PER_BLOCK - 1;
#[expect(clippy::cast_sign_loss, clippy::cast_precision_loss)]
let num_public_accounts = (consumes_per_block as f64
* (f64::from(public_accounts_percentage) / 100.0))
.round() as usize;
let num_private_accounts = consumes_per_block - num_public_accounts;
// +1 to account for the first block with the send assets tx only
let total_blocks = (num_accounts / consumes_per_block) + 1;
// share random coin seed and key pair for all accounts to avoid key generation overhead
let coin_seed: [u64; 4] = rand::rng().random();
let rng = Arc::new(Mutex::new(RpoRandomCoin::new(coin_seed.map(Felt::new).into())));
let key_pair = {
let mut rng = rng.lock().unwrap();
SecretKey::with_rng(&mut *rng)
};
let mut prev_block_header = genesis_block.header().clone();
let mut current_anchor_header = genesis_block.header().clone();
for i in 0..total_blocks {
let mut block_txs = Vec::with_capacity(BATCHES_PER_BLOCK * TRANSACTIONS_PER_BATCH);
// create public accounts and notes that mint assets for these accounts
let (pub_accounts, pub_notes) = create_accounts_and_notes(
num_public_accounts,
AccountStorageMode::Public,
&key_pair,
&rng,
faucet.id(),
i,
);
// create private accounts and notes that mint assets for these accounts
let (priv_accounts, priv_notes) = create_accounts_and_notes(
num_private_accounts,
AccountStorageMode::Private,
&key_pair,
&rng,
faucet.id(),
i,
);
let notes = [pub_notes, priv_notes].concat();
let accounts = [pub_accounts, priv_accounts].concat();
account_ids.extend(accounts.iter().map(Account::id));
note_nullifiers.extend(notes.iter().map(|n| n.nullifier().prefix()));
// create the tx that creates the notes
let emit_note_tx = create_emit_note_tx(&prev_block_header, &mut faucet, notes.clone());
// collect all the txs
block_txs.push(emit_note_tx);
block_txs.extend(consume_notes_txs);
// create the batches with [TRANSACTIONS_PER_BATCH] txs each
let batches: Vec<ProvenBatch> = block_txs
.par_chunks(TRANSACTIONS_PER_BATCH)
.map(|txs| create_batch(txs, &prev_block_header))
.collect();
// create the block and send it to the store
let block_inputs = get_block_inputs(store_client, &batches, &mut metrics).await;
// update blocks
prev_block_header =
apply_block(batches, block_inputs, store_client, &mut metrics, signer).await;
if current_anchor_header.block_epoch() != prev_block_header.block_epoch() {
current_anchor_header = prev_block_header.clone();
}
// create the consume notes txs to be used in the next block
let batch_inputs =
get_batch_inputs(store_client, &prev_block_header, ¬es, &mut metrics).await;
consume_notes_txs =
create_consume_note_txs(&prev_block_header, accounts, notes, &batch_inputs.note_proofs);
// track store size every 50 blocks
if i % 50 == 0 {
metrics.record_store_size();
}
}
// dump account ids to a file
let mut file = fs::File::create(accounts_filepath).await.unwrap();
for id in account_ids {
file.write_all(format!("{id}\n").as_bytes()).await.unwrap();
}
metrics
}
/// Given a list of batches and block inputs, creates a `ProvenBlock` and sends it to the store.
/// Tracks the insertion time on the metrics.
///
/// Returns the the inserted block.
async fn apply_block(
batches: Vec<ProvenBatch>,
block_inputs: BlockInputs,
store_client: &StoreClient,
metrics: &mut SeedingMetrics,
signer: &EcdsaSecretKey,
) -> BlockHeader {
let proposed_block = ProposedBlock::new(block_inputs, batches).unwrap();
let (header, body) = proposed_block.clone().into_header_and_body().unwrap();
let block_size: usize = header.to_bytes().len() + body.to_bytes().len();
let signature = signer.sign(header.commitment());
// SAFETY: The header, body, and signature are known to correspond to each other.
let signed_block = SignedBlock::new_unchecked(header, body, signature);
let ordered_batches = proposed_block.batches().clone();
let start = Instant::now();
store_client.apply_block(&ordered_batches, &signed_block).await.unwrap();
metrics.track_block_insertion(start.elapsed(), block_size);
let (header, ..) = signed_block.into_parts();
header
}
// HELPER FUNCTIONS
// ================================================================================================
/// Extract the payable fee as `FungibleAsset` from the given `BlockHeader`.
fn fee_from_block(block_ref: &BlockHeader) -> Result<FungibleAsset, AssetError> {
FungibleAsset::new(
block_ref.fee_parameters().native_asset_id(),
u64::from(block_ref.fee_parameters().verification_base_fee()),
)
}
/// Creates `num_accounts` accounts, and for each one creates a note that mint assets.
///
/// Returns a tuple with:
/// - The list of new accounts
/// - The list of new notes
fn create_accounts_and_notes(
num_accounts: usize,
storage_mode: AccountStorageMode,
key_pair: &SecretKey,
rng: &Arc<Mutex<RpoRandomCoin>>,
faucet_id: AccountId,
block_num: usize,
) -> (Vec<Account>, Vec<Note>) {
(0..num_accounts)
.into_par_iter()
.map(|account_index| {
let account = create_account(
key_pair.public_key(),
((block_num * num_accounts) + account_index) as u64,
storage_mode,
);
let note = {
let mut rng = rng.lock().unwrap();
create_note(faucet_id, account.id(), &mut rng)
};
(account, note)
})
.collect()
}
/// Creates a public P2ID note containing 10 tokens of the fungible asset associated with the
/// specified `faucet_id` and sent to the specified target account.
fn create_note(faucet_id: AccountId, target_id: AccountId, rng: &mut RpoRandomCoin) -> Note {
let asset = Asset::Fungible(FungibleAsset::new(faucet_id, 10).unwrap());
P2idNote::create(
faucet_id,
target_id,
vec![asset],
miden_protocol::note::NoteType::Public,
miden_protocol::note::NoteAttachment::default(),
rng,
)
.expect("note creation failed")
}
/// Creates a new private account with a given public key and anchor block. Generates the seed from
/// the given index.
fn create_account(public_key: PublicKey, index: u64, storage_mode: AccountStorageMode) -> Account {
let init_seed: Vec<_> = index.to_be_bytes().into_iter().chain([0u8; 24]).collect();
AccountBuilder::new(init_seed.try_into().unwrap())
.account_type(AccountType::RegularAccountImmutableCode)
.storage_mode(storage_mode)
.with_auth_component(AuthSingleSig::new(public_key.into(), AuthScheme::Falcon512Rpo))
.with_component(BasicWallet)
.build()
.unwrap()
}
/// Creates a new faucet account.
fn create_faucet() -> Account {
let coin_seed: [u64; 4] = rand::rng().random();
let mut rng = RpoRandomCoin::new(coin_seed.map(Felt::new).into());
let key_pair = SecretKey::with_rng(&mut rng);
let init_seed = [0_u8; 32];
let token_symbol = TokenSymbol::new("TEST").unwrap();
AccountBuilder::new(init_seed)
.account_type(AccountType::FungibleFaucet)
.storage_mode(AccountStorageMode::Private)
.with_component(BasicFungibleFaucet::new(token_symbol, 2, Felt::new(u64::MAX)).unwrap())
.with_auth_component(AuthSingleSig::new(
key_pair.public_key().into(),
AuthScheme::Falcon512Rpo,
))
.build()
.unwrap()
}
/// Creates a proven batch from a list of transactions and a reference block.
fn create_batch(txs: &[ProvenTransaction], block_ref: &BlockHeader) -> ProvenBatch {
let account_updates = txs
.iter()
.map(|tx| (tx.account_id(), BatchAccountUpdate::from_transaction(tx)))
.collect();
let input_notes = txs.iter().flat_map(|tx| tx.input_notes().iter().cloned()).collect();
let output_notes = txs.iter().flat_map(|tx| tx.output_notes().iter().cloned()).collect();
ProvenBatch::new(
BatchId::from_transactions(txs.iter()),
block_ref.commitment(),
block_ref.block_num(),
account_updates,
InputNotes::new(input_notes).unwrap(),
output_notes,
BlockNumber::MAX,
OrderedTransactionHeaders::new_unchecked(txs.iter().map(TransactionHeader::from).collect()),
)
.unwrap()
}
/// For each pair of account and note, creates a transaction that consumes the note.
fn create_consume_note_txs(
block_ref: &BlockHeader,
accounts: Vec<Account>,
notes: Vec<Note>,
note_proofs: &BTreeMap<NoteId, NoteInclusionProof>,
) -> Vec<ProvenTransaction> {
accounts
.into_iter()
.zip(notes)
.map(|(account, note)| {
let inclusion_proof = note_proofs.get(¬e.id()).unwrap();
create_consume_note_tx(
block_ref,
account,
InputNote::authenticated(note, inclusion_proof.clone()),
)
})
.collect()
}
/// Creates a transaction that creates an account and consumes the given input note.
///
/// The account is updated with the assets from the input note, and the nonce is incremented.
fn create_consume_note_tx(
block_ref: &BlockHeader,
mut account: Account,
input_note: InputNote,
) -> ProvenTransaction {
let init_hash = account.initial_commitment();
input_note.note().assets().iter().for_each(|asset| {
account.vault_mut().add_asset(*asset).unwrap();
});
account.increment_nonce(ONE).unwrap();
let (details, account_delta_commitment) = if account.is_public() {
let account_delta = AccountDelta::try_from(account.clone()).unwrap();
let commitment = account_delta.clone().to_commitment();
(AccountUpdateDetails::Delta(account_delta), commitment)
} else {
(AccountUpdateDetails::Private, Word::empty())
};
ProvenTransactionBuilder::new(
account.id(),
init_hash,
account.to_commitment(),
account_delta_commitment,
block_ref.block_num(),
block_ref.commitment(),
fee_from_block(block_ref).unwrap(),
u32::MAX.into(),
ExecutionProof::new_dummy(),
)
.add_input_notes(vec![input_note])
.account_update_details(details)
.build()
.unwrap()
}
/// Creates a transaction from the faucet that creates the given output notes.
/// Updates the faucet account to increase the issuance slot and it's nonce.
fn create_emit_note_tx(
block_ref: &BlockHeader,
faucet: &mut Account,
output_notes: Vec<Note>,
) -> ProvenTransaction {
let initial_account_hash = faucet.to_commitment();
let metadata_slot_name = BasicFungibleFaucet::metadata_slot();
let slot = faucet.storage().get_item(metadata_slot_name).unwrap();
faucet
.storage_mut()
.set_item(metadata_slot_name, [slot[0] + Felt::new(10), slot[1], slot[2], slot[3]].into())
.unwrap();
faucet.increment_nonce(ONE).unwrap();
ProvenTransactionBuilder::new(
faucet.id(),
initial_account_hash,
faucet.to_commitment(),
Word::empty(),
block_ref.block_num(),
block_ref.commitment(),
FungibleAsset::new(
block_ref.fee_parameters().native_asset_id(),
u64::from(block_ref.fee_parameters().verification_base_fee()),
)
.unwrap(),
u32::MAX.into(),
ExecutionProof::new_dummy(),
)
.add_output_notes(output_notes.into_iter().map(OutputNote::Full).collect::<Vec<OutputNote>>())
.build()
.unwrap()
}
/// Gets the batch inputs from the store and tracks the query time on the metrics.
async fn get_batch_inputs(
store_client: &StoreClient,
block_ref: &BlockHeader,
notes: &[Note],
metrics: &mut SeedingMetrics,
) -> BatchInputs {
let start = Instant::now();
// Mark every note as unauthenticated, so that the store returns the inclusion proofs for all of
// them
let batch_inputs = store_client
.get_batch_inputs(
vec![(block_ref.block_num(), block_ref.commitment())].into_iter(),
notes.iter().map(Note::commitment),
)
.await
.unwrap();
metrics.add_get_batch_inputs(start.elapsed());
batch_inputs
}
/// Gets the block inputs from the store and tracks the query time on the metrics.
async fn get_block_inputs(
store_client: &StoreClient,
batches: &[ProvenBatch],
metrics: &mut SeedingMetrics,
) -> BlockInputs {
let start = Instant::now();
let inputs = store_client
.get_block_inputs(
batches.iter().flat_map(ProvenBatch::updated_accounts),
batches.iter().flat_map(ProvenBatch::created_nullifiers),
batches.iter().flat_map(|batch| {
batch
.input_notes()
.into_iter()
.filter_map(|note| note.header().map(NoteHeader::commitment))
}),
batches.iter().map(ProvenBatch::reference_block_num),
)
.await
.unwrap();
let get_block_inputs_time = start.elapsed();
metrics.add_get_block_inputs(get_block_inputs_time);
inputs
}
/// Runs the store with the given data directory. Returns a tuple with:
/// - a gRPC client to access the store
/// - the URL of the store
///
/// The store uses a local prover.
pub async fn start_store(
data_directory: PathBuf,
) -> (RpcClient<InterceptedService<Channel, OtelInterceptor>>, Url) {
let rpc_listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("Failed to bind store RPC gRPC endpoint");
let block_producer_listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("Failed to bind store block-producer gRPC endpoint");
let store_addr = rpc_listener.local_addr().expect("Failed to get store RPC address");
let ntx_builder_listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("Failed to bind store ntx-builder gRPC endpoint");
let store_block_producer_addr = block_producer_listener
.local_addr()
.expect("Failed to get store block-producer address");
let dir = data_directory.clone();
task::spawn(async move {
Store {
rpc_listener,
block_prover_url: None,
ntx_builder_listener,
block_producer_listener,
data_directory: dir,
grpc_options: GrpcOptionsInternal::bench(),
max_concurrent_proofs: miden_node_store::DEFAULT_MAX_CONCURRENT_PROOFS,
storage_options: StorageOptions::bench(),
}
.serve()
.await
.expect("Failed to start serving store");
});
let channel = tonic::transport::Endpoint::try_from(format!("http://{store_addr}",))
.unwrap()
.connect()
.await
.expect("Failed to connect to store");
// SAFETY: The store_block_producer_addr is always valid as it is created from a `SocketAddr`.
let store_url = Url::parse(&format!("http://{store_block_producer_addr}")).unwrap();
(RpcClient::with_interceptor(channel, OtelInterceptor), store_url)
}