diff --git a/nix/sources.json b/nix/sources.json index b96f54a85a..745ec81378 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -20,7 +20,7 @@ "dfinity": { "ref": "master", "repo": "ssh://git@github.com/dfinity-lab/dfinity", - "rev": "fd64793ba310671093820f7437c5278cb21f8bb6", + "rev": "ddf7981247309be774d40d6a6d25f4bd48e7272a", "type": "git" }, "motoko": { diff --git a/src/dfx/src/lib/api_client.rs b/src/dfx/src/lib/api_client.rs index a0bc446698..2b889e6656 100644 --- a/src/dfx/src/lib/api_client.rs +++ b/src/dfx/src/lib/api_client.rs @@ -289,11 +289,15 @@ mod tests { use mockito; use mockito::mock; + fn canister_test_default_id() -> CanisterId { + CanisterId::from(vec![1]) + } + #[test] fn query_request_serialization() { use serde_cbor::Value; - let canister_id = CanisterId::from(1); + let canister_id = canister_test_default_id(); let method_name = "main".to_string(); let arg = Blob(vec![]); @@ -314,7 +318,10 @@ mod tests { Value::Text("query".to_string()), ), // TODO: when the client moves to using Blobs, move this to being a blob. - (Value::Text("canister_id".to_string()), Value::Integer(1)), + ( + Value::Text("canister_id".to_string()), + Value::Bytes(vec![1]), + ), ( Value::Text("method_name".to_string()), Value::Text(method_name.clone()), @@ -387,7 +394,7 @@ mod tests { let query = query( client, - CanisterId::from(1), + CanisterId::from(vec![1]), "main".to_string(), Some(Blob(vec![])), ); @@ -458,7 +465,7 @@ mod tests { let query = query( client, - CanisterId::from(1), + CanisterId::from(vec![1]), "main".to_string(), Some(Blob(vec![])), ); @@ -478,7 +485,7 @@ mod tests { fn install_code_request_serialization() { use serde_cbor::Value; - let canister_id = CanisterId::from(1); + let canister_id = CanisterId::from(vec![1]); let module = Blob(vec![1]); let arg = Blob(vec![2]); @@ -498,7 +505,10 @@ mod tests { Value::Text("install_code".to_string()), ), // TODO: when the client moves to using Blobs, move this to being a blob. - (Value::Text("canister_id".to_string()), Value::Integer(1)), + ( + Value::Text("canister_id".to_string()), + Value::Bytes(vec![1]), + ), (Value::Text("module".to_string()), Value::Bytes(vec![1])), (Value::Text("arg".to_string()), Value::Bytes(vec![2])), (Value::Text("nonce".to_string()), Value::Null), @@ -523,7 +533,7 @@ mod tests { url: mockito::server_url(), }); - let future = install_code(client, CanisterId::from(1), Blob(vec![1]), None); + let future = install_code(client, canister_test_default_id(), Blob(vec![1]), None); let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime"); let result = runtime.block_on(future); @@ -549,7 +559,7 @@ mod tests { url: mockito::server_url(), }); - let future = install_code(client, CanisterId::from(1), Blob(vec![1]), None); + let future = install_code(client, canister_test_default_id(), Blob(vec![1]), None); let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime"); let result = runtime.block_on(future); diff --git a/src/dfx/src/lib/canister_info.rs b/src/dfx/src/lib/canister_info.rs index fca0691117..39ec8d8da9 100644 --- a/src/dfx/src/lib/canister_info.rs +++ b/src/dfx/src/lib/canister_info.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] use crate::config::dfinity::Config; use crate::lib::error::{DfxError, DfxResult}; -use ic_http_agent::{Blob, CanisterId}; +use ic_http_agent::CanisterId; use rand::{thread_rng, Rng}; use std::cell::RefCell; use std::ops::Shl; @@ -120,7 +120,7 @@ impl CanisterInfo { let canister_id = self.canister_id.replace(None).or_else(|| { std::fs::read(&self.canister_id_path) .ok() - .map(|cid| CanisterId::from(Blob::from(cid))) + .map(|cid| CanisterId::from(cid)) }); self.canister_id.replace(canister_id.clone()); @@ -137,9 +137,11 @@ impl CanisterInfo { let time_since_the_epoch = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Time went backwards."); - let cid = u64::from(time_since_the_epoch.as_millis() as u32).shl(32) + let cid: u64 = u64::from(time_since_the_epoch.as_millis() as u32).shl(32) + u64::from(thread_rng().gen::()); - + let cid = cid.to_le_bytes().to_vec(); + // TODO(eftychis): Go over the spec and consider the + // requirements. Ok(CanisterId::from(cid)) } } diff --git a/src/ic_http_agent/src/types/canister_id.rs b/src/ic_http_agent/src/types/canister_id.rs index ab2fbba7df..bb332e237f 100644 --- a/src/ic_http_agent/src/types/canister_id.rs +++ b/src/ic_http_agent/src/types/canister_id.rs @@ -1,8 +1,8 @@ use crate::types::blob::Blob; -use byteorder::{ByteOrder, LittleEndian}; use crc8::Crc8; use hex; use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use std::str::FromStr; use std::{fmt, num, str}; /// Prefix for [textual form of ID](https://docs.dfinity.systems/spec/public/#textual-ids) @@ -31,16 +31,6 @@ impl std::convert::From for TextualCanisterIdError { } impl CanisterId { - pub(crate) fn from_u64(v: u64) -> CanisterId { - let mut buf = [0 as u8; 8]; - LittleEndian::write_u64(&mut buf, v); - CanisterId(Blob(buf.to_vec())) - } - - pub(crate) fn as_u64(&self) -> u64 { - LittleEndian::read_u64((self.0).0.as_slice()) - } - /// Allow to move canister Ids in blobs. pub fn into_blob(self) -> Blob { self.0 @@ -85,6 +75,11 @@ impl CanisterId { buf.push(checksum_byte); format!("{}{}", IC_COLON, hex::encode_upper(buf)) } + + #[cfg(test)] + pub fn test_default_values() -> CanisterId { + CanisterId::from(vec![1, 2, 3, 4]) + } } /// Serialize into a blob. @@ -94,7 +89,8 @@ impl Serialize for CanisterId { S: Serializer, { // TODO(DFN-862): move this to blobs - serializer.serialize_u64(self.as_u64()) + let v = self.clone(); + v.into_blob().serialize(serializer) } } @@ -104,7 +100,7 @@ impl<'de> Deserialize<'de> for CanisterId { S: Deserializer<'de>, { // TODO(DFN-862): move this to blobs - Ok(CanisterId::from_u64(u64::deserialize(deserializer)?)) + Ok(CanisterId::from(Blob::deserialize(deserializer)?)) } } @@ -116,10 +112,10 @@ impl From for CanisterId { } } -impl From for CanisterId { - fn from(n: u64) -> CanisterId { +impl From> for CanisterId { + fn from(b: Vec) -> CanisterId { // We don't need to make a copy as this assume ownership. - CanisterId::from_u64(n) + CanisterId(Blob(b)) } } @@ -127,7 +123,7 @@ impl str::FromStr for CanisterId { type Err = num::ParseIntError; fn from_str(s: &str) -> Result { - Ok(CanisterId::from_u64(u64::from_str(s)?)) + Ok(CanisterId(Blob(s.as_bytes().to_vec()))) } } @@ -149,7 +145,7 @@ mod tests { #[test] fn check_serialize_deserialize() { - let id = CanisterId::from_u64(88827); + let id = CanisterId::from_str("88827").unwrap(); // Use cbor serialization. let vec = serde_cbor::to_vec(&id).unwrap(); diff --git a/src/ic_http_agent/src/types/request_id.rs b/src/ic_http_agent/src/types/request_id.rs index e485b964b2..f5c7d94e08 100644 --- a/src/ic_http_agent/src/types/request_id.rs +++ b/src/ic_http_agent/src/types/request_id.rs @@ -660,7 +660,7 @@ mod tests { }; let data = PublicSpecExampleStruct { request_type: "call", - canister_id: CanisterId::from(1234), + canister_id: CanisterId::from(vec![1]), method_name: "hello", arg: Blob(b"DIDL\x00\xFD*".to_vec()), }; @@ -687,7 +687,7 @@ mod tests { }, } let data = PublicSpec::Call { - canister_id: CanisterId::from(1234), + canister_id: CanisterId::test_default_values(), method_name: "hello".to_owned(), arg: Some(Blob(b"DIDL\x00\xFD*".to_vec())), };