我在 Mac OS V10.14.6 上运行并在 nixOS 上运行 v0.0.21-alpha1。所以,我一直在尝试创建一个 create_user_entry zome 函数,它需要 User 结构(带有嵌套的 UserInfo 结构)。我在 rust 中的所有单元测试都通过了,但是由于使用 chrono crate 的功能,西洋镜测试显然失败了。这是代码,
use chrono::{offset::Local, Date, Datelike, TimeZone};
use hdk::holochain_json_api::{error::JsonError, json::JsonString};
// This is the User entry
#[derive(Serialize, Deserialize, Debug, DefaultJson, Clone)]
pub struct User {
pub agent: Address,
pub handle: String,
pub user_info: user_info::UserInfo,
}
//Within this struct is the birth_date key with BirthDate struct value which has the get_age() associated function that uses a chrono crate.
#[derive(Serialize, Deserialize, Debug, DefaultJson, Clone, PartialEq)]
pub struct UserInfo {
pub first_name: String,
pub last_name: String,
pub birth_date: BirthDate,
pub age: u32,
}
//This is the BirthDate struct with its associated function get_age
#[derive(Serialize, Deserialize, Debug, DefaultJson, Clone, PartialEq)]
pub struct BirthDate {
birth_year: i32,
birth_month: u32,
birth_day: u32,
}
impl BirthDate {
//This is causing the Holochain Instance Error: WASM invocation failed: Trap: Trap { kind: Unreachable error
pub fn get_age(birth_year: i32, birth_month: u32, birth_day: u32) -> u32 {
let birthday_dt: Date<Local> =
Local.ymd(birth_year.clone(), birth_month.clone(), birth_day.clone());
let local_dt: Date<Local> = Local::today();
let year_diff: i32 = local_dt.year() - birthday_dt.year();
if birthday_dt.month() < local_dt.month() {
return year_diff as u32;
} else if birthday_dt.month() == local_dt.month() {
if birthday_dt.day() >= local_dt.day() {
return year_diff as u32;
} else {
return year_diff as u32 - 1;
}
} else {
return year_diff as u32 - 1;
}
}
}
}
我预计 Diorama 测试会成功,但我得到了
info: [C] Starting instance "alice-3"...
info: [C] Starting instance "bob-3"...
info: [C] 2019-07-31 20:54:14 ThreadId(1):alice-3: debug/dna: 'called `Option::unwrap()` on a `None` value'
info: [C] 2019-07-31 20:54:14 ThreadId(1):alice-3: debug/dna: 'panic occurred in file 'src/libcore/option.rs' at line 345'
got unhandledRejection: { code: -32602,
message:
'Holochain Instance Error: WASM invocation failed: Trap: Trap { kind: Unreachable }' }
error: Test failed while running: "zome call timed out after 60 seconds: alice-3/main/create_user"
not ok 1 (unnamed assert)
---
operator: fail
at: run.catch (/Users/lc/Desktop/kiretter-test/test/node_modules/@holochain/diorama/lib/executors.js:18:19)...
还尝试用这种方式定义的 get_age_test() 替换 get_age() 。西洋镜通过了,所以计时可能是问题所在......
pub fn get_age_test(birth_year: i32, birth_month: u32, birth_day: u32) -> u32 {
// let birthday_dt: Date<Local> =
// Local.ymd(birth_year.clone(), birth_month.clone(), birth_day.clone());
let birthday_dummy: (i32, u32, u32) = (birth_year, birth_month, birth_day);
let local_dummy: (i32, u32, u32) = (2019, 07, 31);
let year_diff: i32 = local_dummy.0 - birthday_dummy.0;
if birthday_dummy.1 < local_dummy.1 {
return year_diff as u32;
} else if birthday_dummy.1 == local_dummy.1 {
if birthday_dummy.2 >= birthday_dummy.2 {
return year_diff as u32;
} else {
return year_diff as u32 - 1;
}
} else {
return year_diff as u32 - 1;
}
}
另外,这里的 get_age() 函数也在 rust 操场上工作,仅供参考
还尝试使用 json-rpc 和 get_age() 工作。