Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up left over Bytecode code + some related testing fixes #2607

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions linera-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,4 @@ pub enum ChainExecutionContext {
IncomingBundle(u32),
Operation(u32),
Block,
#[cfg(with_testing)]
ReadBytecodeLocation,
}
22 changes: 12 additions & 10 deletions linera-chain/src/unit_tests/chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,23 @@ where
}
}

fn make_app_description() -> UserApplicationDescription {
fn make_app_description() -> (UserApplicationDescription, Blob, Blob) {
let contract = Bytecode::new(b"contract".into());
let service = Bytecode::new(b"service".into());
let contract_blob = Blob::new_contract_bytecode(contract.compress());
let service_blob = Blob::new_service_bytecode(service.compress());

let bytecode_id = BytecodeId::new(contract_blob.id().hash, service_blob.id().hash);
UserApplicationDescription {
bytecode_id,
creation: make_admin_message_id(BlockHeight(2)),
required_application_ids: vec![],
parameters: vec![],
}
(
UserApplicationDescription {
bytecode_id,
creation: make_admin_message_id(BlockHeight(2)),
required_application_ids: vec![],
parameters: vec![],
},
contract_blob,
service_blob,
)
}

fn admin_id() -> ChainId {
Expand Down Expand Up @@ -182,16 +186,14 @@ async fn test_application_permissions() {
let mut chain = ChainStateView::new(chain_id).await;

// Create a mock application.
let app_description = make_app_description();
let (app_description, contract_blob, service_blob) = make_app_description();
let application_id = ApplicationId::from(&app_description);
let application = MockApplication::default();
let extra = &chain.context().extra();
extra
.user_contracts()
.insert(application_id, application.clone().into());
let contract_blob = Blob::new_contract_bytecode(Bytecode::new(b"contract".into()).compress());
extra.add_blob(contract_blob);
let service_blob = Blob::new_service_bytecode(Bytecode::new(b"service".into()).compress());
extra.add_blob(service_blob);

// Initialize the chain, with a chain application.
Expand Down
4 changes: 3 additions & 1 deletion linera-core/src/unit_tests/wasm_worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ where
.await;

let info = worker
.fully_handle_certificate(publish_certificate.clone(), vec![service_blob])
.fully_handle_certificate(publish_certificate.clone(), vec![service_blob.clone()])
.await
.unwrap()
.info;
Expand Down Expand Up @@ -215,6 +215,8 @@ where
Timestamp::from(2),
application_description,
initial_value_bytes.clone(),
contract_blob,
service_blob,
)
.await?;
let create_block_proposal = HashedCertificateValue::new_confirmed(
Expand Down
4 changes: 2 additions & 2 deletions linera-core/src/unit_tests/worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3601,7 +3601,7 @@ where
chain.save().await?;
}

let (application_id, application) = applications
let (application_id, application, _, _) = applications
.next()
.expect("Mock application should be registered");

Expand Down Expand Up @@ -3691,7 +3691,7 @@ where
chain.save().await?;
}

let (application_id, application) = applications
let (application_id, application, _, _) = applications
.next()
.expect("Mock application should be registered");

Expand Down
13 changes: 13 additions & 0 deletions linera-execution/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use {
crate::{
ResourceControlPolicy, ResourceTracker, TestExecutionRuntimeContext, UserContractCode,
},
linera_base::data_types::Blob,
linera_views::context::MemoryContext,
std::sync::Arc,
};
Expand Down Expand Up @@ -65,6 +66,8 @@ where
local_time: Timestamp,
application_description: UserApplicationDescription,
instantiation_argument: Vec<u8>,
contract_blob: Blob,
service_blob: Blob,
) -> Result<(), ExecutionError> {
let chain_id = application_description.creation.chain_id;
let context = OperationContext {
Expand All @@ -89,6 +92,16 @@ where
.user_contracts()
.insert(application_id, contract);

self.context()
.extra()
.blobs()
.insert(contract_blob.id(), contract_blob);

self.context()
.extra()
.blobs()
.insert(service_blob.id(), service_blob);

let tracker = ResourceTracker::default();
let policy = ResourceControlPolicy::default();
let mut resource_controller = ResourceController {
Expand Down
10 changes: 8 additions & 2 deletions linera-execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ pub trait ExecutionRuntimeContext {

fn user_services(&self) -> &Arc<DashMap<UserApplicationId, UserServiceCode>>;

fn blobs(&self) -> &Arc<DashMap<BlobId, Blob>>;

async fn get_user_contract(
&self,
description: &UserApplicationDescription,
Expand Down Expand Up @@ -928,6 +930,10 @@ impl ExecutionRuntimeContext for TestExecutionRuntimeContext {
&self.user_services
}

fn blobs(&self) -> &Arc<DashMap<BlobId, Blob>> {
&self.blobs
}

async fn get_user_contract(
&self,
description: &UserApplicationDescription,
Expand Down Expand Up @@ -958,14 +964,14 @@ impl ExecutionRuntimeContext for TestExecutionRuntimeContext {

async fn get_blob(&self, blob_id: BlobId) -> Result<Blob, ExecutionError> {
Ok(self
.blobs
.blobs()
.get(&blob_id)
.ok_or_else(|| SystemExecutionError::BlobNotFoundOnRead(blob_id))?
.clone())
}

async fn contains_blob(&self, blob_id: BlobId) -> Result<bool, ViewError> {
Ok(self.blobs.contains_key(&blob_id))
Ok(self.blobs().contains_key(&blob_id))
}
}

Expand Down
7 changes: 0 additions & 7 deletions linera-execution/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ pub static OPEN_CHAIN_MESSAGE_INDEX: u32 = 0;
/// The relative index of the `ApplicationCreated` message created by the `CreateApplication`
/// operation.
pub static CREATE_APPLICATION_MESSAGE_INDEX: u32 = 0;
/// The relative index of the `BytecodePublished` message created by the `PublishBytecode`
/// operation.
pub static PUBLISH_BYTECODE_MESSAGE_INDEX: u32 = 0;

/// The number of times the [`SystemOperation::OpenChain`] was executed.
#[cfg(with_metrics)]
Expand Down Expand Up @@ -341,8 +338,6 @@ pub enum SystemExecutionError {
#[error(transparent)]
ViewError(#[from] ViewError),

#[error("Incorrect chain ID: {0}")]
IncorrectChainId(ChainId),
#[error("Invalid admin ID in new chain: {0}")]
InvalidNewChainAdminId(ChainId),
#[error("Invalid committees")]
Expand Down Expand Up @@ -389,8 +384,6 @@ pub enum SystemExecutionError {
CannotRewindEpoch,
#[error("Cannot decrease the chain's timestamp")]
TicksOutOfOrder,
#[error("Attempt to create an application using unregistered bytecode identifier {0:?}")]
UnknownBytecodeId(BytecodeId),
#[error("Application {0:?} is not registered by the chain")]
UnknownApplicationId(Box<UserApplicationId>),
#[error("Chain is not active yet.")]
Expand Down
61 changes: 41 additions & 20 deletions linera-execution/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::{sync::Arc, thread, vec};

use linera_base::{
crypto::{BcsSignable, CryptoHash},
data_types::BlockHeight,
identifiers::{BlobId, BytecodeId, ChainId, MessageId},
data_types::{Blob, BlockHeight, CompressedBytecode, OracleResponse},
identifiers::{ApplicationId, BlobId, BlobType, BytecodeId, ChainId, MessageId},
};
use linera_views::{
context::Context,
Expand All @@ -31,20 +31,31 @@ use crate::{
TestExecutionRuntimeContext, UserApplicationDescription, UserApplicationId,
};

pub fn create_dummy_user_application_description(index: u64) -> UserApplicationDescription {
pub fn create_dummy_user_application_description(
index: u64,
) -> (UserApplicationDescription, Blob, Blob) {
let chain_id = ChainId::root(1);
let contract_blob_hash = CryptoHash::new(&FakeBlob(String::from("contract")));
let service_blob_hash = CryptoHash::new(&FakeBlob(String::from("service")));
UserApplicationDescription {
bytecode_id: BytecodeId::new(contract_blob_hash, service_blob_hash),
creation: MessageId {
chain_id,
height: BlockHeight(index),
index: 1,
let contract_blob = Blob::new_contract_bytecode(CompressedBytecode {
compressed_bytes: String::from("contract").as_bytes().to_vec(),
});
let service_blob = Blob::new_service_bytecode(CompressedBytecode {
compressed_bytes: String::from("service").as_bytes().to_vec(),
});

(
UserApplicationDescription {
bytecode_id: BytecodeId::new(contract_blob.id().hash, service_blob.id().hash),
creation: MessageId {
chain_id,
height: BlockHeight(index),
index: 1,
},
required_application_ids: vec![],
parameters: vec![],
},
required_application_ids: vec![],
parameters: vec![],
}
contract_blob,
service_blob,
)
}

#[derive(Deserialize, Serialize)]
Expand All @@ -59,7 +70,7 @@ impl BcsSignable for FakeBlob {}
pub async fn register_mock_applications<C>(
state: &mut ExecutionStateView<C>,
count: u64,
) -> anyhow::Result<vec::IntoIter<(UserApplicationId, MockApplication)>>
) -> anyhow::Result<vec::IntoIter<(UserApplicationId, MockApplication, Blob, Blob)>>
where
C: Context + Clone + Send + Sync + 'static,
C::Extra: ExecutionRuntimeContext,
Expand All @@ -68,17 +79,26 @@ where
create_dummy_user_application_registrations(&mut state.system.registry, count)
.await?
.into_iter()
.map(|(id, _description)| (id, MockApplication::default()))
.map(|(id, _description, contract_blob, service_blob)| {
(id, MockApplication::default(), contract_blob, service_blob)
})
.collect();
let extra = state.context().extra();

for (id, mock_application) in &mock_applications {
for (id, mock_application, contract_blob, service_blob) in &mock_applications {
extra
.user_contracts()
.insert(*id, mock_application.clone().into());
extra
.user_services()
.insert(*id, mock_application.clone().into());

extra
.blobs()
.insert(contract_blob.id(), contract_blob.clone());
extra
.blobs()
.insert(service_blob.id(), service_blob.clone());
}

Ok(mock_applications.into_iter())
Expand All @@ -87,19 +107,20 @@ where
pub async fn create_dummy_user_application_registrations<C>(
registry: &mut ApplicationRegistryView<C>,
count: u64,
) -> anyhow::Result<Vec<(UserApplicationId, UserApplicationDescription)>>
) -> anyhow::Result<Vec<(UserApplicationId, UserApplicationDescription, Blob, Blob)>>
where
C: Context + Clone + Send + Sync + 'static,
{
let mut ids = Vec::with_capacity(count as usize);

for index in 0..count {
let description = create_dummy_user_application_description(index);
let (description, contract_blob, service_blob) =
create_dummy_user_application_description(index);
let id = registry.register_application(description.clone()).await?;

assert_eq!(registry.describe_application(id).await?, description);

ids.push((id, description));
ids.push((id, description, contract_blob, service_blob));
}

Ok(ids)
Expand Down
2 changes: 1 addition & 1 deletion linera-execution/tests/fee_consumption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async fn test_fee_consumption(
}

let mut applications = register_mock_applications(&mut view, 1).await.unwrap();
let (application_id, application) = applications
let (application_id, application, _contract_blob, _service_blob) = applications
.next()
.expect("Caller mock application should be registered");

Expand Down
Loading
Loading