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
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"dfinity": {
"ref": "master",
"repo": "ssh://git@github.com/dfinity-lab/dfinity",
"rev": "fd64793ba310671093820f7437c5278cb21f8bb6",
"rev": "0b3071a3f7d489d560a16417d07930687a6709d4",
"type": "git"
},
"motoko": {
Expand Down
21 changes: 16 additions & 5 deletions src/dfx/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::future::Future;
use indicatif::{ProgressBar, ProgressDrawTarget};
use std::io::{Error, ErrorKind};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};
use sysinfo::{System, SystemExt};
Expand Down Expand Up @@ -75,8 +75,10 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {

let client_pathbuf = env.get_cache().get_binary_command_path("client")?;
let nodemanager_pathbuf = env.get_cache().get_binary_command_path("nodemanager")?;
let temp_dir = env.get_temp_dir();
let state_root = env.get_temp_dir().join("state");

let pid_file_path = env.get_temp_dir().join("pid");
let pid_file_path = temp_dir.join("pid");
check_previous_process_running(&pid_file_path)?;

// We are doing this here to make sure we can write to the temp pid file.
Expand Down Expand Up @@ -108,6 +110,7 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
&client_pathbuf.clone(),
&nodemanager_pathbuf,
&pid_file_path,
&state_root,
is_killed_client,
request_stop,
)
Expand Down Expand Up @@ -258,16 +261,24 @@ fn start_client(
client_pathbuf: &PathBuf,
nodemanager_pathbuf: &PathBuf,
pid_file_path: &PathBuf,
state_root: &Path,
is_killed_client: Receiver<()>,
request_stop: Sender<()>,
) -> DfxResult<()> {
let client = client_pathbuf.as_path();
let nodemanager = nodemanager_pathbuf.as_path();
let config = format!(
r#"
[state_manager]
state_root = "{state_root}"
"#,
state_root = state_root.display(),
);
let client = client_pathbuf.as_path().as_os_str();
let nodemanager = nodemanager_pathbuf.as_path().as_os_str();
// We use unwrap() here to transmit an error to the parent
// thread.
while is_killed_client.is_empty() {
let mut cmd = std::process::Command::new(nodemanager);
cmd.args(&[client]);
cmd.args(&[client, "--config".as_ref(), config.as_ref()]);
cmd.stdout(std::process::Stdio::inherit());
cmd.stderr(std::process::Stdio::inherit());

Expand Down
22 changes: 14 additions & 8 deletions src/dfx/src/lib/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ mod tests {
fn query_request_serialization() {
use serde_cbor::Value;

let canister_id = CanisterId::from(1);
let canister_id = CanisterId::from_bytes(&[1]);
let method_name = "main".to_string();
let arg = Blob(vec![]);

Expand All @@ -314,7 +314,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 +390,7 @@ mod tests {

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

let query = query(
client,
CanisterId::from(1),
CanisterId::from_bytes(&[1]),
"main".to_string(),
Some(Blob(vec![])),
);
Expand All @@ -478,7 +481,7 @@ mod tests {
fn install_code_request_serialization() {
use serde_cbor::Value;

let canister_id = CanisterId::from(1);
let canister_id = CanisterId::from_bytes(&[1]);
let module = Blob(vec![1]);
let arg = Blob(vec![2]);

Expand All @@ -498,7 +501,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 +529,7 @@ mod tests {
url: mockito::server_url(),
});

let future = install_code(client, CanisterId::from(1), Blob(vec![1]), None);
let future = install_code(client, CanisterId::from_bytes(&[1]), 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 +555,7 @@ mod tests {
url: mockito::server_url(),
});

let future = install_code(client, CanisterId::from(1), Blob(vec![1]), None);
let future = install_code(client, CanisterId::from_bytes(&[1]), 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
17 changes: 6 additions & 11 deletions src/dfx/src/lib/canister_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
use crate::config::dfinity::Config;
use crate::lib::error::{DfxError, DfxResult};
use ic_http_agent::{Blob, CanisterId};
use rand::{thread_rng, Rng};
use rand::{thread_rng, RngCore};
use std::cell::RefCell;
use std::ops::Shl;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};

/// Information about a canister project (source code, destination, etc).
#[derive(Debug)]
Expand Down Expand Up @@ -148,13 +146,10 @@ impl CanisterInfo {
}

pub fn generate_canister_id(&self) -> DfxResult<CanisterId> {
// Generate a random u64.
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)
+ u64::from(thread_rng().gen::<u32>());

Ok(CanisterId::from(cid))
let mut rng = thread_rng();
let mut v: Vec<u8> = std::iter::repeat(0u8).take(8).collect();
rng.fill_bytes(v.as_mut_slice());

Ok(CanisterId::from(Blob(v)))
}
}
1 change: 0 additions & 1 deletion src/ic_http_agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
byteorder = "1.3.2"
crc8 = "0.1.1"
error = "0.1.9"
hex = "0.4.0"
Expand Down
41 changes: 13 additions & 28 deletions src/ic_http_agent/src/types/canister_id.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::types::blob::Blob;
use byteorder::{ByteOrder, LittleEndian};
use crc8::Crc8;
use hex;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{fmt, num, str};
use std::{fmt, str};

/// Prefix for [textual form of ID](https://docs.dfinity.systems/spec/public/#textual-ids)
const IC_COLON: &str = "ic:";
const CANISTER_ID_TEXT_PREFIX: &str = "ic:";

/// A Canister ID.
///
Expand All @@ -31,21 +30,15 @@ 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
}

pub fn from_bytes<S: AsRef<[u8]>>(bytes: S) -> CanisterId {
CanisterId(Blob::from(bytes.as_ref()))
}

/// Parse the text format for canister IDs (e.g., `ic:010840FFAD`).
///
/// The text format follows this
Expand All @@ -57,7 +50,7 @@ impl CanisterId {
let (text_prefix, text_rest) = text.as_ref().split_at(3);
match std::str::from_utf8(text_prefix) {
Ok(ref s) => {
if s != &IC_COLON {
if s != &CANISTER_ID_TEXT_PREFIX {
return Err(TextualCanisterIdError::BadPrefix);
}
}
Expand All @@ -83,7 +76,7 @@ impl CanisterId {
let checksum_byte: u8 = crc8.calc(&(self.0).0, (self.0).0.len() as i32, 0);
let mut buf = (self.0).0.clone();
buf.push(checksum_byte);
format!("{}{}", IC_COLON, hex::encode_upper(buf))
format!("{}{}", CANISTER_ID_TEXT_PREFIX, hex::encode_upper(buf))
}
}

Expand All @@ -93,8 +86,7 @@ impl Serialize for CanisterId {
where
S: Serializer,
{
// TODO(DFN-862): move this to blobs
serializer.serialize_u64(self.as_u64())
self.0.serialize(serializer)
}
}

Expand All @@ -104,7 +96,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 +108,11 @@ impl From<Blob> for CanisterId {
}
}

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

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

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

Expand All @@ -149,7 +134,7 @@ mod tests {

#[test]
fn check_serialize_deserialize() {
let id = CanisterId::from_u64(88827);
let id = CanisterId::from_text("ic:ABCD01A7").unwrap();

// Use cbor serialization.
let vec = serde_cbor::to_vec(&id).unwrap();
Expand Down
11 changes: 4 additions & 7 deletions src/ic_http_agent/src/types/request_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! A single method is exported, to_request_id, which returns a RequestId
//! (a 256 bits slice) or an error.
use crate::types::request_id_error::{RequestIdError, RequestIdFromStringError};
use byteorder::{BigEndian, ByteOrder};
use openssl::sha::Sha256;
use serde::{ser, Serialize, Serializer};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -219,10 +218,8 @@ impl<'a> ser::Serializer for &'a mut RequestIdSerializer {
}

/// Serialize a `u64` value.
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
let mut buf = [0 as u8; 8];
BigEndian::write_u64(&mut buf, v);
self.serialize_bytes(&buf)
fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
Err(RequestIdError::UnsupportedTypeU64)
}

/// Serialize an `f32` value.
Expand Down Expand Up @@ -660,7 +657,7 @@ mod tests {
};
let data = PublicSpecExampleStruct {
request_type: "call",
canister_id: CanisterId::from(1234),
canister_id: CanisterId::from_bytes(&[0, 0, 0, 0, 0, 0, 0x04, 0xD2]), // 1234 in u64
method_name: "hello",
arg: Blob(b"DIDL\x00\xFD*".to_vec()),
};
Expand All @@ -687,7 +684,7 @@ mod tests {
},
}
let data = PublicSpec::Call {
canister_id: CanisterId::from(1234),
canister_id: CanisterId::from_bytes(&[0, 0, 0, 0, 0, 0, 0x04, 0xD2]), // 1234 in u64
method_name: "hello".to_owned(),
arg: Some(Blob(b"DIDL\x00\xFD*".to_vec())),
};
Expand Down