Skip to content

Instantly share code, notes, and snippets.

@JoshOrndorff
Last active November 15, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshOrndorff/b0fe7aed93d16beabc2885484c6e8c54 to your computer and use it in GitHub Desktop.
Save JoshOrndorff/b0fe7aed93d16beabc2885484c6e8c54 to your computer and use it in GitHub Desktop.
Ability to lookup data on RChain
// When listening for data (or continuation) at a name, the returned
// blockResults contain only blocks in which data was actually sent on
// the specified name. HOWEVER, each block in the blockResults contains
// a postBlockData which contains ALL previous sends on the name, not
// only the sends in that block. This code demonstrates that behavior
// by sending on the public name @"testChan" in blocks 1, 3, and 5, but
// not in blocks 2, 4, and 6.
//
// Run this shit on a fresh node if you care about your sanity.
//
// This behaviour is clear enough now that I've figured it out, but it
// is not at all intuitive. Worse, it means that the data sent on @"testChan"
// is present in ALL future blocks (assuming it isn't consumed), but it
// cannot be queried unless the query depth extends back to at least
// the most recent send.
const { RNode } = require('rchain-api');
const grpc = require('grpc');
const myNode = RNode(grpc, {host: 'localhost', port: 40401});
function deployWithSend() {
return {
term: '@"testChan"!("data sent on testChan")',
timestamp: (new Date()).valueOf(),
phloLimit: { value: 999999 },
phloPrice: { value: 1 },
};
}
function deployWithoutSend() {
return {
term: '"Nothing to see here."',
timestamp: (new Date()).valueOf(),
phloLimit: { value: 999999 },
phloPrice: { value: 1 },
};
}
main();
async function main() {
// Add blocks to DAG in pairs
for (let x = 0; x < 3; x++) {
console.log(await myNode.doDeploy(deployWithSend(), false));
console.log(await myNode.createBlock());
console.log(await myNode.doDeploy(deployWithoutSend(), false));
console.log(await myNode.createBlock());
}
// Grab all blocks in which data was sent on @"testChan"
// Change depth to observe trippy behavior
const blockResults = await myNode.listenForDataAtPublicName('testChan', depth=1);
console.log(`we got ${blockResults.length} blocks.`)
// Uncomment for detailed logging of blocks
//console.log(blockResults);
// Investigate the most recent block with a send
const data = blockResults[0].postBlockData;
console.log(`We got ${data.length} sends in the latest block`);
// Uncomment for detailed logging of sends in latest block
//for (let datum of data) { console.log(datum.exprs); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment