Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 261c518133 | |||
| 2a1741ac52 | |||
| 1f59698bda | |||
| 3238964d2a | |||
| 85260f0281 | |||
| 8c944e469c | |||
| dae35d962a | |||
| 89fc132363 | |||
| af3cc3e902 | |||
| 4d8a30042e | |||
| a760b46501 | |||
| 9ef2d782c1 | |||
| 278654276e | |||
| 5ca2284583 |
+1
-1
@@ -31,7 +31,7 @@
|
|||||||
"-fno-exceptions -std=gnu11 -march=native -fPIC -DNDEBUG -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2"
|
"-fno-exceptions -std=gnu11 -march=native -fPIC -DNDEBUG -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2"
|
||||||
],
|
],
|
||||||
"cflags_cc": [
|
"cflags_cc": [
|
||||||
"-fexceptions -frtti -std=c++14 -march=native -fPIC -DNDEBUG -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2"
|
"-fexceptions -frtti -std=c++17 -march=native -fPIC -DNDEBUG -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2"
|
||||||
],
|
],
|
||||||
"xcode_settings": {
|
"xcode_settings": {
|
||||||
"OTHER_CFLAGS": [ "-fexceptions -frtti" ]
|
"OTHER_CFLAGS": [ "-fexceptions -frtti" ]
|
||||||
|
|||||||
@@ -221,8 +221,20 @@ module.exports.convertRtmBlob = function(blobBuffer) {
|
|||||||
return header;
|
return header;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.convertKcnBlob = function(blobBuffer) {
|
||||||
|
let header = blobBuffer.slice(0, 80);
|
||||||
|
update_merkle_root_hash(80, false, blobBuffer, header);
|
||||||
|
return header;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) {
|
module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) {
|
||||||
update_merkle_root_hash(80, true, blockTemplate, blockTemplate);
|
update_merkle_root_hash(80, true, blockTemplate, blockTemplate);
|
||||||
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
||||||
return blockTemplate;
|
return blockTemplate;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.constructNewKcnBlob = function(blockTemplate, nonceBuff) {
|
||||||
|
update_merkle_root_hash(80, false, blockTemplate, blockTemplate);
|
||||||
|
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
||||||
|
return blockTemplate;
|
||||||
|
};
|
||||||
|
|||||||
+2
-2
@@ -1,7 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cryptoforknote-util",
|
"name": "cryptoforknote-util",
|
||||||
"version": "15.0.1",
|
"version": "15.3.7",
|
||||||
"main": "cryptoforknote-util",
|
|
||||||
"author": {
|
"author": {
|
||||||
"name": "LucasJones",
|
"name": "LucasJones",
|
||||||
"email": "lucasjonesdev@hotmail.co.uk"
|
"email": "lucasjonesdev@hotmail.co.uk"
|
||||||
@@ -17,6 +16,7 @@
|
|||||||
"bignum": "^0.13.1",
|
"bignum": "^0.13.1",
|
||||||
"sha3": "*",
|
"sha3": "*",
|
||||||
"base58-native": "*",
|
"base58-native": "*",
|
||||||
|
"bech32": "*",
|
||||||
"varuint-bitcoin": "^1.0.4",
|
"varuint-bitcoin": "^1.0.4",
|
||||||
"merkle-lib": "^2.0.10",
|
"merkle-lib": "^2.0.10",
|
||||||
"bitcoinjs-lib": "git+https://github.com/MoneroOcean/bitcoinjs-lib.git"
|
"bitcoinjs-lib": "git+https://github.com/MoneroOcean/bitcoinjs-lib.git"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const bignum = require('bignum');
|
const bignum = require('bignum');
|
||||||
const base58 = require('base58-native');
|
const base58 = require('base58-native');
|
||||||
|
const bech32 = require('bech32');
|
||||||
const bitcoin = require('bitcoinjs-lib');
|
const bitcoin = require('bitcoinjs-lib');
|
||||||
|
|
||||||
const diff1 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
|
const diff1 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
|
||||||
@@ -148,9 +149,15 @@ function getTransactionBuffers(txs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addressToScript(addr) {
|
function addressToScript(addr) {
|
||||||
const decoded = base58.decode(addr);
|
let decoded;
|
||||||
if (decoded.length != 25) throw new Error('Invalid address length for ' + addr);
|
try {
|
||||||
if (!decoded) throw new Error('Base58 decode failed for ' + addr);
|
decoded = base58.decode(addr);
|
||||||
|
} catch(err) {}
|
||||||
|
if (!decoded || decoded.length != 25) {
|
||||||
|
const decoded2 = Buffer.from(bech32.bech32.fromWords(bech32.bech32.decode(addr).words.slice(1)));
|
||||||
|
if (decoded2.length != 20) throw new Error('Invalid address ' + addr);
|
||||||
|
return Buffer.concat([Buffer.from([0x0, 0x14]), decoded2]);
|
||||||
|
}
|
||||||
const pubkey = decoded.slice(1, -4);
|
const pubkey = decoded.slice(1, -4);
|
||||||
return Buffer.concat([Buffer.from([0x76, 0xa9, 0x14]), pubkey, Buffer.from([0x88, 0xac])]);
|
return Buffer.concat([Buffer.from([0x76, 0xa9, 0x14]), pubkey, Buffer.from([0x88, 0xac])]);
|
||||||
}
|
}
|
||||||
@@ -171,6 +178,12 @@ function generateOutputTransactions(rpcData, poolAddress) {
|
|||||||
let rewardToPool = reward;
|
let rewardToPool = reward;
|
||||||
let txOutputBuffers = [];
|
let txOutputBuffers = [];
|
||||||
|
|
||||||
|
if (rpcData.coinbasedevreward) {
|
||||||
|
const rewards = createOutputTransaction(rpcData.coinbasedevreward.value, rpcData.coinbasedevreward.address, rewardToPool, reward, txOutputBuffers, Buffer.from(rpcData.coinbasedevreward.scriptpubkey, 'hex'));
|
||||||
|
reward = rewards.reward;
|
||||||
|
rewardToPool = rewards.rewardToPool;
|
||||||
|
}
|
||||||
|
|
||||||
if (rpcData.smartnode) {
|
if (rpcData.smartnode) {
|
||||||
if (rpcData.smartnode.payee) {
|
if (rpcData.smartnode.payee) {
|
||||||
const rewards = createOutputTransaction(rpcData.smartnode.amount, rpcData.smartnode.payee, rewardToPool, reward, txOutputBuffers);
|
const rewards = createOutputTransaction(rpcData.smartnode.amount, rpcData.smartnode.payee, rewardToPool, reward, txOutputBuffers);
|
||||||
@@ -220,7 +233,7 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
|||||||
|
|
||||||
const scriptSigPart1 = Buffer.concat([
|
const scriptSigPart1 = Buffer.concat([
|
||||||
serializeNumber(rpcData.height),
|
serializeNumber(rpcData.height),
|
||||||
Buffer.from(rpcData.coinbaseaux.flags, 'hex'),
|
Buffer.from(rpcData.coinbaseaux.flags ? rpcData.coinbaseaux.flags : "", 'hex'),
|
||||||
serializeNumber(Date.now() / 1000 | 0),
|
serializeNumber(Date.now() / 1000 | 0),
|
||||||
Buffer.from([extraNoncePlaceholderLength])
|
Buffer.from([extraNoncePlaceholderLength])
|
||||||
]);
|
]);
|
||||||
@@ -244,11 +257,17 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
|||||||
// transaction output
|
// transaction output
|
||||||
generateOutputTransactions(rpcData, poolAddress),
|
generateOutputTransactions(rpcData, poolAddress),
|
||||||
// end transaction ouput
|
// end transaction ouput
|
||||||
packUInt32LE(0), // txLockTime
|
packUInt32LE(0) // txLockTime
|
||||||
varIntBuffer(rpcData.coinbase_payload.length / 2),
|
|
||||||
Buffer.from(rpcData.coinbase_payload, 'hex')
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if (rpcData.coinbase_payload) {
|
||||||
|
blob2 = Buffer.concat([
|
||||||
|
blob2,
|
||||||
|
varIntBuffer(rpcData.coinbase_payload.length / 2),
|
||||||
|
Buffer.from(rpcData.coinbase_payload, 'hex')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
const prev_hash = reverseBuffer(Buffer.from(rpcData.previousblockhash, 'hex')).toString('hex');
|
const prev_hash = reverseBuffer(Buffer.from(rpcData.previousblockhash, 'hex')).toString('hex');
|
||||||
const version = packInt32LE(rpcData.version).toString('hex');
|
const version = packInt32LE(rpcData.version).toString('hex');
|
||||||
const curtime = packUInt32LE(rpcData.curtime).toString('hex');
|
const curtime = packUInt32LE(rpcData.curtime).toString('hex');
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|||||||
@@ -35,4 +35,5 @@ enum BLOB_TYPE {
|
|||||||
BLOB_TYPE_CRYPTONOTE_XHV = 11, // Haven
|
BLOB_TYPE_CRYPTONOTE_XHV = 11, // Haven
|
||||||
BLOB_TYPE_CRYPTONOTE_XTA = 12, // ITALO
|
BLOB_TYPE_CRYPTONOTE_XTA = 12, // ITALO
|
||||||
BLOB_TYPE_CRYPTONOTE_ZEPHYR = 13, // ZEPHYR
|
BLOB_TYPE_CRYPTONOTE_ZEPHYR = 13, // ZEPHYR
|
||||||
|
BLOB_TYPE_CRYPTONOTE_XLA = 14, // XLA
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -935,6 +935,7 @@ namespace cryptonote
|
|||||||
crypto::cycle cycle;
|
crypto::cycle cycle;
|
||||||
crypto::cycle40 cycle40;
|
crypto::cycle40 cycle40;
|
||||||
crypto::cycle48 cycle48;
|
crypto::cycle48 cycle48;
|
||||||
|
crypto::signature signature;
|
||||||
|
|
||||||
BEGIN_SERIALIZE()
|
BEGIN_SERIALIZE()
|
||||||
VARINT_FIELD(major_version)
|
VARINT_FIELD(major_version)
|
||||||
@@ -956,7 +957,27 @@ namespace cryptonote
|
|||||||
if (blob_type == BLOB_TYPE_CRYPTONOTE_TUBE) FIELD(cycle40)
|
if (blob_type == BLOB_TYPE_CRYPTONOTE_TUBE) FIELD(cycle40)
|
||||||
if (blob_type == BLOB_TYPE_CRYPTONOTE_XTA) FIELD(cycle48)
|
if (blob_type == BLOB_TYPE_CRYPTONOTE_XTA) FIELD(cycle48)
|
||||||
if (blob_type == BLOB_TYPE_CRYPTONOTE_XHV) FIELD(pricing_record)
|
if (blob_type == BLOB_TYPE_CRYPTONOTE_XHV) FIELD(pricing_record)
|
||||||
if (blob_type == BLOB_TYPE_CRYPTONOTE_ZEPHYR) FIELD_N("pricing_record", zephyr_pricing_record)
|
if (blob_type == BLOB_TYPE_CRYPTONOTE_ZEPHYR) {
|
||||||
|
if (major_version >= 3)
|
||||||
|
{
|
||||||
|
FIELD_N("pricing_record", zephyr_pricing_record)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
zephyr_oracle::pricing_record_v1 pr_v1;
|
||||||
|
if (!typename Archive<W>::is_saving())
|
||||||
|
{
|
||||||
|
FIELD(pr_v1)
|
||||||
|
pr_v1.write_to_pr(zephyr_pricing_record);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pr_v1.read_from_pr(zephyr_pricing_record);
|
||||||
|
FIELD(pr_v1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (blob_type == BLOB_TYPE_CRYPTONOTE_XLA && major_version >= 13) FIELD(signature)
|
||||||
|
|
||||||
END_SERIALIZE()
|
END_SERIALIZE()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ template <>
|
|||||||
struct binary_archive<false> : public binary_archive_base<std::istream, false>
|
struct binary_archive<false> : public binary_archive_base<std::istream, false>
|
||||||
{
|
{
|
||||||
explicit binary_archive(stream_type &s) : base_type(s) {
|
explicit binary_archive(stream_type &s) : base_type(s) {
|
||||||
stream_type::streampos pos = stream_.tellg();
|
auto pos = stream_.tellg();
|
||||||
stream_.seekg(0, std::ios_base::end);
|
stream_.seekg(0, std::ios_base::end);
|
||||||
eof_pos_ = stream_.tellg();
|
eof_pos_ = stream_.tellg();
|
||||||
stream_.seekg(pos);
|
stream_.seekg(pos);
|
||||||
|
|||||||
@@ -40,16 +40,33 @@
|
|||||||
template <template <bool> class Archive>
|
template <template <bool> class Archive>
|
||||||
bool do_serialize(Archive<false> &ar, zephyr_oracle::pricing_record &pr, uint8_t version)
|
bool do_serialize(Archive<false> &ar, zephyr_oracle::pricing_record &pr, uint8_t version)
|
||||||
{
|
{
|
||||||
// very basic sanity check
|
if (version < 3)
|
||||||
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record)) {
|
{
|
||||||
ar.stream().setstate(std::ios::failbit);
|
// very basic sanity check
|
||||||
return false;
|
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record_v1)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
zephyr_oracle::pricing_record_v1 pr_v1;
|
||||||
|
ar.serialize_blob(&pr_v1, sizeof(zephyr_oracle::pricing_record_v1), "");
|
||||||
|
if (!ar.good())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!pr_v1.write_to_pr(pr))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// very basic sanity check
|
||||||
|
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
|
||||||
|
if (!ar.good())
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
|
|
||||||
if (!ar.stream().good())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +76,51 @@ bool do_serialize(Archive<true> &ar, zephyr_oracle::pricing_record &pr, uint8_t
|
|||||||
{
|
{
|
||||||
ar.begin_string();
|
ar.begin_string();
|
||||||
|
|
||||||
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
|
if (version < 3)
|
||||||
|
{
|
||||||
if (!ar.stream().good())
|
zephyr_oracle::pricing_record_v1 pr_v1;
|
||||||
|
if (!pr_v1.read_from_pr(pr))
|
||||||
|
return false;
|
||||||
|
ar.serialize_blob(&pr_v1, sizeof(zephyr_oracle::pricing_record_v1), "");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ar.good())
|
||||||
|
return false;
|
||||||
|
ar.end_string();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read
|
||||||
|
template <template <bool> class Archive>
|
||||||
|
bool do_serialize(Archive<false> &ar, zephyr_oracle::pricing_record_v1 &pr, uint8_t version)
|
||||||
|
{
|
||||||
|
// very basic sanity check
|
||||||
|
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record_v1)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record_v1), "");
|
||||||
|
if (!ar.good())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write
|
||||||
|
template <template <bool> class Archive>
|
||||||
|
bool do_serialize(Archive<true> &ar, zephyr_oracle::pricing_record_v1 &pr, uint8_t version)
|
||||||
|
{
|
||||||
|
ar.begin_string();
|
||||||
|
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record_v1), "");
|
||||||
|
if (!ar.good())
|
||||||
return false;
|
return false;
|
||||||
ar.end_string();
|
ar.end_string();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BLOB_SERIALIZER(zephyr_oracle::pricing_record);
|
BLOB_SERIALIZER(zephyr_oracle::pricing_record);
|
||||||
|
BLOB_SERIALIZER(zephyr_oracle::pricing_record_v1);
|
||||||
|
|||||||
@@ -40,22 +40,39 @@ namespace zephyr_oracle
|
|||||||
{
|
{
|
||||||
struct pr_serialized
|
struct pr_serialized
|
||||||
{
|
{
|
||||||
uint64_t zEPHUSD;
|
uint64_t spot;
|
||||||
uint64_t zEPHRSV;
|
uint64_t moving_average;
|
||||||
|
uint64_t stable;
|
||||||
|
uint64_t stable_ma;
|
||||||
|
uint64_t reserve;
|
||||||
|
uint64_t reserve_ma;
|
||||||
uint64_t timestamp;
|
uint64_t timestamp;
|
||||||
|
std::string signature;
|
||||||
|
|
||||||
BEGIN_KV_SERIALIZE_MAP()
|
BEGIN_KV_SERIALIZE_MAP()
|
||||||
KV_SERIALIZE(zEPHUSD)
|
KV_SERIALIZE(spot)
|
||||||
KV_SERIALIZE(zEPHRSV)
|
KV_SERIALIZE(moving_average)
|
||||||
|
KV_SERIALIZE(stable)
|
||||||
|
KV_SERIALIZE(stable_ma)
|
||||||
|
KV_SERIALIZE(reserve)
|
||||||
|
KV_SERIALIZE(reserve_ma)
|
||||||
KV_SERIALIZE(timestamp)
|
KV_SERIALIZE(timestamp)
|
||||||
|
KV_SERIALIZE(signature)
|
||||||
END_KV_SERIALIZE_MAP()
|
END_KV_SERIALIZE_MAP()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pricing_record::pricing_record() noexcept
|
pricing_record::pricing_record() noexcept
|
||||||
: zEPHUSD(0)
|
: spot(0)
|
||||||
, zEPHRSV(0)
|
, moving_average(0)
|
||||||
, timestamp(0) {}
|
, stable(0)
|
||||||
|
, stable_ma(0)
|
||||||
|
, reserve(0)
|
||||||
|
, reserve_ma(0)
|
||||||
|
, timestamp(0)
|
||||||
|
{
|
||||||
|
std::memset(signature, 0, sizeof(signature));
|
||||||
|
}
|
||||||
|
|
||||||
bool pricing_record::_load(epee::serialization::portable_storage& src, epee::serialization::section* hparent)
|
bool pricing_record::_load(epee::serialization::portable_storage& src, epee::serialization::section* hparent)
|
||||||
{
|
{
|
||||||
@@ -63,52 +80,71 @@ namespace zephyr_oracle
|
|||||||
if (in._load(src, hparent))
|
if (in._load(src, hparent))
|
||||||
{
|
{
|
||||||
// Copy everything into the local instance
|
// Copy everything into the local instance
|
||||||
zEPHUSD = in.zEPHUSD;
|
spot = in.spot;
|
||||||
zEPHRSV = in.zEPHRSV;
|
moving_average = in.moving_average;
|
||||||
|
stable = in.stable;
|
||||||
|
stable_ma = in.stable_ma;
|
||||||
|
reserve = in.reserve;
|
||||||
|
reserve_ma = in.reserve_ma;
|
||||||
timestamp = in.timestamp;
|
timestamp = in.timestamp;
|
||||||
|
for (unsigned int i = 0; i < in.signature.length(); i += 2) {
|
||||||
|
std::string byteString = in.signature.substr(i, 2);
|
||||||
|
signature[i>>1] = (char) strtol(byteString.c_str(), NULL, 16);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Report error here?
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pricing_record::store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const
|
bool pricing_record::store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const
|
||||||
{
|
{
|
||||||
const pr_serialized out{zEPHUSD,zEPHRSV,timestamp};
|
std::string sig_hex;
|
||||||
|
for (unsigned int i=0; i<64; i++) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << std::hex << std::setw(2) << std::setfill('0') << (0xff & signature[i]);
|
||||||
|
sig_hex += ss.str();
|
||||||
|
}
|
||||||
|
const pr_serialized out{spot,moving_average,stable,stable_ma,reserve,reserve_ma,timestamp,sig_hex};
|
||||||
return out.store(dest, hparent);
|
return out.store(dest, hparent);
|
||||||
}
|
}
|
||||||
|
|
||||||
pricing_record::pricing_record(const pricing_record& orig) noexcept
|
pricing_record::pricing_record(const pricing_record& orig) noexcept
|
||||||
: zEPHUSD(orig.zEPHUSD)
|
: spot(orig.spot)
|
||||||
, zEPHRSV(orig.zEPHRSV)
|
, moving_average(orig.moving_average)
|
||||||
, timestamp(orig.timestamp) {}
|
, stable(orig.stable)
|
||||||
|
, stable_ma(orig.stable_ma)
|
||||||
|
, reserve(orig.reserve)
|
||||||
|
, reserve_ma(orig.reserve_ma)
|
||||||
|
, timestamp(orig.timestamp)
|
||||||
|
{
|
||||||
|
std::memcpy(signature, orig.signature, sizeof(signature));
|
||||||
|
}
|
||||||
|
|
||||||
pricing_record& pricing_record::operator=(const pricing_record& orig) noexcept
|
pricing_record& pricing_record::operator=(const pricing_record& orig) noexcept
|
||||||
{
|
{
|
||||||
zEPHUSD = orig.zEPHUSD;
|
spot = orig.spot;
|
||||||
zEPHRSV = orig.zEPHRSV;
|
moving_average = orig.moving_average;
|
||||||
|
stable = orig.stable;
|
||||||
|
stable_ma = orig.stable_ma;
|
||||||
|
reserve = orig.reserve;
|
||||||
|
reserve_ma = orig.reserve_ma;
|
||||||
timestamp = orig.timestamp;
|
timestamp = orig.timestamp;
|
||||||
|
::memcpy(signature, orig.signature, sizeof(signature));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t pricing_record::operator[](const std::string& asset_type) const
|
|
||||||
{
|
|
||||||
if (asset_type == "ZEPH") {
|
|
||||||
return zEPHUSD; // ZEPH spot price
|
|
||||||
} else if (asset_type == "ZEPHUSD") {
|
|
||||||
return 1000000000000; // 1
|
|
||||||
} else if (asset_type == "ZEPHRSV") {
|
|
||||||
return zEPHRSV; // ZEPHRSV spot price
|
|
||||||
} else {
|
|
||||||
CHECK_AND_ASSERT_THROW_MES(false, "Asset type doesn't exist in pricing record!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool pricing_record::equal(const pricing_record& other) const noexcept
|
bool pricing_record::equal(const pricing_record& other) const noexcept
|
||||||
{
|
{
|
||||||
return ((zEPHUSD == other.zEPHUSD) &&
|
return ((spot == other.spot) &&
|
||||||
(zEPHRSV == other.zEPHRSV) &&
|
(moving_average == other.moving_average) &&
|
||||||
(timestamp == other.timestamp));
|
(stable == other.stable) &&
|
||||||
|
(stable_ma == other.stable_ma) &&
|
||||||
|
(reserve == other.reserve) &&
|
||||||
|
(reserve_ma == other.reserve_ma) &&
|
||||||
|
(timestamp == other.timestamp) &&
|
||||||
|
!::memcmp(signature, other.signature, sizeof(signature)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pricing_record::empty() const noexcept
|
bool pricing_record::empty() const noexcept
|
||||||
@@ -117,6 +153,69 @@ namespace zephyr_oracle
|
|||||||
return (*this).equal(empty_pr);
|
return (*this).equal(empty_pr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool pricing_record::verifySignature(const std::string& public_key) const
|
||||||
|
{
|
||||||
|
CHECK_AND_ASSERT_THROW_MES(!public_key.empty(), "Pricing record verification failed. NULL public key. PK Size: " << public_key.size()); // TODO: is this necessary or the one below already covers this case, meannin it will produce empty pubkey?
|
||||||
|
|
||||||
|
// extract the key
|
||||||
|
EVP_PKEY* pubkey;
|
||||||
|
BIO* bio = BIO_new_mem_buf(public_key.c_str(), public_key.size());
|
||||||
|
if (!bio) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pubkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
|
||||||
|
BIO_free(bio);
|
||||||
|
CHECK_AND_ASSERT_THROW_MES(pubkey != NULL, "Pricing record verification failed. NULL public key.");
|
||||||
|
|
||||||
|
// Convert our internal 64-byte binary representation into 128-byte hex string
|
||||||
|
std::string sig_hex;
|
||||||
|
for (unsigned int i=0; i<64; i++) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << std::hex << std::setw(2) << std::setfill('0') << (0xff & signature[i]);
|
||||||
|
sig_hex += ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the JSON string, so that we can verify the signature
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "{\"spot\":" << spot;
|
||||||
|
oss << ",\"moving_average\":" << moving_average;
|
||||||
|
oss << ",\"timestamp\":" << timestamp;
|
||||||
|
oss << "}";
|
||||||
|
std::string message = oss.str();
|
||||||
|
|
||||||
|
// Create a verify digest from the message
|
||||||
|
EVP_MD_CTX *ctx = EVP_MD_CTX_create();
|
||||||
|
int ret = 0;
|
||||||
|
if (ctx) {
|
||||||
|
ret = EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, pubkey);
|
||||||
|
if (ret == 1) {
|
||||||
|
ret = EVP_DigestVerifyUpdate(ctx, message.data(), message.length());
|
||||||
|
if (ret == 1) {
|
||||||
|
ret = EVP_DigestVerifyFinal(ctx, (const unsigned char *)signature, 64);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup the context we created
|
||||||
|
EVP_MD_CTX_destroy(ctx);
|
||||||
|
// Cleanup the openssl stuff
|
||||||
|
EVP_PKEY_free(pubkey);
|
||||||
|
|
||||||
|
if (ret == 1)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Get the errors from OpenSSL
|
||||||
|
// ERR_print_errors_fp (stderr);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pricing_record::has_missing_rates() const noexcept
|
||||||
|
{
|
||||||
|
return (spot == 0) || (moving_average == 0) || (stable == 0) || (stable_ma == 0) || (reserve == 0) || (reserve_ma == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// overload for pr validation for block
|
||||||
bool pricing_record::valid(uint32_t hf_version, uint64_t bl_timestamp, uint64_t last_bl_timestamp) const
|
bool pricing_record::valid(uint32_t hf_version, uint64_t bl_timestamp, uint64_t last_bl_timestamp) const
|
||||||
{
|
{
|
||||||
if (hf_version < 3) {
|
if (hf_version < 3) {
|
||||||
@@ -126,16 +225,29 @@ namespace zephyr_oracle
|
|||||||
|
|
||||||
if (this->empty())
|
if (this->empty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
if (this->has_missing_rates()) {
|
||||||
|
LOG_ERROR("Pricing record has missing rates.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string const MAINNET_ORACLE_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n"
|
||||||
|
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAO5hVuc6ylYMbj3WhqOMoAcJ0SD4e3zW\n"
|
||||||
|
"edsUmhQeYwBkelAaFyxhX4ZotP+b/cFr2mX5iuND1znEnMZkyg+YmtkCAwEAAQ==\n"
|
||||||
|
"-----END PUBLIC KEY-----\n";
|
||||||
|
|
||||||
|
if (!verifySignature(MAINNET_ORACLE_PUBLIC_KEY)) {
|
||||||
|
LOG_ERROR("Invalid pricing record signature.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// validate the timestmap
|
// validate the timestmap
|
||||||
if (this->timestamp > bl_timestamp + PRICING_RECORD_VALID_TIME_DIFF_FROM_BLOCK) {
|
if (this->timestamp > bl_timestamp + PRICING_RECORD_VALID_TIME_DIFF_FROM_BLOCK) {
|
||||||
LOG_ERROR("Pricing record timestamp is too far in the future.");
|
LOG_ERROR("Pricing record timestamp is too far in the future.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->timestamp <= last_bl_timestamp) {
|
||||||
|
|
||||||
if (this->timestamp <= last_bl_timestamp - PRICING_RECORD_VALID_TIME_DIFF_FROM_BLOCK) {
|
|
||||||
LOG_ERROR("Pricing record timestamp: " << this->timestamp << ", block timestamp: " << bl_timestamp);
|
LOG_ERROR("Pricing record timestamp: " << this->timestamp << ", block timestamp: " << bl_timestamp);
|
||||||
LOG_ERROR("Pricing record timestamp is too old.");
|
LOG_ERROR("Pricing record timestamp is too old.");
|
||||||
return false;
|
return false;
|
||||||
@@ -144,3 +256,4 @@ namespace zephyr_oracle
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,15 +57,28 @@ namespace epee
|
|||||||
|
|
||||||
namespace zephyr_oracle
|
namespace zephyr_oracle
|
||||||
{
|
{
|
||||||
|
#pragma pack(push, 1)
|
||||||
|
POD_CLASS pricing_record_pre {
|
||||||
|
uint64_t zEPHUSD;
|
||||||
|
uint64_t zEPHRSV;
|
||||||
|
uint64_t timestamp;
|
||||||
|
};
|
||||||
|
#pragma pack(pop)
|
||||||
class pricing_record
|
class pricing_record
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Fields
|
// Fields
|
||||||
uint64_t zEPHUSD;
|
uint64_t spot;
|
||||||
uint64_t zEPHRSV;
|
uint64_t moving_average;
|
||||||
|
uint64_t stable;
|
||||||
|
uint64_t stable_ma;
|
||||||
|
uint64_t reserve;
|
||||||
|
uint64_t reserve_ma;
|
||||||
uint64_t timestamp;
|
uint64_t timestamp;
|
||||||
|
unsigned char signature[64];
|
||||||
|
|
||||||
// Default c'tor
|
// Default c'tor
|
||||||
pricing_record() noexcept;
|
pricing_record() noexcept;
|
||||||
//! Load from epee p2p format
|
//! Load from epee p2p format
|
||||||
@@ -76,6 +89,8 @@ namespace zephyr_oracle
|
|||||||
~pricing_record() = default;
|
~pricing_record() = default;
|
||||||
bool equal(const pricing_record& other) const noexcept;
|
bool equal(const pricing_record& other) const noexcept;
|
||||||
bool empty() const noexcept;
|
bool empty() const noexcept;
|
||||||
|
bool verifySignature(const std::string& public_key) const;
|
||||||
|
bool has_missing_rates() const noexcept;
|
||||||
bool valid(uint32_t hf_version, uint64_t bl_timestamp, uint64_t last_bl_timestamp) const;
|
bool valid(uint32_t hf_version, uint64_t bl_timestamp, uint64_t last_bl_timestamp) const;
|
||||||
|
|
||||||
pricing_record& operator=(const pricing_record& orig) noexcept;
|
pricing_record& operator=(const pricing_record& orig) noexcept;
|
||||||
@@ -92,4 +107,34 @@ namespace zephyr_oracle
|
|||||||
return !a.equal(b);
|
return !a.equal(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class pricing_record_v1
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
uint64_t zEPHUSD;
|
||||||
|
uint64_t zEPHRSV;
|
||||||
|
uint64_t timestamp;
|
||||||
|
|
||||||
|
bool write_to_pr(zephyr_oracle::pricing_record &pr)
|
||||||
|
{
|
||||||
|
pr.spot = 0;
|
||||||
|
pr.moving_average = 0;
|
||||||
|
pr.stable = 0;
|
||||||
|
pr.stable_ma = 0;
|
||||||
|
pr.reserve = 0;
|
||||||
|
pr.reserve_ma = 0;
|
||||||
|
pr.timestamp = 0;
|
||||||
|
std::memset(pr.signature, 0, sizeof(zephyr_oracle::pricing_record::signature));
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool read_from_pr(zephyr_oracle::pricing_record &pr)
|
||||||
|
{
|
||||||
|
zEPHUSD = 0;
|
||||||
|
zEPHRSV = 0;
|
||||||
|
timestamp = 0;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
} // oracle
|
} // oracle
|
||||||
|
|||||||
Reference in New Issue
Block a user