Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
DHT hold rejection example
const { Config, Scenario } = require("@holochain/holochain-nodejs")
const dnaPath = "./dist/bundle.json"
const agentAlice = Config.agent("alice")
const agentBob = Config.agent("bob")
const dna = Config.dna(dnaPath)
const instanceAlice = Config.instance(agentAlice, dna)
const instanceBob = Config.instance(agentBob, dna)
const bobAndAlice = new Scenario([instanceAlice, instanceBob], { debugLog: true })
bobAndAlice.runTape("Alice can publish to herself, but others will reject her entries", async (t, { alice, bob }) => {
// in this case
// the await actually doesn't return, since it was waiting for full DHT saturation
// (everyone running the app holds a copy of the entry)
let result = await alice.callSync("people", "add_person", { name: "Bonnitta" })
extern crate hdk;
extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate holochain_core_types_derive;
extern crate boolinator;
use boolinator::Boolinator;
use hdk::{
use holochain_wasm_utils::api_serialization::get_links::GetLinksResult;
#[derive(Serialize, Deserialize, Debug, DefaultJson)]
pub struct Person {
name: String
fn handle_add_person(name: String) -> ZomeApiResult<Address> {
let person_entry = Entry::App("person".into(), Person {
name: name,
// somehow, we know Alice's public address ahead of time
const ALICE_ADDRESS: &'static str = "alice-----------------------------------------------------------------------------AAAIuDJb4M";
define_zome! {
entries: [
name: "person",
description: "a human with a name",
sharing: Sharing::Public,
native_type: Person,
validation_package: || {
validation: |person: Person, validation_data: hdk::ValidationData| {
// when using (in validation_package) hdk::ValidationPackageDefinition::Entry
// the chain_header for the entry is returned, providing some useful
// metadata for validating against
let chain_header = &validation_data.package.chain_header;
// provenances() returns an array, since there can be multiple authors/signers for a single Entry
let first_author = &chain_header.provenances()[0];
// first_author is a tuple (agent_address, agent_signature)
let first_author_agent_address = first_author.0.to_string();
// if self is not alice, and entry author is alice, don't hold the Entry
if hdk::AGENT_ADDRESS.to_string() != ALICE_ADDRESS && first_author_agent_address == ALICE_ADDRESS {
Err("No one but Alice will hold Alice's entries".to_string())
else {
links: []
genesis: || { Ok(()) }
functions: [
add_person: {
inputs: |name: String|,
outputs: |result: ZomeApiResult<Address>|,
handler: handle_add_person
traits: {
hc_public [add_person]

This comment has been minimized.

Copy link
Owner Author

@Connoropolous Connoropolous commented Feb 20, 2019

In the scenario test that we've developed, Bob's node logs as blue, and Alice's as green.
Notice how eventually, after some communication between them, Bob is the one to reject holding Alice's entry.
screen shot 2019-02-20 at 5 57 40 pm


This comment has been minimized.

Copy link

@GildedHonour GildedHonour commented Jun 21, 2019

// somehow, we know Alice's public address ahead of time
const ALICE_ADDRESS: &'static str = "alice-----------------------------------------------------------------------------AAAIuDJb4M";

How somehow? How to find it out by running JS tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment