This repo contains a simple RaiBlocks PoW implementation compiled to WebAssembly to boost its performance on browsers. That's been done using
emscripten.
Compiling to WebAssembly, the result is around 10 times faster than a pure JS PoW implementation (e.g.: RaiBlocksJS).
This basically makes possible to generate proofs of work on modern browsers in a reasonable time. So yep, lets see...
All the PoW thing takes place at functions.cpp
. There is the main loop which calculates stuff and a function which
can be called from JS and runs the loop (the iterations function).
To compile it to webassembly you need to install emscripten:
git clone https://github.com/juj/emsdk.git
cd emsdk
# on Linux or Mac OS X
./emsdk install --build=Release sdk-incoming-64bit binaryen-master-64bit
./emsdk activate --global --build=Release sdk-incoming-64bit binaryen-master-64bit
# on Windows
emsdk install --build=Release sdk-incoming-64bit binaryen-master-64bit
emsdk activate --global --build=Release sdk-incoming-64bit binaryen-master-64bit
Then:
# on Linux or Mac OS X
source ./emsdk_env.sh
# on Windows
emsdk_env.bat
(From here)
With that done, at the repo folder run this:
emcc functions.cpp blake2/blake2b-ref.cpp -o pow.js -s WASM=1 -std=gnu++11 -O3 -s EXPORTED_FUNCTIONS="['_launchPoW']"
It will output 2 files: pow.js
and pow.wasm
, place those files together somewhere and include pow.js
in your html as usual.
To call the "launchPoW" function you can do 2 things:
var hash = "SOME_RAIBLOCKS_HASH_HERE_AND_STUFF_HEX_ENCODED_32_BYTES";
var pow = Module.ccall("launchPoW", "string", ["string"], hash);
console.log(pow);
OR
var hash = "SOME_RAIBLOCKS_HASH_HERE_AND_STUFF_HEX_ENCODED_32_BYTES";
var calculatePoW = Module.cwrap("launchPoW", "string", ["string"]);
var pow = calculatePoW(hash);
console.log(pow);
I prefer the second option :D
What that function does is to try to find a valid PoW in 10,000,000 iterations. If it finds it it will return the result as a hex string.
If it does not find it, will return a 64bit hex 0 (0000000000000000
). Keep that in mind :P
BUT ... but. You cannot call the function before WebAssembly is loaded and compiled. How do we know when that happens?
Well, we have:
Module['onRuntimeInitialized'] = function() {
// Its all fine here
// do stuff
// ...
}
If Module['onRuntimeInitialized']
is a function it will be called once its all ready to run. So yeah, be aware of that :)
There are more docs about the Module API and emscripten itself here http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/index.html.
This implementation has just been tested in Chrome (Windows 64bit), Firefox (Windows 64bit) and Chrome (Android) but should also work in all the devices supporting WASM.
I've made two examples for you to see/use/improve/whatever. They are at the /examples folder.
One of them uses webworkers to multithread this stuff. The other one is single threaded.
Yeah, take a look there :P