It’s the strongest open-source chess engine in the world, and has been the top computer chess engine for years. Grandmasters use it to analyse their games. It’s over 12,000 lines of heavily optimised C++, the kind of code you’d normally never expect to run inside a smart contract.
What runs on-chain
There’s no server and no frontend hosting. One canister does everything:
serves the web page,
referees the game (checks your move is legal, detects checkmate, stalemate, threefold repetition and the 50-move rule),
thinks of its reply: a full Stockfish search, computed by all 13 nodes of the subnet and agreed on by consensus,
writes every move to stable memory, so each game gets a permanent link anyone can replay (/game/<id>), plus a raw JSON record (/api/game/<id>).
For each engine move the page shows a receipt: how many positions it searched, how many instructions that took, and roughly what it cost in cycles. The canister pays (reverse gas), so players don’t need a wallet.
How
Stockfish 11 (the last version before neural nets), compiled to wasm with icpp-pro. Wasm is about 2.65 MB.
The IC has no threads, so a small SF_NO_THREADS patch makes the search run synchronously inside a single update call. Apart from that the engine code is unchanged, and node counts match a native build exactly.
Cost is about 8–9.5k instructions per searched position. A depth-18 move takes around 12B instructions, which fits comfortably inside the 40B per-message limit, so each move is one update call. There’s no need for chunking or timers.
All HTTP routes go through http_request_update, so the responses are certified.
There’s a daily budget: moves are full strength up to a daily allowance, then lighter searches, then the canister politely says “come back tomorrow”. It’s funded by a small cycles balance, so please be gentle
Thanks
Big thanks to the @icpp and onicai. Porting a large real-world C++ codebase was much smoother than I expected, and I think it’s a great showcase of what C++ canisters can do.
Feedback welcome, and so is a game. Can anyone beat it?
Box2D 2.4.2 is vendored unchanged: it compiled for the IC with icpp-pro without a single patch.
One hole a day, generated from the date, so everybody plays the same one.
The browser sends only an angle and a power. The canister simulates the shot at 60 Hz until the ball stops, sinks or goes out, stores it, and returns the path for the page to animate.
So the leaderboard can’t be faked: the score is whatever the chain computed. GET /api/golf/round/<id>/shot/<n> makes the canister simulate any stored shot again and report whether it lands in exactly the same place. It does, bit for bit, because wasm floats are deterministic.
A shot is about 12M instructions, roughly a thousandth of one chess move.
A shared fuel tank
Everything is reverse gas, so players need no wallet. To keep it running without one person paying forever, anyone can send ICP to the canister’s principal (from funnAI, the NNS app or any wallet) and press Refuel. The canister then:
checks its ICP balance on the ledger,
transfers it to the cycles minting canister with the top-up memo, and
calls notify_top_up to turn it into its own cycles.
Engine moves pause before the freezing threshold, so the canister never freezes and the page can still ask for fuel. The dashboard shows every game and the cycles it used.
Also new
A home page that links both games. Chess moved to /chess; old /game/<id> replay links still work.
For C++ devs: icpp-pro’s C++20 coroutines made the ledger and CMC calls straightforward. The ledger’s error types are large, so I decode only the Ok/Err label of their Candid replies with a small parser.
Next on my list: certified assets, so pages load faster. And still open to suggestions: which C++ library should run on-chain next?
This is nice to see. Before C++ canisters were able to make their own cross-canister calls, it was always needed to put a Motoko “controller or gateway” canister in front of it, and it introduced additional cross-canister hops from frontend > gateway > C++ engine. Now it is direct, which makes is cheaper in cycles and with less latency.
Did you need to use the capability to yield the instruction budget so the C++ engine can go beyond the 40B instructions limit per update call, or did it all fit within a single update call of 40B instructions?
That would allow to use decistion tree models for all kinds of stuff. For many years, xgboost models won every single ML competition, and I think it is a good fit for an IC canister.
thats very impressive. Call me old fashioned but I actually still prefer c over c++. And box2d is a useful library but you just dont get the unique feeling of a custom physics engine. Rigid body’s just feel a bit rigid.
In my opinion.
getting a physics engine on chain though, that’s an impressive thing for sure.
I think that is the correct word. Maybe you could get Rybka to run on chain too, and people could play against either of them. That might be fun and it seems appropriate. Maybe you could even have them play against eachother, and find the strongest on chain chess engine.
No yield needed: every move fits in one update call. A depth-18 search is about 12B instructions, well under 40B, and the daily budget lowers the depth before it gets anywhere near the limit. A golf shot is ~10M instructions, and the 3D putts I’m testing with Bullet are ~200M. Yielding would make sense for something like a full-depth analysis mode, maybe later!
And the direct ledger/CMC calls really are nice: no gateway canister, one hop.
Great suggestion, and it’s high on my list. Inference is pure C++ and deterministic, so it fits a canister well, and it would be the first non-game demo.
Love the engine-vs-engine idea. Rybka isn’t open source, but there are strong GPL engines (Fruit 2.1, Ethereal, Berserk). Two engines playing each other on-chain, every move computed by 13 nodes, would make a fun tournament. And yes, Box2D is rigid by design The next game uses Bullet for 3D.
@stefan.schneider this one’s for you After you said you lost badly, I added levels. You can now start at level 1, where Stockfish still makes silly mistakes, and work your way up.
So, what changed:
Every game now has 20 levels. You unlock the next one by winning. In chess that means beating Stockfish; in golf, sinking the hole. At the bottom, Stockfish throws in random moves (all 13 nodes still agree on which ones). By level 20 it’s the real thing, full strength.
Every golf level is a different course. The higher you go, the hillier it gets and the more stuff is in your way. The sky changes as you climb too: you start at sunrise, and by level 17 you’re playing under the moon. Same for the 3D mini-golf, which now has a windmill that keeps speeding up.
3D mini-golf is properly live now, right on the home page.
A couple of things I had to be careful about:
Every shot ever played can be replayed on-chain and has to land in exactly the same spot. So older rounds keep their old course, and only new rounds get the new ones.
I didn’t want an impossible level. I ran a little solver over 30 days of every level, and it sank every hole. One level-20 3D course was boxed in at first, so I toned it down.
Still no need to yield: even a full-strength chess move fits in one update call.