GithubHelp home page GithubHelp logo

jaimehgb / raiblockswebassemblypow Goto Github PK

View Code? Open in Web Editor NEW
33.0 5.0 8.0 249 KB

WebAssembly Nanocurrency PoW implementation

License: BSD 3-Clause "New" or "Revised" License

C 8.61% C++ 91.32% Makefile 0.07%
nano nanocurrency nano-currency cryptocurrency webassembly pow proof-of-work

raiblockswebassemblypow's Introduction

RaiBlocksWebAssemblyPoW

Overview

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...


Installation and usage

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.


Compatibility

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.


Examples

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

raiblockswebassemblypow's People

Contributors

jaimehgb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

raiblockswebassemblypow's Issues

PoW threshold will be invalid with new upcoming v21 change

The new PoW needs to be changed from ffffffc000000000 to 8x that or fffffff800000000. Even better if it can be set via a parameter because send/change blocks will use 8x while open/receive blocks only require 1/8x or fffffe0000000000.

Current threshold:

uint64_t threshold = 0xffffffc000000000;

Reference: https://docs.nano.org/releases/network-upgrades/#increased-work-difficulty
Reference tool: https://tools.nanos.cc/?tool=difficulty&basediff=ffffffc000000000&multiplier=8

PR for webgl: numtel/nano-webgl-pow#1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.