bc
Blockchain and distributed ledger - blocks, Merkle trees, transactions, Proof of Work mining, wallets, consensus algorithms, smart contracts, and network propagation.
Load with: use bc
What this module does
bc is a complete blockchain implementation in pure ilusm. It gives you
all the primitives: create blocks, link them in a chain, build Merkle trees from
transaction IDs, create and sign transactions, mine blocks with Proof of Work,
manage key-pair wallets, validate the full chain, and run smart contract functions.
Block hashes are SHA-256 of the concatenated index, timestamp, data, previous hash,
nonce, and Merkle root. Transaction IDs are SHA-256 of sender + recipient + amount + timestamp + fee.
Proof of Work requires the hash to start with diff zero characters.
Quick example
use bc
# Create a chain (difficulty 4, mining reward 10)
chain = bccaa(4, 10.0)
# Create a wallet
wallet = bcwba()
# Create and sign a transaction
tx = blcea(wallet.addr, "recipient_addr", 5.0, 0.01)
tx = blcda(tx, wallet.priv_key)
chain = bcdtx(chain, tx)
# Mine the next block
mined = bcmne(chain, wallet.addr)
prn(mined.hash)
# Validate the chain
prn(bcvld(chain)) # tru
Functions
Blocks
bcbaa(index, data, previous_hash)
Creates a new block with the given index, data (any value), and previous block's hash. Automatically sets timestamp to tim.now() and computes the initial hash via blcia. Returns the block object.
blcia(block)
Computes a block's hash as SHA-256 of index + timestamp + data + previous_hash + nonce + merkle_root concatenated as strings. Used internally to set and verify block.hash.
Merkle tree
blcfa()
Creates an empty Merkle tree with nodes, root, and leaves fields.
blcba(merkle, data)
Adds a leaf to the Merkle tree by SHA-256 hashing data and appending it to leaves and nodes.
blckc(merkle)
Builds the Merkle tree by pairing and hashing leaf nodes up to the root. Odd numbers of nodes duplicate the last one. Sets merkle.root to the final hash.
Transactions
blcea(sender, recipient, amount, fee)
Creates a transaction. The transaction ID is SHA-256 of sender + recipient + amount + timestamp + fee. Fee defaults to 0.0 if nil. Returns the transaction object.
blcca(tx)
Computes the transaction hash (same data as used for the ID). Used internally for signing and verification.
blcda(tx, priv_key)
Signs a transaction with a private key via cry.sign. Sets tx.sign. Returns the updated transaction.
blcaa(tx, pub_key)
Verifies a transaction's signature against a public key via cry.cryct. Returns tru on valid signature.
Blockchain
bccaa(diff, mine_rwd)
Creates a new blockchain. Immediately creates the genesis block (index 0, data "Genesis Block", previous hash "0"). Sets the mining difficulty and reward. Starts with an empty pending transaction list.
bcdtx(bc, tx)
Adds a transaction to the pending pool.
bcmne(bc, miner_addr)
Mines a new block from all pending transactions. Creates a coinbase transaction paying mine_rwd to miner_addr, builds a Merkle tree from transaction IDs, mines the block with Proof of Work, and appends it to the chain. Returns the mined block, or nil if no pending transactions or mining fails.
bcmnp(blk, diff)
Proof of Work loop. Increments nonce until the block hash starts with diff zeros (target string). Stops at nonce 4294967295 (max 32-bit int). Returns the solved block or nil.
bcvld(bc)
Validates the full chain. For each block (starting at index 1) checks: previous hash matches, Proof of Work target is met, and the computed hash matches the stored hash. Returns tru if all checks pass.
Wallets
bcwba()
Generates a new key-pair wallet. Creates a 32-byte random private key via cry.cryrn, derives the public key via cry.cryfp, and sets the address to SHA-256 of the public key. Starts with balance 0.0 and an empty transaction history.
bcwll(wal, recip, amt, fee)
Creates a signed transaction from the wallet to a recipient, adds it to the wallet's transaction history, and decrements the wallet balance by amt + fee. Returns the transaction.
bcwaa(wal, bc)
Calculates the actual wallet balance by scanning all blocks in the chain for transactions involving the wallet's address. Updates and returns wal.bal.
Consensus
bccsc(algo)
Creates a consensus object for the given algorithm string: "pow", "pos", or "dpos".
bccpw(cns, bc)
Proof of Work consensus - mining is handled by bcmne/bcmnp, so this returns tru.
bccnp(cns, tot_stk)
Proof of Stake - selects a validator weighted by their stake in cns.stks using a random number. Sets cns.cur_prop to the chosen validator.
bccnd(cns, bc)
Delegated Proof of Stake - round-robin through sorted validator keys, selecting the one at index current_block_count % validator_count. Sets cns.cur_prop.
Network
bcnaa()
Creates a network object with an empty node map, connection list, message list, and protocol version "1.0".
bcntd(net, node)
Adds a node to the network by its node.id.
bcntc(net, nid1, nid2)
Establishes a connection between two node IDs, recording the timestamp.
bcntb(net, msg, from_n)
Broadcasts a message to all nodes that from_n has a connection to. Appends messages to net.msgs.
bcnts(net, bc)
Broadcasts the latest block to the network as a "new_blk" message.
Smart contracts
bcsca(addr, code, own)
Creates a smart contract with an address, code string, owner address, empty storage, zero balance, and empty ABI.
bcsmr(cont, fn_name, params, sndr, vl)
Dispatches a contract function call. Supports "transfer" (token transfer between addresses in storage) and "balOf" (balance lookup). Returns {success: fls, err: "Unknown fn"} for any other function name.
Notes
- Proof of Work mines up to nonce 4294967295 only - very high difficulty values may never find a solution within this range.
- Block hashes use SHA-256 via
cry.crys2. Transaction signing usescry.signandcry.cryct. - The smart contract dispatcher only implements
"transfer"and"balOf"- other function names return an error object. - Requires
trl,txt,jsn,cry,hash,tim, andobs.