Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions packages/rs-drive-abci/src/execution/engine.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bls_signatures;
use dashcore::hashes::Hash;
use dashcore::{QuorumHash, Txid};
use dpp::bincode;
use dpp::consensus::basic::identity::IdentityInsufficientBalanceError;
use dpp::consensus::ConsensusError;
use dpp::state_transition::StateTransition;
Expand All @@ -21,6 +22,7 @@ use crate::abci::withdrawal::WithdrawalTxs;
use crate::abci::AbciError;
use crate::block::{BlockExecutionContext, BlockStateInfo};
use crate::error::execution::ExecutionError;
use crate::error::serialization::SerializationError;
use crate::error::Error;
use crate::execution::block_proposal::BlockProposal;
use crate::execution::execution_event::ExecutionResult::{
Expand Down Expand Up @@ -238,6 +240,7 @@ where
let last_block_core_height =
state.known_core_height_or(self.config.abci.genesis_core_height);
let hpmn_list_len = state.hpmn_list_len();
let quorum_hash = state.current_validator_set_quorum_hash;
drop(state);

// Init block execution context
Expand Down Expand Up @@ -345,6 +348,9 @@ where
transaction,
)?;

// Store ephemeral data
self.store_ephemeral_data(&block_info, &quorum_hash, transaction)?;

let root_hash = self
.drive
.grove
Expand All @@ -365,6 +371,52 @@ where
}))
}

// TODO: remove function from here
fn store_ephemeral_data(
&self,
block_info: &BlockInfo,
quorum_hash: &QuorumHash,
transaction: &Transaction,
) -> Result<(), Error> {
// we need to serialize the block info
let mut serialized_block_info = vec![];
bincode::encode_into_slice(
block_info,
serialized_block_info.as_mut_slice(),
bincode::config::standard(),
)
.map_err(|_| {
Error::Serialization(SerializationError::CorruptedSerialization(
"failed to serialize block info".to_string(),
))
})?;

// next we need to store this data in groveb
self.drive
.grove
.put_aux(
b"saved_state",
&serialized_block_info,
None,
Some(transaction),
)
.unwrap()
.map_err(|e| Error::Drive(GroveDB(e)))?;

self.drive
.grove
.put_aux(
b"saved_quorum_hash",
&quorum_hash.into_inner(),
None,
Some(&transaction),
)
.unwrap()
.map_err(|e| Error::Drive(GroveDB(e)))?;

Ok(())
}

/// Update the current quorums if the core_height changes
pub fn update_state_cache_and_quorums(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ where
}
}

/*
#[cfg(test)]
mod tests {
use crate::config::PlatformConfig;
Expand Down Expand Up @@ -733,3 +734,4 @@ mod tests {
#[test]
fn test_update_owner_identity() {}
}
*/
133 changes: 129 additions & 4 deletions packages/rs-drive-abci/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,24 @@ use crate::block::BlockExecutionContext;
use crate::config::PlatformConfig;
use crate::error::execution::ExecutionError;
use crate::error::Error;
use crate::rpc::core::DefaultCoreRPC;
use crate::rpc::core::{CoreRPCLike, DefaultCoreRPC};
use crate::state::PlatformState;
use drive::drive::Drive;

use drive::drive::defaults::PROTOCOL_VERSION;
use std::path::Path;
use std::sync::RwLock;

use crate::error::serialization::SerializationError;
use crate::error::Error::Serialization;
use crate::rpc::core::MockCoreRPCLike;
use dpp::dashcore::hashes::hex::FromHex;
use dpp::dashcore::BlockHash;
use dashcore::hashes::hex::FromHex;
use dashcore::hashes::Hash;
use dashcore::{BlockHash, QuorumHash};
use dpp::bincode;
use drive::drive::block_info::BlockInfo;
use drive::error::drive::DriveError;
use drive::error::Error::GroveDB;
use serde_json::json;

mod state_repository;
Expand Down Expand Up @@ -159,7 +166,10 @@ impl<C> Platform<C> {
path: P,
config: Option<PlatformConfig>,
core_rpc: C,
) -> Result<Platform<C>, Error> {
) -> Result<Platform<C>, Error>
where
C: CoreRPCLike,
{
let config = config.unwrap_or_default();
let drive = Drive::open(path, config.drive.clone()).map_err(Error::Drive)?;

Expand All @@ -172,6 +182,121 @@ impl<C> Platform<C> {
.map_err(Error::Drive)?
.unwrap_or(PROTOCOL_VERSION);

// TODO: factor out key so we don't duplicate
let maybe_serialized_block_info = drive
.grove
.get_aux(b"saved_state", None)
.unwrap()
.map_err(|e| Error::Drive(GroveDB(e)))?;

if let Some(serialized_block_info) = maybe_serialized_block_info {
Platform::open_with_client_saved_state::<P>(
drive,
core_rpc,
config,
serialized_block_info,
current_protocol_version_in_consensus,
next_epoch_protocol_version,
)
} else {
Platform::open_with_client_no_saved_state::<P>(
drive,
core_rpc,
config,
current_protocol_version_in_consensus,
next_epoch_protocol_version,
)
}
}

/// Open Platform with Drive and block execution context from saved state.
pub fn open_with_client_saved_state<P: AsRef<Path>>(
drive: Drive,
core_rpc: C,
config: PlatformConfig,
serialized_block_info: Vec<u8>,
current_protocol_version_in_consensus: u32,
next_epoch_protocol_version: u32,
) -> Result<Platform<C>, Error>
where
C: CoreRPCLike,
{
let block_info: BlockInfo =
bincode::decode_from_slice(&serialized_block_info, bincode::config::standard())
.map_err(|e| {
Serialization(SerializationError::CorruptedDeserialization(
"failed to deserialize saved state".to_string(),
))
})?
.0;

let maybe_quorum_hash = drive
.grove
.get_aux(b"saved_quorum_hash", None)
.unwrap()
.map_err(|e| Error::Drive(GroveDB(e)))?;

// TODO: remove unwrap
let current_validator_set_quorum_hash =
QuorumHash::from_slice(&maybe_quorum_hash.unwrap()).unwrap();

let state = PlatformState {
last_committed_block_info: Some(block_info),
current_protocol_version_in_consensus,
next_epoch_protocol_version,
quorums_extended_info: Default::default(),
current_validator_set_quorum_hash,
validator_sets: Default::default(),
full_masternode_list: Default::default(),
hpmn_masternode_list: Default::default(),
};

let core_height = state.core_height();
let block_info = state
.last_committed_block_info
.clone()
.unwrap_or(BlockInfo::genesis());

let platform: Platform<C> = Platform {
drive,
state: RwLock::new(state),
config,
block_execution_context: RwLock::new(None),
core_rpc,
};

let transaction = platform.drive.grove.start_transaction();
let mut state_cache = platform.state.write().unwrap();
platform.update_quorum_info(&mut state_cache, core_height)?;
platform.update_masternode_list(
&mut state_cache,
core_height,
&block_info,
&transaction,
)?;
drop(state_cache);

platform
.drive
.grove
.commit_transaction(transaction)
.unwrap()
.map_err(|e| Error::Drive(GroveDB(e)))?;

return Ok(platform);
}

/// Open Platform with Drive and block execution context without saved state.
pub fn open_with_client_no_saved_state<P: AsRef<Path>>(
drive: Drive,
core_rpc: C,
config: PlatformConfig,
current_protocol_version_in_consensus: u32,
next_epoch_protocol_version: u32,
) -> Result<Platform<C>, Error>
where
C: CoreRPCLike,
{
let state = PlatformState {
last_committed_block_info: None,
current_protocol_version_in_consensus,
Expand Down
3 changes: 2 additions & 1 deletion packages/rs-drive-abci/src/state/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ mod tests {
assert_eq!(
root_hash,
[
223, 137, 33, 5, 253, 177, 248, 37, 0, 40, 198, 213, 196, 196, 66, 200, 71, 85, 103, 138, 52, 63, 102, 105, 27, 86, 102, 242, 79, 247, 217, 108
223, 137, 33, 5, 253, 177, 248, 37, 0, 40, 198, 213, 196, 196, 66, 200, 71, 85,
103, 138, 52, 63, 102, 105, 27, 86, 102, 242, 79, 247, 217, 108
]
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/rs-drive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ thiserror = { version = "1.0.30" }
moka = { version = "0.10.1", features = ["future", "futures-util"]}
nohash-hasher = { version = "0.2.0" }
dpp = { path = "../rs-dpp", features = ["fixtures-and-mocks"] }
bincode = { version="2.0.0-rc.3", features=["serde"] }

# optional dependencies
bs58 = { version = "0.4.0", optional = true }
Expand Down
7 changes: 6 additions & 1 deletion packages/rs-drive/src/drive/block_info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use dpp::dashcore::QuorumHash;
use crate::fee_pools::epochs::Epoch;
use dpp::bincode::{Encode, Decode};

/// Block information
#[derive(Clone, Default)]
#[derive(Clone, Default, Encode, Decode)]
pub struct BlockInfo {
/// Block time in milliseconds
pub time_ms: u64,
Expand All @@ -14,6 +16,9 @@ pub struct BlockInfo {

/// Current fee epoch
pub epoch: Epoch,

// /// current quorum
// pub current_validator_set_quorum_hash: QuorumHash,
}

impl BlockInfo {
Expand Down
Loading