treat genesis special (#2615)
* treat genesis special * fix poh_recorder to understand new world order
This commit is contained in:
parent
84567d36cf
commit
8ba1d5f426
23
src/bank.rs
23
src/bank.rs
|
@ -177,7 +177,11 @@ impl Bank {
|
|||
|
||||
self.accounts
|
||||
.store_slow(true, &genesis_block.mint_id, &mint_account);
|
||||
self.register_tick(&genesis_block.last_id());
|
||||
|
||||
self.last_id_queue
|
||||
.write()
|
||||
.unwrap()
|
||||
.genesis_last_id(&genesis_block.last_id());
|
||||
}
|
||||
|
||||
fn add_system_program(&self) {
|
||||
|
@ -757,18 +761,12 @@ impl Bank {
|
|||
|
||||
/// Starting from the genesis block, append the provided entries to the ledger verifying them
|
||||
/// along the way.
|
||||
pub fn process_ledger<I>(
|
||||
&mut self,
|
||||
genesis_block: &GenesisBlock,
|
||||
entries: I,
|
||||
) -> Result<(u64, Hash)>
|
||||
pub fn process_ledger<I>(&mut self, entries: I) -> Result<(u64, Hash)>
|
||||
where
|
||||
I: IntoIterator<Item = Entry>,
|
||||
{
|
||||
let mut entry_height = 0;
|
||||
let mut last_id = genesis_block.last_id();
|
||||
self.last_id_queue = RwLock::new(LastIdQueue::default());
|
||||
self.status_cache = RwLock::new(BankStatusCache::default());
|
||||
let mut last_id = self.last_id();
|
||||
|
||||
// Ledger verification needs to be parallelized, but we can't pull the whole
|
||||
// thing into memory. We therefore chunk it.
|
||||
|
@ -1271,8 +1269,9 @@ mod tests {
|
|||
let (genesis_block, mint_keypair, ledger) = create_sample_ledger(100, 2);
|
||||
let mut bank = Bank::default();
|
||||
bank.process_genesis_block(&genesis_block);
|
||||
assert_eq!(bank.tick_height(), 0);
|
||||
bank.add_system_program();
|
||||
let (ledger_height, last_id) = bank.process_ledger(&genesis_block, ledger).unwrap();
|
||||
let (ledger_height, last_id) = bank.process_ledger(ledger).unwrap();
|
||||
assert_eq!(bank.get_balance(&mint_keypair.pubkey()), 98);
|
||||
assert_eq!(ledger_height, 4);
|
||||
assert_eq!(bank.tick_height(), 2);
|
||||
|
@ -1305,11 +1304,11 @@ mod tests {
|
|||
let mut bank0 = Bank::default();
|
||||
bank0.add_system_program();
|
||||
bank0.process_genesis_block(&genesis_block);
|
||||
bank0.process_ledger(&genesis_block, ledger0).unwrap();
|
||||
bank0.process_ledger(ledger0).unwrap();
|
||||
let mut bank1 = Bank::default();
|
||||
bank1.add_system_program();
|
||||
bank1.process_genesis_block(&genesis_block);
|
||||
bank1.process_ledger(&genesis_block, ledger1).unwrap();
|
||||
bank1.process_ledger(ledger1).unwrap();
|
||||
|
||||
let initial_state = bank0.hash_internal_state();
|
||||
|
||||
|
|
|
@ -396,9 +396,7 @@ impl Fullnode {
|
|||
let entries = db_ledger.read_ledger().expect("opening ledger");
|
||||
info!("processing ledger...");
|
||||
|
||||
let (entry_height, last_entry_id) = bank
|
||||
.process_ledger(genesis_block, entries)
|
||||
.expect("process_ledger");
|
||||
let (entry_height, last_entry_id) = bank.process_ledger(entries).expect("process_ledger");
|
||||
// entry_height is the network-wide agreed height of the ledger.
|
||||
// initialize it from the input ledger
|
||||
info!(
|
||||
|
|
|
@ -52,6 +52,19 @@ impl LastIdQueue {
|
|||
pub fn check_entry(&self, entry_id: Hash) -> bool {
|
||||
self.entries.get(&entry_id).is_some()
|
||||
}
|
||||
|
||||
pub fn genesis_last_id(&mut self, last_id: &Hash) {
|
||||
self.entries.insert(
|
||||
*last_id,
|
||||
LastIdEntry {
|
||||
tick_height: 0,
|
||||
timestamp: timestamp(),
|
||||
},
|
||||
);
|
||||
|
||||
self.last_id = Some(*last_id);
|
||||
}
|
||||
|
||||
/// Tell the bank which Entry IDs exist on the ledger. This function
|
||||
/// assumes subsequent calls correspond to later entries, and will boot
|
||||
/// the oldest ones once its internal cache is full. Once boot, the
|
||||
|
|
|
@ -122,12 +122,12 @@ mod tests {
|
|||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn test_poh() {
|
||||
fn test_poh_recorder() {
|
||||
let (genesis_block, _mint_keypair) = GenesisBlock::new(1);
|
||||
let bank = Arc::new(Bank::new(&genesis_block));
|
||||
let prev_id = bank.last_id();
|
||||
let (entry_sender, entry_receiver) = channel();
|
||||
let mut poh_recorder = PohRecorder::new(bank, entry_sender, prev_id, Some(3));
|
||||
let mut poh_recorder = PohRecorder::new(bank, entry_sender, prev_id, Some(2));
|
||||
|
||||
//send some data
|
||||
let h1 = hash(b"hello world!");
|
||||
|
@ -135,16 +135,16 @@ mod tests {
|
|||
poh_recorder.record(h1, vec![tx.clone()]).unwrap();
|
||||
//get some events
|
||||
let e = entry_receiver.recv().unwrap();
|
||||
assert_eq!(e[0].tick_height, 0); // super weird case, but ok!
|
||||
|
||||
poh_recorder.tick().unwrap();
|
||||
let e = entry_receiver.recv().unwrap();
|
||||
assert_eq!(e[0].tick_height, 1);
|
||||
|
||||
poh_recorder.tick().unwrap();
|
||||
let e = entry_receiver.recv().unwrap();
|
||||
assert_eq!(e[0].tick_height, 2);
|
||||
|
||||
poh_recorder.tick().unwrap();
|
||||
let e = entry_receiver.recv().unwrap();
|
||||
assert_eq!(e[0].tick_height, 3);
|
||||
|
||||
// max tick height reached
|
||||
assert!(poh_recorder.tick().is_err());
|
||||
assert!(poh_recorder.record(h1, vec![tx]).is_err());
|
||||
|
|
Loading…
Reference in New Issue