Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Init replaces genesis #1508

Merged
merged 38 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ada0b11
replace all case sensitive
willemolding May 21, 2019
cadd4da
rename callback/genesis to callback/init
willemolding May 21, 2019
6cc6238
update changelog
willemolding May 21, 2019
fa24887
renames in documentation
zippy May 21, 2019
29edb1d
file rename in mdbook
zippy May 21, 2019
f793e84
update changelog for breaking change. Merges latest develop
willemolding May 22, 2019
793e779
add failing tests to wasm-test for required functionality of init
willemolding May 22, 2019
f4b1f1f
splits call_init into its own action which is now called after networ…
willemolding May 22, 2019
495e7b9
updates changelog
willemolding May 22, 2019
36414fa
adds tests to init in app-spec
willemolding May 22, 2019
a39a645
rustfmt
lucksus May 22, 2019
81730d4
merge develop into rename-genesis-to-int
zippy May 22, 2019
6e04f53
merge rename-genesis-to-init into init-has-required-functionality
zippy May 22, 2019
3f3faf6
Merge branch 'develop' into init-has-required-functionality
zippy May 23, 2019
290b141
Merge branch 'develop' into rename-genesis-to-init
zippy May 23, 2019
2ed5a49
Merge branch 'develop' into rename-genesis-to-init
willemolding May 26, 2019
77249df
merge develop
willemolding May 27, 2019
3ab47bb
merge develop and fix conflicts
willemolding Jun 11, 2019
9386df9
merge add functionality to init and fix conflict
willemolding Jun 11, 2019
04eea4a
round up missed conflict marker
willemolding Jun 11, 2019
21f5363
Merge branch 'develop' into init-replaces-genesis
zippy Jun 13, 2019
4f80234
Merge branch 'develop' into init-replaces-genesis
willemolding Jul 1, 2019
2569e7d
updates changelog
willemolding Jul 1, 2019
191eb46
update changelog again
willemolding Jul 1, 2019
70127e2
Merge branch 'develop' into init-replaces-genesis
willemolding Jul 3, 2019
f6a97af
Merge branch 'develop' of https://github.com/holochain/holochain-rust…
willemolding Jul 17, 2019
217844a
fix broken test import
willemolding Jul 17, 2019
947fd77
Merge branch 'init-replaces-genesis' of https://github.com/holochain/…
willemolding Jul 17, 2019
d6c3e24
fix simple app spec test
willemolding Jul 17, 2019
fd633c6
update genesis to init in proc macro simple zome
willemolding Jul 17, 2019
7d7f465
update proc macros to support init
willemolding Jul 17, 2019
6fed136
update proc macro tests
willemolding Jul 18, 2019
66128fc
Merge branch 'develop' into init-replaces-genesis
willemolding Jul 18, 2019
caabf0f
Merge branch 'develop' into init-replaces-genesis
lucksus Jul 23, 2019
61c67bd
Update CHANGELOG-UNRELEASED.md
willemolding Jul 23, 2019
d66dded
Merge branch 'develop' into init-replaces-genesis
lucksus Jul 23, 2019
798fad6
Merge branch 'develop' into init-replaces-genesis
willemolding Jul 24, 2019
1fcc13c
Merge branch 'develop' into init-replaces-genesis
lucksus Jul 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
{{ version-heading }}

### Added

### Changed
- **Breaking Change** genesis function now renamed to init [#1508](https://github.com/holochain/holochain-rust/pull/1508)
- **BREAKING:** Zomes must now include a `validate_agent` callback. If this rejects in any zome the DNA will not start. This can be used to enforce membrane requirements. [#1497](https://github.com/holochain/holochain-rust/pull/1497)
- Added a `get_links_count` method which allows the user to get number of links by base and tag [#1568](https://github.com/holochain/holochain-rust/pull/1568)### Changed
- The Conductor will shut down gracefully when receiving SIGINT (i.e. Ctrl+C) or SIGKILL, also causing a graceful shutdown of an attached n3h instance, if running [#1599](https://github.com/holochain/holochain-rust/pull/1599)
Expand Down
4 changes: 2 additions & 2 deletions app_spec/zomes/blog/code/src/blog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::{
};

#[derive(Serialize, Deserialize, Debug, DefaultJson, PartialEq)]
struct SumInput {
pub struct SumInput {
num1: u32,
num2: u32,
}
Expand Down Expand Up @@ -89,7 +89,7 @@ pub fn handle_get_sources(address: Address) -> ZomeApiResult<Vec<Address>> {
}
}

fn check_sum_args(num1: u32, num2: u32) -> SumInput {
pub fn check_sum_args(num1: u32, num2: u32) -> SumInput {
SumInput {
num1: num1,
num2: num2,
Expand Down
38 changes: 36 additions & 2 deletions app_spec/zomes/blog/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod post;

use blog::Env;
use hdk::{
AGENT_ADDRESS, DNA_ADDRESS, PUBLIC_TOKEN,
error::ZomeApiResult,
holochain_persistence_api::{
cas::content::Address
Expand All @@ -40,9 +41,42 @@ define_zome! {
memo::definition()
]

genesis: || {
init: || {{
// should be able to commit an entry
let entry = Entry::App(
"post".into(),
post::Post {
content: "called from init".into(),
date_created: "1234".into(),
}
.into(),
);
let addr = hdk::commit_entry(&entry)?;

// should be able to get the entry
let get_result = hdk::get_entry(&addr)?.unwrap();
if !(entry == get_result) {
return Err("Could not retrieve the same entry in init".into());
}

// should be able to access globals
let agent_addr: Address = AGENT_ADDRESS.to_string().into();
let _dna_hash: Address = DNA_ADDRESS.to_string().into();

// should be able to call hdk::send, will timeout immedietly but that is ok
let _send_result = hdk::send(agent_addr, "".to_string(), 10000.into())?;

// should be able to call other zome funcs
hdk::call(
hdk::THIS_INSTANCE,
"summer",
Address::from(PUBLIC_TOKEN.to_string()),
"sum",
blog::check_sum_args(1, 2).into(),
)?;

Ok(())
}
}}

validate_agent: |validation_data : EntryValidationData::<AgentId>| {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion app_spec/zomes/converse/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn handle_list_secrets() -> ZomeApiResult<Vec<String>> {
define_zome! {
entries: []

genesis: || {
init: || {
{
hdk::keystore_new_random("app_root_seed", 32)
.map_err(|err|
Expand Down
2 changes: 1 addition & 1 deletion app_spec/zomes/simple/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ define_zome! {
definition()
]

genesis: || {
init: || {
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion app_spec/zomes/summer/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn handle_sum(num1: u32, num2: u32) -> ZomeApiResult<u32> {
define_zome! {
entries: []

genesis: || {
init: || {
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions app_spec_proc_macro/zomes/blog/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub mod blog {
memo::definition()
}

#[genesis]
pub fn genesis() {
#[init]
pub fn init() {
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions app_spec_proc_macro/zomes/converse/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use hdk::{
#[zome]
pub mod converse {

#[genesis]
pub fn genesis() {
#[init]
pub fn init() {
hdk::keystore_new_random("app_root_seed", 32)
.map_err(|err|
hdk::debug(format!("ignoring new seed generation because of error: {}",err))
Expand Down
4 changes: 2 additions & 2 deletions app_spec_proc_macro/zomes/simple/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ pub mod simple {
}


#[genesis]
pub fn genesis() {
#[init]
pub fn init() {
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions app_spec_proc_macro/zomes/summer/code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use hdk_proc_macros::zome;

#[zome]
pub mod summer {
#[genesis]
fn genesis() {
#[init]
fn init() {
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli/scaffold/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ define_zome! {
definition()
]

genesis: || { Ok(()) }
init: || { Ok(()) }

functions: [
create_my_entry: {
Expand Down
15 changes: 9 additions & 6 deletions conductor_api/src/holochain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,14 @@ mod tests {
}

#[test]
fn fails_instantiate_if_genesis_fails() {
fn fails_instantiate_if_init_fails() {
let dna = create_test_dna_with_wat(
"test_zome",
Some(
r#"
(module
(memory (;0;) 1)
(func (export "genesis") (param $p0 i64) (result i64)
(func (export "init") (param $p0 i64) (result i64)
i64.const 9
)
(data (i32.const 0)
Expand All @@ -414,22 +414,25 @@ mod tests {
let result = Holochain::new(dna.clone(), context.clone());
assert!(result.is_err());
assert_eq!(
HolochainInstanceError::from(HolochainError::ErrorGeneric("\"Genesis\"".to_string())),
HolochainInstanceError::from(HolochainError::ErrorGeneric(
"At least one zome init returned error: [(\"test_zome\", \"\\\"Init\\\"\")]"
.to_string()
)),
result.err().unwrap(),
);
}

#[test]
#[cfg(feature = "broken-tests")]
fn fails_instantiate_if_genesis_times_out() {
fn fails_instantiate_if_init_times_out() {
let dna = create_test_dna_with_wat(
"test_zome",
Callback::Genesis.capability().as_str(),
Callback::Init.capability().as_str(),
Some(
r#"
(module
(memory (;0;) 1)
(func (export "genesis") (param $p0 i64) (result i64)
(func (export "init") (param $p0 i64) (result i64)
(loop (br 0))
i64.const 0
)
Expand Down
2 changes: 1 addition & 1 deletion conductor_api/test-bridge-caller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn handle_call_bridge_error() -> JsonString {
define_zome! {
entries: []

genesis: || {
init: || {
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ pub enum Action {
// Nucleus actions:
// ----------------
/// initialize a chain from Dna
/// not the same as genesis
/// may call genesis internally
/// not the same as init
/// may call init internally
InitializeChain(Dna),
/// return the result of an InitializeChain action
/// the result is an initialization structure which include the generated public token if any
Expand Down
4 changes: 2 additions & 2 deletions core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ impl Context {
}

pub fn get_dna(&self) -> Option<Dna> {
// In the case of genesis we encounter race conditions with regards to setting the DNA.
// Genesis gets called asynchronously right after dispatching an action that sets the DNA in
// In the case of init we encounter race conditions with regards to setting the DNA.
// Init gets called asynchronously right after dispatching an action that sets the DNA in
// the state, which can result in this code being executed first.
// But we can't run anything if there is no DNA which holds the WASM, so we have to wait here.
// TODO: use a future here
Expand Down
27 changes: 14 additions & 13 deletions core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ pub mod tests {
dna::{zome::Zome, Dna},
entry::{entry_type::EntryType, test_entry},
};
use holochain_json_api::json::{JsonString, RawString};
use holochain_persistence_api::cas::content::AddressableContent;
use holochain_persistence_file::{cas::file::FilesystemStorage, eav::file::EavFileStorage};
use tempfile;
Expand Down Expand Up @@ -570,7 +569,7 @@ pub mod tests {
// fair warning... use test_instance_blank() if you want a minimal instance
assert!(
!dna.zomes.clone().is_empty(),
"Empty zomes = No genesis = infinite loops below!"
"Empty zomes = No init = infinite loops below!"
);

// @TODO abstract and DRY this out
Expand Down Expand Up @@ -606,7 +605,7 @@ pub mod tests {
})
.is_none()
{
println!("Waiting for Commit for genesis");
println!("Waiting for Commit for init");
sleep(Duration::from_millis(10))
}

Expand Down Expand Up @@ -715,10 +714,10 @@ pub mod tests {
}

#[test]
/// tests that an unimplemented genesis allows the nucleus to initialize
/// tests that an unimplemented init allows the nucleus to initialize
/// @TODO is this right? should return unimplemented?
/// @see https://github.com/holochain/holochain-rust/issues/97
fn test_missing_genesis() {
fn test_missing_init() {
let dna = test_utils::create_test_dna_with_wat("test_zome", None);

let instance = test_instance(dna, None);
Expand All @@ -729,15 +728,15 @@ pub mod tests {
}

#[test]
/// tests that a valid genesis allows the nucleus to initialize
fn test_genesis_ok() {
/// tests that a valid init allows the nucleus to initialize
fn test_init_ok() {
let dna = test_utils::create_test_dna_with_wat(
"test_zome",
Some(
r#"
(module
(memory (;0;) 1)
(func (export "genesis") (param $p0 i64) (result i64)
(func (export "init") (param $p0 i64) (result i64)
i64.const 0
)
(data (i32.const 0)
Expand All @@ -749,23 +748,23 @@ pub mod tests {
),
);

let maybe_instance = test_instance(dna, Some("test_genesis_ok"));
let maybe_instance = test_instance(dna, Some("test_init_ok"));
assert!(maybe_instance.is_ok());

let instance = maybe_instance.unwrap();
assert!(instance.state().nucleus().has_initialized());
}

#[test]
/// tests that a failed genesis prevents the nucleus from initializing
fn test_genesis_err() {
/// tests that a failed init prevents the nucleus from initializing
fn test_init_err() {
let dna = test_utils::create_test_dna_with_wat(
"test_zome",
Some(
r#"
(module
(memory (;0;) 1)
(func (export "genesis") (param $p0 i64) (result i64)
(func (export "init") (param $p0 i64) (result i64)
i64.const 9
)
(data (i32.const 0)
Expand All @@ -781,7 +780,9 @@ pub mod tests {
assert!(instance.is_err());
assert_eq!(
instance.err().unwrap(),
String::from(JsonString::from(RawString::from("Genesis")))
String::from(
"At least one zome init returned error: [(\"test_zome\", \"\\\"Init\\\"\")]"
)
);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/network/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn test_instance_with_spoofed_dna(
// fair warning... use test_instance_blank() if you want a minimal instance
assert!(
!dna.zomes.clone().is_empty(),
"Empty zomes = No genesis = infinite loops below!"
"Empty zomes = No init = infinite loops below!"
);

Ok((instance, context))
Expand Down
37 changes: 37 additions & 0 deletions core/src/nucleus/actions/call_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{
context::Context,
nucleus::ribosome::callback::{init::init, CallbackParams, CallbackResult},
};
use holochain_core_types::{
dna::Dna,
error::{HcResult, HolochainError},
};
use std::sync::Arc;

/// Creates a network proxy object and stores DNA and agent hash in the network state.
pub async fn call_init(dna: Dna, context: &Arc<Context>) -> HcResult<()> {
// map init across every zome. Find which zomes init callback errored, if any
let errors: Vec<(String, String)> = dna
.zomes
.keys()
.map(|zome_name| {
(
zome_name,
init(context.clone(), zome_name, &CallbackParams::Init),
)
})
.filter_map(|(zome_name, result)| match result {
CallbackResult::Fail(error_string) => Some((zome_name.to_owned(), error_string)),
_ => None,
})
.collect();

if errors.is_empty() {
Ok(())
} else {
Err(HolochainError::ErrorGeneric(format!(
"At least one zome init returned error: {:?}",
errors
)))
}
}