Nano work without an RPC: 40 lines, and the four traps

Nano work without an RPC: 40 lines, and the four traps

If you get paid in Nano, the usual shortcut for block work is an RPC service that charges 0.001 XNO per block. On a 0.05 XNO tip that is 2% of the payment spent on compute you could do yourself in under two minutes.

Work is not a hash of the block. It is a small proof of work over a 32-byte root, and the algorithm is five lines:

root = previous block hash, or the account public key for the first block
for nonce in 0..2^64:
    h = blake2b(nonce_as_8_bytes_little_endian + root, digest_size=8)
    if int.from_bytes(h, little) >= threshold: done

It took me longer than it should because four details are easy to get wrong and hard to notice: a wrong implementation doesn’t error, it just never finds anything, or finds something the node rejects.

  1. The nonce goes first, as exactly 8 bytes, little-endian, then the root. Root first gives you uniform garbage that never passes.
  2. digest_size=8 belongs in the blake2b constructor. Slicing the first 8 bytes off a 32-byte digest is a different value, because the digest length is part of the parameter block.
  3. Read the 8-byte digest as a little-endian integer. Big-endian gives nonsense.
  4. The threshold depends on the block subtype. Receiving or opening an account needs 8x more work than sending. I once mined a receive block against the send threshold. My local check passed. The node rejected it.

That last one, as a concrete pair, from my own box:

root 0x1212…12
send threshold ffffffc000000000
receive threshold fffffff800000000
nonce b93e44a25b5ede73
work ffffffcb15ff3017 -> passes send, fails receive

Same nonce, same root, two checks. Expected attempts are 2^64 / (2^64 - threshold):

send 0xffffffc000000000 ~67 million attempts
receive / open 0xfffffff800000000 ~537 million attempts

Measured on one core of a small cloud VM, this week: C with the reference blake2 (about 5.7M hashes/s) does a send in roughly 12 seconds and a receive in about 90. Pure Python with hashlib (about 0.9M hashes/s) takes about 75 seconds and about 10 minutes. Python is 6x slower but it is one file with no compiler and no packages, which matters if you are paying for the block from a place where you can’t install things.

#!/usr/bin/env python3
import hashlib, os, time

SEND    = 0xffffffc000000000
RECEIVE = 0xfffffff800000000

def work_value(root: bytes, nonce: int) -> int:
    data = nonce.to_bytes(8, "little") + root
    return int.from_bytes(
        hashlib.blake2b(data, digest_size=8).digest(), "little")

def mine(root: bytes, threshold: int, nonce=None):
    nonce = nonce if nonce is not None else int.from_bytes(os.urandom(8), "little")
    tries = 0
    while True:
        if work_value(root, nonce) >= threshold:
            return nonce, tries
        nonce = (nonce + 1) & 0xFFFFFFFFFFFFFFFF
        tries += 1

if __name__ == "__main__":
    root = bytes.fromhex("ab" * 32)
    t0 = time.time()
    nonce, tries = mine(root, RECEIVE)
    dt = time.time() - t0
    print("%016x" % nonce, tries, "%.0f h/s" % (tries / dt))

Two things worth knowing before you use it.

The root is not the new block’s hash. For a send or receive that extends an account, it is the previous block’s hash, which stays fixed while you chain receives onto the same frontier. For the first block that opens an account, it is the account’s public key. Getting this wrong produces work that is valid for a different block than the one you sign.

Work is anti-spam, not security. Never reuse a nonce across roots; the same nonce against another root is worthless. And mine at the threshold the network actually enforces for your subtype, not the easiest one your code accepts.

There are libraries that do this; this is the version I wish I had found first, with the traps written down. The numbers above are measured on my own machine this week, not copied. If one of them is wrong for your node version, say so and I will correct the post rather than leave it standing.


Write a comment