Create and Validate a Block
Create one ALX Block from JSON content and validate both deterministic identities. A successful validation confirms that the content and declared parent set produce the recorded contentHash and blockHash.
Before You Start
Use Node.js 20 or later and the published @alx-protocol/sdk package. No account, API key, network connection, smart contract, blockchain, or production data is required.
Workflow
- Create a Block from JSON content and an empty parent set.
- Inspect the generated
contentHashandblockHash. - Run
validateBlock()to recompute and compare both deterministic identities. - Modify the content,
contentHash,blockHash, or parent set. - Run
validateBlock()again and confirm that validation fails.
What Validation Confirms
validateBlock() performs local Block validation.
A successful result confirms that:
contentHashmatches the canonicalized content.blockHashmatches the canonicalized content and normalized parent set.- The Block satisfies protocol validation requirements.
Validation uses only protocol data. Any conforming implementation produces equivalent validation results from equivalent protocol inputs.
- Prerequisites
- Example
- Output
- Failure Cases
Use Node.js 20 or later and install the package from npm.
Install the package:
npm i @alx-protocol/sdk
Dependency installation may require package-registry access. After installation, Block creation and validation run locally without credentials or a network connection.
Create a root Block, validate both deterministic identities, and fail the process if validation does not pass.
Create validate-block.mjs:
import {
createBlock,
validateBlock,
} from '@alx-protocol/sdk';
const content = {type: 'example', value: 1};
const block = createBlock(content, []);
const result = validateBlock(block);
console.log(JSON.stringify({
contentHash: result.contentHash,
blockHash: result.blockHash,
ok: result.ok,
}, null, 2));
if (!result.ok) process.exitCode = 1;
Run:
node validate-block.mjs
The overall result is true only when both deterministic identity checks pass.
contentHash.storedandcontentHash.recomputedcontain the same lowercase0x-prefixed 32-byte hash.blockHash.storedandblockHash.recomputedcontain the same lowercase0x-prefixed 32-byte hash.- Both
matchvalues andokaretrue. - The empty parent array makes the result a root Block.
Change one protocol input at a time and inspect which deterministic identity fails.
| Change | contentHash.match | blockHash.match | ok |
|---|---|---|---|
Modify content | false | false | false |
Modify stored contentHash only | false | true | false |
| Modify the parent set | true | false | false |
Modify stored blockHash only | true | false | false |
Malformed hashes are rejected before comparison. Reordering the same valid parents does not change identity because ALX normalizes the parent set.
Related Documentation
Continue with the protocol rules for Block structure, Block identity, canonicalization, and validation.