Skip to content

Commit

Permalink
Update Block mining to RandomX.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Jan 9, 2020
1 parent a63edf3 commit f28b57f
Show file tree
Hide file tree
Showing 35 changed files with 256 additions and 117 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ __pycache__
libminisketch.so
minisketch.dll

#RandomX.
mc_randomx

#Milagro.
incubator-milagro-crypto-c

Expand Down
1 change: 1 addition & 0 deletions Meros.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ skipExt = @["nim"]
#Dependencies
requires "nim >= 1.0.4"
requires "https://github.com/MerosCrypto/Argon2"
requires "https://github.com/MerosCrypto/mc_randomx"
requires "https://github.com/MerosCrypto/mc_bls"
requires "https://github.com/MerosCrypto/mc_ed25519"
requires "https://github.com/MerosCrypto/mc_minisketch"
Expand Down
26 changes: 5 additions & 21 deletions PythonTests/Classes/Merit/Block.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#Types.
from typing import Dict, Any

#RandomX lib.
from PythonTests.Libs.RandomX import RandomX

#BLS lib.
from PythonTests.Libs.BLS import PrivateKey

#BlockHeader and BlockBody classes.
from PythonTests.Classes.Merit.BlockHeader import BlockHeader
from PythonTests.Classes.Merit.BlockBody import BlockBody

#Argon2 lib.
import argon2

#Block class.
class Block:
#Constructor.
Expand All @@ -34,26 +34,10 @@ def mine(
(int.from_bytes(self.header.hash, "big") < difficulty)
):
self.header.proof += 1
self.header.hash = argon2.low_level.hash_secret_raw(
self.header.serializeHash(),
self.header.proof.to_bytes(8, "big"),
1,
65536,
1,
48,
argon2.low_level.Type.D
)
self.header.hash = RandomX(self.header.serializeHash())
self.header.signature = privKey.sign(self.header.hash).serialize()

self.header.hash = argon2.low_level.hash_secret_raw(
self.header.hash,
self.header.signature,
1,
65536,
1,
48,
argon2.low_level.Type.D
)
self.header.hash = RandomX(self.header.hash + self.header.signature)

#Serialize.
def serialize(
Expand Down
31 changes: 6 additions & 25 deletions PythonTests/Classes/Merit/BlockHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#Sketch class.
from PythonTests.Libs.Minisketch import Sketch

#Argon2 lib.
import argon2
#RandomX lib.
from PythonTests.Libs.RandomX import RandomX

#Blake2b standard function.
from hashlib import blake2b
Expand Down Expand Up @@ -102,30 +102,15 @@ def serializeHash(
self.sketchCheck +
(1 if self.newMiner else 0).to_bytes(1, "big") +
(self.minerKey if self.newMiner else self.minerNick.to_bytes(2, "big")) +
self.time.to_bytes(4, "big")
self.time.to_bytes(4, "big") +
self.proof.to_bytes(4, "big")
)

#Hash.
def rehash(
self
) -> None:
self.hash: bytes = argon2.low_level.hash_secret_raw(
argon2.low_level.hash_secret_raw(
self.serializeHash(),
self.proof.to_bytes(8, "big"),
1,
65536,
1,
48,
argon2.low_level.Type.D
),
self.signature,
1,
65536,
1,
48,
argon2.low_level.Type.D
)
self.hash: bytes = RandomX(RandomX(self.serializeHash()) + self.signature)

#Constructor.
def __init__(
Expand Down Expand Up @@ -164,11 +149,7 @@ def __init__(
def serialize(
self
) -> bytes:
return (
self.serializeHash() +
self.proof.to_bytes(4, "big") +
self.signature
)
return self.serializeHash() + self.signature

#BlockHeader -> JSON.
def toJSON(
Expand Down
11 changes: 11 additions & 0 deletions PythonTests/Classes/Merit/Blockchain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#Types.
from typing import Dict, List, Tuple, Any

#RandomX lib.
from PythonTests.Libs.RandomX import setRandomXKey

#BLS lib.
from PythonTests.Libs.BLS import PublicKey

Expand All @@ -18,6 +21,9 @@ def __init__(
blockTime: int,
startDifficulty: int
) -> None:
self.upcomingKey: bytes = genesis.rjust(48, b'\0')
setRandomXKey(self.upcomingKey)

self.blockTime: int = blockTime

self.startDifficulty: int = startDifficulty
Expand Down Expand Up @@ -46,6 +52,11 @@ def add(
) -> None:
self.blocks.append(block)

if len(self.blocks) % 2048 == 0:
self.upcomingKey = block.header.hash
elif len(self.blocks) % 2048 == 64:
setRandomXKey(self.upcomingKey)

if len(self.blocks) - 1 == self.difficulties[-1][1]:
#Blocks per months.
blocksPerMonth: int = 2592000 // self.blockTime
Expand Down
58 changes: 58 additions & 0 deletions PythonTests/Libs/RandomX.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#Types.
from typing import Any

#CTypes.
from ctypes import cdll, c_int, c_char, \
Array, c_char_p, c_void_p, create_string_buffer, byref

#OS standard lib.
import os

#Import the RandomX library.
#pylint: disable=invalid-name
RandomXLib: Any
if os.name == "nt":
RandomXLib = cdll.LoadLibrary("PythonTests/Libs/mc_randomx/RandomX/build/randomx")
else:
RandomXLib = cdll.LoadLibrary("PythonTests/Libs/mc_randomx/RandomX/build/librandomx.so")

#Define the function types.
RandomXLib.randomx_get_flags.randomx_get_flags = None
RandomXLib.randomx_get_flags.restype = c_int

RandomXLib.randomx_alloc_cache.argtypes = [c_int]
RandomXLib.randomx_alloc_cache.restype = c_void_p

RandomXLib.randomx_init_cache.argtypes = [c_void_p, c_char_p, c_int]
RandomXLib.randomx_init_cache.restype = None

RandomXLib.randomx_create_vm.argtypes = [c_int, c_void_p, c_void_p]
RandomXLib.randomx_create_vm.restype = c_void_p

RandomXLib.randomx_vm_set_cache.argtypes = [c_void_p, c_void_p]
RandomXLib.randomx_vm_set_cache.restype = None

RandomXLib.randomx_calculate_hash.argtypes = [c_void_p, c_char_p, c_int, c_void_p]
RandomXLib.randomx_calculate_hash.restype = None

flags: c_int = RandomXLib.randomx_get_flags()
cache: c_void_p = RandomXLib.randomx_alloc_cache(flags)
RandomXLib.randomx_init_cache(cache, None, 0)
vm: c_void_p = RandomXLib.randomx_create_vm(flags, cache, None)

def setRandomXKey(
key: bytes
) -> None:
RandomXLib.randomx_init_cache(cache, c_char_p(key), c_int(len(key)))
RandomXLib.randomx_vm_set_cache(vm, cache)

def RandomX(
data: bytes
) -> bytes:
hashResult: Array[c_char] = create_string_buffer(48)
RandomXLib.randomx_calculate_hash(vm, c_char_p(data), c_int(len(data)), byref(hashResult))

result: bytes = bytes()
for b in hashResult:
result += b
return result
2 changes: 1 addition & 1 deletion PythonTests/Meros/Meros.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,4 @@ def quit(
pass

if self.process.returncode != 0:
raise Exception("Meros didn't quit with code 0.")
raise NodeError("Meros didn't quit with code 0.")
2 changes: 1 addition & 1 deletion PythonTests/Meros/RPC.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def reset(

#Launch Meros.
self.meros = Meros(self.meros.db, self.meros.tcp, self.meros.rpc)
sleep(3)
sleep(5)

#Reconnect.
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down
20 changes: 19 additions & 1 deletion PythonTests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@ These tests require Python 3.6+ and pip. To install the needed modules:

`python3 -m pip install --user argon2-cffi ed25519`

They also require the Minisketch and Milagro dynamic libraries. The first should have been built in the process of setting up the `mc_minisketch` Nimble package. Place `libminisketch.so` or `minisketch.dll` under `PythonTests/Libs`. The second needs to be rebuilt by running the following commands from within the `PythonTests/Libs` directory.
They also require the Minisketch, RandomX, and Milagro shared libraries. Minisketch's should have been built as part of `mc_minisketch`.

- Place `libminisketch.so` (or `minisketch.dll`) from `mc_minisketch` under `PythonTests/Libs`.

RandomX and Milagro need to be built again by running the following commands from within the `PythonTests/Libs` directory.

```
git clone https://github.com/MerosCrypto/mc_randomx
cd mc_randomx
git submodule update --init --recursive
cd RandomX/src
rm configuration.h
rm randomx.h
cp ../../MerosConfiguration/* .
cd ..
mkdir build
cd build
cmake -DARCH=native -DBUILD_SHARED_LIBS=ON ..
make
cd ../../..
git clone https://github.com/apache/incubator-milagro-crypto-c
cd incubator-milagro-crypto-c
mkdir build
Expand Down
3 changes: 2 additions & 1 deletion PythonTests/Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@

#Meros instance.
meros: Meros = Meros(test.__name__, port, port + 1)
sleep(2)
sleep(5)

rpc: RPC = RPC(meros)
try:
Expand All @@ -140,6 +140,7 @@
finally:
try:
rpc.quit()
meros.quit()
except NodeError:
if ress[-1] != crash:
ress.append(crash)
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion PythonTests/Vectors/Consensus/Verification/Competing.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion PythonTests/Vectors/Consensus/Verification/Parsable.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"blockchain": [{"transactions": [], "elements": [], "aggregate": "C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "header": {"version": 0, "last": "74E8778118160833C86ACC64952C9BC44F2E4AEA4BAB21A8C49A56EC22FC97AAF6EE9D0D2A2BD9B8204711A4DE30EC07", "contents": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "significant": 1, "sketchSalt": "00000000", "sketchCheck": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "miner": "95CCC3FEFF7E76B0C40F2790DB6988823D9B7A7819B241EED4187A0A4356441CE3342DF1911566A9E56CCA909D8A3AE7033AA53930B7C29A0137B3B36E24186776FC7E67CBE4C5EFFAAC61F52B9AF361594C42ED351CDE7624D949D520B71028", "time": 1200, "proof": 30, "signature": "A741CC8FCBAC767A3F680361729FC69E464708DDF009BBAF5043E89CD64BAE677EF144BE587FD8B4115A9B79761DD3E0"}, "hash": "FDFF5716D4C88705F2B17E5E4EE2F2F356324204E3EB1C5273F9DA86E86CA23CD88A3F4A9373C03A5838323F721DE965"}, {"transactions": [{"hash": "49E0C9ED8396782F3D464164E2385FE7DB38018063DE36738E83FB6B11A3015093872BF887C8981085CFE1674B081C10", "holders": [0]}], "elements": [], "aggregate": "98D5FDE32C4FC65F2FD182CEB6978E6EC5F3EC14220BE865A4967DBD1AA82FB67851CB4747609D32134464716174527A", "header": {"version": 0, "last": "FDFF5716D4C88705F2B17E5E4EE2F2F356324204E3EB1C5273F9DA86E86CA23CD88A3F4A9373C03A5838323F721DE965", "contents": "549D8BB691D77510453A0ECDB2F59C684160ACDC7E8948C99DABCEF06858D57EF4E082253966FDEE1725B360744BC978", "significant": 1, "sketchSalt": "00000000", "sketchCheck": "399B811189E47FB3C56942BE16BACE4D9A9BFE8FF84E0BFF054499C1A33723706161D00CAA172E46C9382F19C4F27415", "miner": 0, "time": 2400, "proof": 71, "signature": "B273C62F0FB4076E3685C48680C134E89ED258CF58AA225CBE02B52E3847175EB26FFC9E7937BC5DB6DECC202D1E1DFB"}, "hash": "FE8C5ED9FE639A4B1EB7EB2FCBB51D3D97CE639C2D7DBB8250F18FAFFCFE4A7C908A28B4AD5CFD704402C081F33DEFF5"}], "data": {"descendant": "Data", "inputs": [{"hash": "000000000000000000000000000000003B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"}], "outputs": [], "hash": "49E0C9ED8396782F3D464164E2385FE7DB38018063DE36738E83FB6B11A3015093872BF887C8981085CFE1674B081C10", "data": "", "signature": "02F39E94E5FC6C7E52956DBA2453DAF6073EDF055D115B8279BC060AFA21CA06627BC630F5848B5612C7CEA7F0B8E62A8054E8393DA1CFB1FDE420D5E090E20D", "proof": 4, "argon": "DE19CE192BCA6F72E13623D63FE60878F7A9E10F21C6ECCD9F2B092B1BF543A50EBDFFD73DD9ACC6FF8724D351444C7C"}}
{"blockchain": [{"transactions": [], "elements": [], "aggregate": "C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "header": {"version": 0, "last": "A381B334EE43CA7A7E6BC3836CDF090DE361A003699D2F1F5477674E700BF6B36856982776DA913E3BF13EB3CB6BDD1B", "contents": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "significant": 1, "sketchSalt": "00000000", "sketchCheck": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "miner": "95CCC3FEFF7E76B0C40F2790DB6988823D9B7A7819B241EED4187A0A4356441CE3342DF1911566A9E56CCA909D8A3AE7033AA53930B7C29A0137B3B36E24186776FC7E67CBE4C5EFFAAC61F52B9AF361594C42ED351CDE7624D949D520B71028", "time": 1200, "proof": 44, "signature": "8CF042324180498A5A6770D60067E14879A38780F99BC13C0090FE1EE9C43E7EBE7C980ABBA4896D580CBA53E3CA6F0F"}, "hash": "FDC5113BBCA00BE1353C487EE93BE44AC313F9729C2B7BE0229B6C828ABDE629DDF37BD1E2034C60E5E4E811586394D1"}, {"transactions": [{"hash": "49E0C9ED8396782F3D464164E2385FE7DB38018063DE36738E83FB6B11A3015093872BF887C8981085CFE1674B081C10", "holders": [0]}], "elements": [], "aggregate": "98D5FDE32C4FC65F2FD182CEB6978E6EC5F3EC14220BE865A4967DBD1AA82FB67851CB4747609D32134464716174527A", "header": {"version": 0, "last": "FDC5113BBCA00BE1353C487EE93BE44AC313F9729C2B7BE0229B6C828ABDE629DDF37BD1E2034C60E5E4E811586394D1", "contents": "549D8BB691D77510453A0ECDB2F59C684160ACDC7E8948C99DABCEF06858D57EF4E082253966FDEE1725B360744BC978", "significant": 1, "sketchSalt": "00000000", "sketchCheck": "399B811189E47FB3C56942BE16BACE4D9A9BFE8FF84E0BFF054499C1A33723706161D00CAA172E46C9382F19C4F27415", "miner": 0, "time": 2400, "proof": 67, "signature": "879BE3D8C5CA5C196FF54B012EDD27E0FD42ACAE8D74DAA5F40CA8FDE26FF49678AECE93C8CEC130EC682DEFA638B079"}, "hash": "FDA1F012FE22B8E96CF79D4EE172CA2FC6E977E4C10DC5494472896420FD30AA8D18B68FE3DF228869C2531776AE04CA"}], "data": {"descendant": "Data", "inputs": [{"hash": "000000000000000000000000000000003B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"}], "outputs": [], "hash": "49E0C9ED8396782F3D464164E2385FE7DB38018063DE36738E83FB6B11A3015093872BF887C8981085CFE1674B081C10", "data": "", "signature": "02F39E94E5FC6C7E52956DBA2453DAF6073EDF055D115B8279BC060AFA21CA06627BC630F5848B5612C7CEA7F0B8E62A8054E8393DA1CFB1FDE420D5E090E20D", "proof": 4, "argon": "DE19CE192BCA6F72E13623D63FE60878F7A9E10F21C6ECCD9F2B092B1BF543A50EBDFFD73DD9ACC6FF8724D351444C7C"}}
2 changes: 1 addition & 1 deletion PythonTests/Vectors/Merit/BlankBlocks.json

Large diffs are not rendered by default.

Loading

0 comments on commit f28b57f

Please sign in to comment.