Compose Blocks into a Graph
A Block references earlier Blocks through parentHashes. Each parent reference is a direct edge in the derivation Graph, from the new Block to a parent Block the application declares.
Create a Child Block
Create compose.mjs. In the script, result.parentHashes contains source.blockHash. contentHash identifies canonicalized content. blockHash identifies that content together with the normalized parent set, so result.blockHash depends on source.blockHash and result.contentHash does not.
For a Block created from multiple inputs, list each input's blockHash. ALX normalizes the list, so the order of the entries and any duplicate entries do not change the resulting blockHash.
import { createBlock, validateBlock, validateGraph } from '@alx-protocol/sdk';
const source = createBlock({ step: 'source' }, []);
const result = createBlock(
{ step: 'result' },
[source.blockHash],
);
console.log(result.parentHashes);
Verify the Graph
Validate each Block, then verify the Graph that the declared parents form. With blocks set to [result], the parent is missing and the script prints true incomplete, not invalid. Lineage Graphs defines the four statuses and the Verification Context. Add the following to the same script and run node compose.mjs:
const blocks = [source, result];
if (!blocks.every((block) => validateBlock(block).ok)) {
throw new Error('Block identity validation failed');
}
const graphResult = validateGraph(
result,
{ kind: 'finite', blocks },
{ protocolVersion: '1', validationMode: 'closed-world', graphRoot: result.blockHash, externalParents: [] },
);
console.log(graphResult.checked, graphResult.status);
[
'0xee8164109029fd6871345f68c4e241d16485823f95ccd83ce9b6fdc1400ea6b8'
]
true valid
When to Use a Graph
Use parent relationships when your application needs to:
- Trace how a Block was produced.
- Find the dependencies of a Block.
- Verify that every referenced parent is available in the supplied Blocks.
- Detect cycles or missing links in lineage.