From 4576250342ddf0c61df67d6d9749ee46f95c0061 Mon Sep 17 00:00:00 2001 From: Sagar Dhawan Date: Wed, 15 May 2019 13:28:56 -0700 Subject: [PATCH] Fix bug in storage processor and remove duplicate Constant (#4294) * Fix bug in storage processor and remove duplicate Constant * Add test * Bump replicator timeout --- core/src/chacha.rs | 3 +-- core/src/chacha_cuda.rs | 3 +-- core/src/replicator.rs | 5 ++--- core/src/storage_stage.rs | 4 ++-- programs/storage_api/src/lib.rs | 2 +- programs/storage_api/src/storage_contract.rs | 6 +++--- programs/storage_api/src/storage_processor.rs | 18 ++++++++++++++++++ 7 files changed, 28 insertions(+), 13 deletions(-) diff --git a/core/src/chacha.rs b/core/src/chacha.rs index e5c14081a9..1ca2db1473 100644 --- a/core/src/chacha.rs +++ b/core/src/chacha.rs @@ -1,12 +1,11 @@ use crate::blocktree::Blocktree; +use solana_storage_api::SLOTS_PER_SEGMENT; use std::fs::File; use std::io; use std::io::{BufWriter, Write}; use std::path::Path; use std::sync::Arc; -use crate::storage_stage::SLOTS_PER_SEGMENT; - pub const CHACHA_BLOCK_SIZE: usize = 64; pub const CHACHA_KEY_SIZE: usize = 32; diff --git a/core/src/chacha_cuda.rs b/core/src/chacha_cuda.rs index 44aab635e9..cc6626cff6 100644 --- a/core/src/chacha_cuda.rs +++ b/core/src/chacha_cuda.rs @@ -7,12 +7,11 @@ use crate::sigverify::{ chacha_cbc_encrypt_many_sample, chacha_end_sha_state, chacha_init_sha_state, }; use solana_sdk::hash::Hash; +use solana_storage_api::SLOTS_PER_SEGMENT; use std::io; use std::mem::size_of; use std::sync::Arc; -use crate::storage_stage::SLOTS_PER_SEGMENT; - // Encrypt a file with multiple starting IV states, determined by ivecs.len() // // Then sample each block at the offsets provided by samples argument with sha256 diff --git a/core/src/replicator.rs b/core/src/replicator.rs index c3053f515f..ab8b7544ca 100644 --- a/core/src/replicator.rs +++ b/core/src/replicator.rs @@ -9,7 +9,6 @@ use crate::packet::to_shared_blob; use crate::repair_service::{RepairSlotRange, RepairStrategy}; use crate::result::Result; use crate::service::Service; -use crate::storage_stage::SLOTS_PER_SEGMENT; use crate::streamer::receiver; use crate::streamer::responder; use crate::window_service::WindowService; @@ -29,7 +28,7 @@ use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::system_transaction; use solana_sdk::transaction::Transaction; use solana_sdk::transport::TransportError; -use solana_storage_api::{get_segment_from_slot, storage_instruction}; +use solana_storage_api::{get_segment_from_slot, storage_instruction, SLOTS_PER_SEGMENT}; use std::fs::File; use std::io; use std::io::BufReader; @@ -530,7 +529,7 @@ impl Replicator { if get_segment_from_slot(storage_slot) != 0 { return Ok((storage_blockhash, storage_slot)); } - sleep(Duration::from_secs(3)); + sleep(Duration::from_secs(5)); } Err(Error::new( ErrorKind::Other, diff --git a/core/src/storage_stage.rs b/core/src/storage_stage.rs index 54428e4e6b..76b1fe8386 100644 --- a/core/src/storage_stage.rs +++ b/core/src/storage_stage.rs @@ -60,7 +60,6 @@ pub const STORAGE_ROTATE_TEST_COUNT: u64 = 2; // TODO: some way to dynamically size NUM_IDENTITIES const NUM_IDENTITIES: usize = 1024; pub const NUM_STORAGE_SAMPLES: usize = 4; -pub const SLOTS_PER_SEGMENT: u64 = 16; const KEY_SIZE: usize = 64; type InstructionSender = Sender; @@ -463,6 +462,7 @@ mod tests { use solana_sdk::hash::{Hash, Hasher}; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; + use solana_storage_api::SLOTS_PER_SEGMENT; use std::cmp::{max, min}; use std::fs::remove_dir_all; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; @@ -547,7 +547,7 @@ mod tests { let mut result = storage_state.get_mining_result(&signature); assert_eq!(result, Hash::default()); - for i in slot..slot + 3 { + for i in slot..slot + SLOTS_PER_SEGMENT + 1 { blocktree .write_entries(i, 0, 0, ticks_per_slot, &entries) .unwrap(); diff --git a/programs/storage_api/src/lib.rs b/programs/storage_api/src/lib.rs index 514ddfcd37..c53c105399 100644 --- a/programs/storage_api/src/lib.rs +++ b/programs/storage_api/src/lib.rs @@ -2,7 +2,7 @@ pub mod storage_contract; pub mod storage_instruction; pub mod storage_processor; -pub const SLOTS_PER_SEGMENT: u64 = 2; +pub const SLOTS_PER_SEGMENT: u64 = 16; pub fn get_segment_from_slot(slot: u64) -> usize { (slot / SLOTS_PER_SEGMENT) as usize diff --git a/programs/storage_api/src/storage_contract.rs b/programs/storage_api/src/storage_contract.rs index 83c89829f3..a9591fa951 100644 --- a/programs/storage_api/src/storage_contract.rs +++ b/programs/storage_api/src/storage_contract.rs @@ -81,11 +81,11 @@ impl<'a> StorageAccount<'a> { if let StorageContract::ReplicatorStorage { proofs, .. } = &mut storage_contract { let segment_index = get_segment_from_slot(slot); - if segment_index > proofs.len() || proofs.is_empty() { - proofs.resize(cmp::max(1, segment_index), Proof::default()); + if segment_index >= proofs.len() || proofs.is_empty() { + proofs.resize(cmp::max(1, segment_index + 1), Proof::default()); } - if segment_index > proofs.len() { + if segment_index >= proofs.len() { // only possible if usize max < u64 max return Err(InstructionError::InvalidArgument); } diff --git a/programs/storage_api/src/storage_processor.rs b/programs/storage_api/src/storage_processor.rs index 518b157e2e..d5f8001fab 100644 --- a/programs/storage_api/src/storage_processor.rs +++ b/programs/storage_api/src/storage_processor.rs @@ -108,6 +108,24 @@ mod tests { ret } + #[test] + fn test_proof_bounds() { + let pubkey = Pubkey::new_rand(); + let account = Account { + data: vec![0; 16 * 1024], + ..Account::default() + }; + + let ix = storage_instruction::mining_proof( + &pubkey, + Hash::default(), + SLOTS_PER_SEGMENT, + Signature::default(), + ); + + assert_eq!(test_instruction(&ix, &mut [account]), Ok(())); + } + #[test] fn test_storage_tx() { let pubkey = Pubkey::new_rand();