Add ability to hard fork at any slot (#7801)

automerge
This commit is contained in:
Michael Vines
2020-01-24 18:27:04 -07:00
committed by Grimes
parent a2f2c46f87
commit 989355e885
8 changed files with 227 additions and 36 deletions

View File

@ -6,6 +6,7 @@ use crate::{
accounts::{Accounts, TransactionAccounts, TransactionLoadResult, TransactionLoaders},
accounts_db::{AccountStorageEntry, AccountsDBSerialize, AppendVecId, ErrorCounters},
blockhash_queue::BlockhashQueue,
hard_forks::HardForks,
message_processor::{MessageProcessor, ProcessInstruction},
nonce_utils,
rent_collector::RentCollector,
@ -35,7 +36,7 @@ use solana_sdk::{
epoch_schedule::EpochSchedule,
fee_calculator::FeeCalculator,
genesis_config::GenesisConfig,
hash::{hashv, Hash},
hash::{extend_and_hash, hashv, Hash},
inflation::Inflation,
native_loader,
nonce_state::NonceState,
@ -226,6 +227,9 @@ pub struct Bank {
/// parent's slot
parent_slot: Slot,
/// slots to hard fork at
hard_forks: Arc<RwLock<HardForks>>,
/// The number of transactions processed without error
#[serde(serialize_with = "serialize_atomicu64")]
#[serde(deserialize_with = "deserialize_atomicu64")]
@ -422,6 +426,7 @@ impl Bank {
signature_count: AtomicU64::new(0),
message_processor: MessageProcessor::default(),
entered_epoch_callback: parent.entered_epoch_callback.clone(),
hard_forks: parent.hard_forks.clone(),
last_vote_sync: AtomicU64::new(parent.last_vote_sync.load(Ordering::Relaxed)),
};
@ -1685,6 +1690,10 @@ impl Bank {
*self.inflation.write().unwrap() = inflation;
}
pub fn hard_forks(&self) -> Arc<RwLock<HardForks>> {
self.hard_forks.clone()
}
pub fn set_entered_epoch_callback(&self, entered_epoch_callback: EnteredEpochCallback) {
*self.entered_epoch_callback.write().unwrap() = Some(entered_epoch_callback);
}
@ -1779,20 +1788,33 @@ impl Bank {
let accounts_delta_hash = self.rc.accounts.bank_hash_info_at(self.slot());
let mut signature_count_buf = [0u8; 8];
LittleEndian::write_u64(&mut signature_count_buf[..], self.signature_count() as u64);
let hash = hashv(&[
let mut hash = hashv(&[
self.parent_hash.as_ref(),
accounts_delta_hash.hash.as_ref(),
&signature_count_buf,
self.last_blockhash().as_ref(),
]);
if let Some(buf) = self
.hard_forks
.read()
.unwrap()
.get_hash_data(self.slot(), self.parent_slot())
{
info!("hard fork at bank {}", self.slot());
hash = extend_and_hash(&hash, &buf)
}
info!(
"bank frozen: {} hash: {} accounts_delta: {} signature_count: {} last_blockhash: {}",
self.slot(),
hash,
accounts_delta_hash.hash,
self.signature_count(),
self.last_blockhash()
self.last_blockhash(),
);
info!(
"accounts hash slot: {} stats: {:?}",
self.slot(),
@ -2147,6 +2169,7 @@ impl From<LegacyBank0223> for Bank {
storage_accounts: bank.storage_accounts,
parent_hash: bank.parent_hash,
parent_slot: bank.parent_slot,
hard_forks: Arc::new(RwLock::new(HardForks::default())),
collector_id: bank.collector_id,
collector_fees: bank.collector_fees,
ancestors: bank.ancestors,

91
runtime/src/hard_forks.rs Normal file
View File

@ -0,0 +1,91 @@
//! The `hard_forks` module is used to maintain the list of slot boundaries for when a hard fork
//! should occur.
use byteorder::{ByteOrder, LittleEndian};
use solana_sdk::clock::Slot;
use std::ops::Add;
#[derive(Default, Clone, Deserialize, Serialize)]
pub struct HardForks {
hard_forks: Vec<(Slot, usize)>,
}
impl HardForks {
// Register a fork to occur at all slots >= `slot` with a parent slot < `slot`
pub fn register(&mut self, new_slot: Slot) {
if let Some(i) = self
.hard_forks
.iter()
.position(|(slot, _)| *slot == new_slot)
{
self.hard_forks[i] = (new_slot, self.hard_forks[i].1 + 1);
} else {
self.hard_forks.push((new_slot, 1));
}
self.hard_forks.sort();
}
// Returns a sorted-by-slot iterator over the registered hark forks
pub fn iter(&self) -> std::slice::Iter<(Slot, usize)> {
self.hard_forks.iter()
}
// Returns data to include in the bank hash for the given slot if a hard fork is scheduled
pub fn get_hash_data(&self, slot: Slot, parent_slot: Slot) -> Option<[u8; 8]> {
// The expected number of hard forks in a cluster is small.
// If this turns out to be false then a more efficient data
// structure may be needed here to avoid this linear search
let fork_count = self
.hard_forks
.iter()
.fold(0, |acc, (fork_slot, fork_count)| {
acc.add(if parent_slot < *fork_slot && slot >= *fork_slot {
*fork_count
} else {
0
})
});
if fork_count > 0 {
let mut buf = [0u8; 8];
LittleEndian::write_u64(&mut buf[..], fork_count as u64);
Some(buf)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn iter_is_sorted() {
let mut hf = HardForks::default();
hf.register(30);
hf.register(20);
hf.register(10);
hf.register(20);
assert_eq!(
hf.iter().map(|i| *i).collect::<Vec<_>>(),
vec![(10, 1), (20, 2), (30, 1)]
);
}
#[test]
fn multiple_hard_forks_since_parent() {
let mut hf = HardForks::default();
hf.register(10);
hf.register(20);
assert_eq!(hf.get_hash_data(9, 0), None);
assert_eq!(hf.get_hash_data(10, 0), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
assert_eq!(hf.get_hash_data(19, 0), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
assert_eq!(hf.get_hash_data(20, 0), Some([2, 0, 0, 0, 0, 0, 0, 0,]));
assert_eq!(hf.get_hash_data(20, 10), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
assert_eq!(hf.get_hash_data(20, 11), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
assert_eq!(hf.get_hash_data(21, 11), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
assert_eq!(hf.get_hash_data(21, 20), None);
}
}

View File

@ -7,6 +7,7 @@ pub mod bank_client;
mod blockhash_queue;
pub mod bloom;
pub mod genesis_utils;
pub mod hard_forks;
pub mod loader_utils;
pub mod message_processor;
mod native_loader;