Skip to content
Closed
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: 1 addition & 1 deletion nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dfinity": {
"ref": "master",
"repo": "ssh://git@github.com/dfinity-lab/dfinity",
"rev": "fd64793ba310671093820f7437c5278cb21f8bb6",
"rev": "ddf7981247309be774d40d6a6d25f4bd48e7272a",
"type": "git"
},
"motoko": {
Expand Down
26 changes: 18 additions & 8 deletions src/dfx/src/lib/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![]);

Expand All @@ -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()),
Expand Down Expand Up @@ -387,7 +394,7 @@ mod tests {

let query = query(
client,
CanisterId::from(1),
CanisterId::from(vec![1]),
"main".to_string(),
Some(Blob(vec![])),
);
Expand Down Expand Up @@ -458,7 +465,7 @@ mod tests {

let query = query(
client,
CanisterId::from(1),
CanisterId::from(vec![1]),
"main".to_string(),
Some(Blob(vec![])),
);
Expand All @@ -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]);

Expand All @@ -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),
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions src/dfx/src/lib/canister_info.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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::<u32>());

let cid = cid.to_le_bytes().to_vec();
// TODO(eftychis): Go over the spec and consider the
// requirements.
Ok(CanisterId::from(cid))
}
}
32 changes: 14 additions & 18 deletions src/ic_http_agent/src/types/canister_id.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -31,16 +31,6 @@ impl std::convert::From<hex::FromHexError> 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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
}
}

Expand All @@ -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)?))
}
}

Expand All @@ -116,18 +112,18 @@ impl From<Blob> for CanisterId {
}
}

impl From<u64> for CanisterId {
fn from(n: u64) -> CanisterId {
impl From<Vec<u8>> for CanisterId {
fn from(b: Vec<u8>) -> CanisterId {
// We don't need to make a copy as this assume ownership.
CanisterId::from_u64(n)
CanisterId(Blob(b))
}
}

impl str::FromStr for CanisterId {
type Err = num::ParseIntError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(CanisterId::from_u64(u64::from_str(s)?))
Ok(CanisterId(Blob(s.as_bytes().to_vec())))
}
}

Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/ic_http_agent/src/types/request_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
};
Expand All @@ -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())),
};
Expand Down