Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a9f2317ffa | |||
| eea6d166b2 | |||
| 6bb5e00c17 | |||
| 7bbb0cf80e | |||
| 16f9569d0c | |||
| e6143eb9c0 | |||
| 1b2f6af8f8 | |||
| eb61aefe8b | |||
| 1d0ada1c82 | |||
| 0bb1785826 | |||
| 1c48ad7e46 | |||
| f0c26e6d5b | |||
| 7a1d7271a1 | |||
| 30e051fa46 |
@@ -64,22 +64,14 @@ function transaction_hash3(transaction, forWitness) {
|
|||||||
return hash256_3(transaction.__toBuffer(undefined, undefined, forWitness));
|
return hash256_3(transaction.__toBuffer(undefined, undefined, forWitness));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMerkleRoot(transactions) {
|
function getMerkleRoot(transactions, transaction_hash_func, detectWitness) {
|
||||||
if (transactions.length === 0) return Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
if (transactions.length === 0) return Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
||||||
const forWitness = txesHaveWitnessCommit(transactions);
|
const forWitness = detectWitness ? txesHaveWitnessCommit(transactions) : false;
|
||||||
const hashes = transactions.map(transaction => transaction_hash(transaction, forWitness));
|
const hashes = transactions.map(transaction => transaction_hash_func(transaction, forWitness));
|
||||||
const rootHash = fastMerkleRoot(hashes, hash256);
|
const rootHash = fastMerkleRoot(hashes, hash256);
|
||||||
return forWitness ? hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash;
|
return forWitness ? hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMerkleRoot3(transactions) {
|
|
||||||
if (transactions.length === 0) return Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
|
||||||
const forWitness = txesHaveWitnessCommit(transactions);
|
|
||||||
const hashes = transactions.map(transaction => transaction_hash3(transaction, forWitness));
|
|
||||||
const rootHash = fastMerkleRoot(hashes, hash256_3);
|
|
||||||
return forWitness ? hash256_3(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
let last_epoch_number;
|
let last_epoch_number;
|
||||||
let last_seed_hash;
|
let last_seed_hash;
|
||||||
|
|
||||||
@@ -183,7 +175,7 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function update_merkle_root_hash(offset, payload, blob_in, blob_out) {
|
function update_merkle_root_hash(offset, payload, blob_in, blob_out, transaction_hash_func, detectWitness) {
|
||||||
const nTransactions = varuint.decode(blob_in, offset);
|
const nTransactions = varuint.decode(blob_in, offset);
|
||||||
offset += varuint.decode.bytes;
|
offset += varuint.decode.bytes;
|
||||||
let transactions = [];
|
let transactions = [];
|
||||||
@@ -192,19 +184,7 @@ function update_merkle_root_hash(offset, payload, blob_in, blob_out) {
|
|||||||
transactions.push(tx);
|
transactions.push(tx);
|
||||||
offset += tx.byteLength();
|
offset += tx.byteLength();
|
||||||
}
|
}
|
||||||
getMerkleRoot(transactions).copy(blob_out, 4 + 32);
|
getMerkleRoot(transactions, transaction_hash_func, detectWitness).copy(blob_out, 4 + 32);
|
||||||
};
|
|
||||||
|
|
||||||
function update_merkle_root_hash3(offset, payload, blob_in, blob_out) {
|
|
||||||
const nTransactions = varuint.decode(blob_in, offset);
|
|
||||||
offset += varuint.decode.bytes;
|
|
||||||
let transactions = [];
|
|
||||||
for (let i = 0; i < nTransactions; ++i) {
|
|
||||||
const tx = bitcoin.Transaction.fromBuffer(blob_in.slice(offset), true, payload && i == 0);
|
|
||||||
transactions.push(tx);
|
|
||||||
offset += tx.byteLength();
|
|
||||||
}
|
|
||||||
getMerkleRoot3(transactions).copy(blob_out, 4 + 32);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.blockHashBuff = function(blobBuffer) {
|
module.exports.blockHashBuff = function(blobBuffer) {
|
||||||
@@ -217,12 +197,12 @@ module.exports.blockHashBuff3 = function(blobBuffer) {
|
|||||||
|
|
||||||
module.exports.convertRavenBlob = function(blobBuffer) {
|
module.exports.convertRavenBlob = function(blobBuffer) {
|
||||||
let header = blobBuffer.slice(0, 80);
|
let header = blobBuffer.slice(0, 80);
|
||||||
update_merkle_root_hash(80 + 8 + 32, false, blobBuffer, header);
|
update_merkle_root_hash(80 + 8 + 32, false, blobBuffer, header, transaction_hash, true);
|
||||||
return module.exports.blockHashBuff(header);
|
return module.exports.blockHashBuff(header);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
|
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
|
||||||
update_merkle_root_hash(80 + 8 + 32, false, blockTemplate, blockTemplate);
|
update_merkle_root_hash(80 + 8 + 32, false, blockTemplate, blockTemplate, transaction_hash, true);
|
||||||
nonceBuff.copy (blockTemplate, 80, 0, 8);
|
nonceBuff.copy (blockTemplate, 80, 0, 8);
|
||||||
mixhashBuff.copy(blockTemplate, 88, 0, 32);
|
mixhashBuff.copy(blockTemplate, 88, 0, 32);
|
||||||
return blockTemplate;
|
return blockTemplate;
|
||||||
@@ -259,24 +239,24 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
|||||||
|
|
||||||
module.exports.convertRtmBlob = function(blobBuffer) {
|
module.exports.convertRtmBlob = function(blobBuffer) {
|
||||||
let header = blobBuffer.slice(0, 80);
|
let header = blobBuffer.slice(0, 80);
|
||||||
update_merkle_root_hash(80, true, blobBuffer, header);
|
update_merkle_root_hash(80, true, blobBuffer, header, transaction_hash, true);
|
||||||
return header;
|
return header;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.convertKcnBlob = function(blobBuffer) {
|
module.exports.convertKcnBlob = function(blobBuffer) {
|
||||||
let header = blobBuffer.slice(0, 80);
|
let header = blobBuffer.slice(0, 80);
|
||||||
update_merkle_root_hash3(80, false, blobBuffer, header);
|
update_merkle_root_hash(80, false, blobBuffer, header, transaction_hash3, false);
|
||||||
return 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, transaction_hash, true);
|
||||||
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
||||||
return blockTemplate;
|
return blockTemplate;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.constructNewKcnBlob = function(blockTemplate, nonceBuff) {
|
module.exports.constructNewKcnBlob = function(blockTemplate, nonceBuff) {
|
||||||
update_merkle_root_hash(80, false, blockTemplate, blockTemplate);
|
update_merkle_root_hash(80, false, blockTemplate, blockTemplate, transaction_hash3, false);
|
||||||
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
nonceBuff.copy(blockTemplate, 76, 0, 4);
|
||||||
return blockTemplate;
|
return blockTemplate;
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cryptoforknote-util",
|
"name": "cryptoforknote-util",
|
||||||
"version": "15.3.9",
|
"version": "15.4.0",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "LucasJones",
|
"name": "LucasJones",
|
||||||
"email": "lucasjonesdev@hotmail.co.uk"
|
"email": "lucasjonesdev@hotmail.co.uk"
|
||||||
|
|||||||
@@ -173,13 +173,13 @@ function createTransactionOutput(amount, payee, rewardToPool, reward, txOutputBu
|
|||||||
return { reward: reward - amount, rewardToPool: rewardToPool - amount };
|
return { reward: reward - amount, rewardToPool: rewardToPool - amount };
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateTransactionOutputs(rpcData, poolAddress, is_witness) {
|
function generateTransactionOutputs(rpcData, poolAddress) {
|
||||||
let reward = rpcData.coinbasevalue;
|
let reward = rpcData.coinbasevalue + (rpcData.coinbasedevreward ? rpcData.coinbasedevreward.value : 0);
|
||||||
let rewardToPool = reward;
|
let rewardToPool = reward;
|
||||||
let txOutputBuffers = [];
|
let txOutputBuffers = [];
|
||||||
|
|
||||||
if (rpcData.coinbasedevreward) {
|
if (rpcData.coinbasedevreward) {
|
||||||
const rewards = createTransactionOutput(rpcData.coinbasedevreward.value, rpcData.coinbasedevreward.address, rewardToPool, reward, txOutputBuffers, Buffer.from(rpcData.coinbasedevreward.scriptpubkey, 'hex'));
|
const rewards = createTransactionOutput(rpcData.coinbasedevreward.value, null, rewardToPool, reward, txOutputBuffers, Buffer.from(rpcData.coinbasedevreward.scriptpubkey, 'hex'));
|
||||||
reward = rewards.reward;
|
reward = rewards.reward;
|
||||||
rewardToPool = rewards.rewardToPool;
|
rewardToPool = rewards.rewardToPool;
|
||||||
}
|
}
|
||||||
@@ -215,21 +215,21 @@ function generateTransactionOutputs(rpcData, poolAddress, is_witness) {
|
|||||||
|
|
||||||
createTransactionOutput(rewardToPool, null, rewardToPool, reward, txOutputBuffers, Buffer.from(addressToScript(poolAddress), "hex"));
|
createTransactionOutput(rewardToPool, null, rewardToPool, reward, txOutputBuffers, Buffer.from(addressToScript(poolAddress), "hex"));
|
||||||
|
|
||||||
if (is_witness) {
|
if (rpcData.default_witness_commitment) {
|
||||||
const witness_commitment = Buffer.from(rpcData.default_witness_commitment, 'hex');
|
createTransactionOutput(0, null, rewardToPool, reward, txOutputBuffers, Buffer.from(rpcData.default_witness_commitment, 'hex'));
|
||||||
txOutputBuffers.push(Buffer.concat([
|
txOutputBuffers.push(Buffer.concat([
|
||||||
varIntBuffer(1),
|
varIntBuffer(1),
|
||||||
varIntBuffer(witness_commitment.length),
|
varIntBuffer(32),
|
||||||
witness_commitment
|
Buffer.alloc(32, 0)
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Buffer.concat([ varIntBuffer(is_witness ? txOutputBuffers.length - 1 : txOutputBuffers.length), Buffer.concat(txOutputBuffers)]);
|
return Buffer.concat([ varIntBuffer(rpcData.default_witness_commitment ? txOutputBuffers.length - 1 : txOutputBuffers.length), Buffer.concat(txOutputBuffers)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
||||||
const extraNoncePlaceholderLength = 17;
|
const extraNoncePlaceholderLength = 17;
|
||||||
const coinbaseVersion = Buffer.concat([packUInt16LE(3), packUInt16LE(5)]);
|
const coinbaseVersion = rpcData.coinbasedevreward ? Buffer.concat([packUInt16LE(1), packUInt16LE(0)]) : Buffer.concat([packUInt16LE(3), packUInt16LE(5)]);
|
||||||
|
|
||||||
const scriptSigPart1 = Buffer.concat([
|
const scriptSigPart1 = Buffer.concat([
|
||||||
serializeNumber(rpcData.height),
|
serializeNumber(rpcData.height),
|
||||||
@@ -240,7 +240,7 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
|
|||||||
|
|
||||||
const scriptSigPart2 = serializeString('/nodeStratum/');
|
const scriptSigPart2 = serializeString('/nodeStratum/');
|
||||||
|
|
||||||
const is_witness = false; //rpcData.default_witness_commitment !== undefined;
|
const is_witness = rpcData.default_witness_commitment !== undefined;
|
||||||
|
|
||||||
const blob1 = Buffer.concat([
|
const blob1 = Buffer.concat([
|
||||||
coinbaseVersion,
|
coinbaseVersion,
|
||||||
|
|||||||
@@ -958,10 +958,24 @@ namespace cryptonote
|
|||||||
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) {
|
if (blob_type == BLOB_TYPE_CRYPTONOTE_ZEPHYR) {
|
||||||
if (major_version >= 3)
|
if (major_version >= 4)
|
||||||
{
|
{
|
||||||
FIELD_N("pricing_record", zephyr_pricing_record)
|
FIELD_N("pricing_record", zephyr_pricing_record)
|
||||||
}
|
}
|
||||||
|
else if (major_version >= 3)
|
||||||
|
{
|
||||||
|
zephyr_oracle::pricing_record_v2 pr_v2;
|
||||||
|
if (!typename Archive<W>::is_saving())
|
||||||
|
{
|
||||||
|
FIELD(pr_v2)
|
||||||
|
pr_v2.write_to_pr(zephyr_pricing_record);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pr_v2.read_from_pr(zephyr_pricing_record);
|
||||||
|
FIELD(pr_v2)
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
zephyr_oracle::pricing_record_v1 pr_v1;
|
zephyr_oracle::pricing_record_v1 pr_v1;
|
||||||
|
|||||||
@@ -40,7 +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)
|
||||||
{
|
{
|
||||||
if (version < 3)
|
if (version >= 4)
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
else if (version >= 3)
|
||||||
|
{
|
||||||
|
// very basic sanity check
|
||||||
|
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record_v2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
zephyr_oracle::pricing_record_v2 pr_v2;
|
||||||
|
ar.serialize_blob(&pr_v2, sizeof(zephyr_oracle::pricing_record_v2), "");
|
||||||
|
if (!ar.good())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!pr_v2.write_to_pr(pr))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
// very basic sanity check
|
// very basic sanity check
|
||||||
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record_v1)) {
|
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record_v1)) {
|
||||||
@@ -55,17 +81,6 @@ bool do_serialize(Archive<false> &ar, zephyr_oracle::pricing_record &pr, uint8_t
|
|||||||
if (!pr_v1.write_to_pr(pr))
|
if (!pr_v1.write_to_pr(pr))
|
||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -76,17 +91,24 @@ bool do_serialize(Archive<true> &ar, zephyr_oracle::pricing_record &pr, uint8_t
|
|||||||
{
|
{
|
||||||
ar.begin_string();
|
ar.begin_string();
|
||||||
|
|
||||||
if (version < 3)
|
if (version >= 4)
|
||||||
|
{
|
||||||
|
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
|
||||||
|
}
|
||||||
|
else if (version >= 3)
|
||||||
|
{
|
||||||
|
zephyr_oracle::pricing_record_v2 pr_v2;
|
||||||
|
if (!pr_v2.read_from_pr(pr))
|
||||||
|
return false;
|
||||||
|
ar.serialize_blob(&pr_v2, sizeof(zephyr_oracle::pricing_record_v2), "");
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
zephyr_oracle::pricing_record_v1 pr_v1;
|
zephyr_oracle::pricing_record_v1 pr_v1;
|
||||||
if (!pr_v1.read_from_pr(pr))
|
if (!pr_v1.read_from_pr(pr))
|
||||||
return false;
|
return false;
|
||||||
ar.serialize_blob(&pr_v1, sizeof(zephyr_oracle::pricing_record_v1), "");
|
ar.serialize_blob(&pr_v1, sizeof(zephyr_oracle::pricing_record_v1), "");
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ar.good())
|
if (!ar.good())
|
||||||
return false;
|
return false;
|
||||||
@@ -122,5 +144,34 @@ bool do_serialize(Archive<true> &ar, zephyr_oracle::pricing_record_v1 &pr, uint8
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read
|
||||||
|
template <template <bool> class Archive>
|
||||||
|
bool do_serialize(Archive<false> &ar, zephyr_oracle::pricing_record_v2 &pr, uint8_t version)
|
||||||
|
{
|
||||||
|
// very basic sanity check
|
||||||
|
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record_v2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record_v2), "");
|
||||||
|
if (!ar.good())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write
|
||||||
|
template <template <bool> class Archive>
|
||||||
|
bool do_serialize(Archive<true> &ar, zephyr_oracle::pricing_record_v2 &pr, uint8_t version)
|
||||||
|
{
|
||||||
|
ar.begin_string();
|
||||||
|
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record_v2), "");
|
||||||
|
if (!ar.good())
|
||||||
|
return false;
|
||||||
|
ar.end_string();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
BLOB_SERIALIZER(zephyr_oracle::pricing_record);
|
BLOB_SERIALIZER(zephyr_oracle::pricing_record);
|
||||||
BLOB_SERIALIZER(zephyr_oracle::pricing_record_v1);
|
BLOB_SERIALIZER(zephyr_oracle::pricing_record_v1);
|
||||||
|
BLOB_SERIALIZER(zephyr_oracle::pricing_record_v2);
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ namespace zephyr_oracle
|
|||||||
uint64_t stable_ma;
|
uint64_t stable_ma;
|
||||||
uint64_t reserve;
|
uint64_t reserve;
|
||||||
uint64_t reserve_ma;
|
uint64_t reserve_ma;
|
||||||
|
uint64_t reserve_ratio;
|
||||||
|
uint64_t reserve_ratio_ma;
|
||||||
uint64_t timestamp;
|
uint64_t timestamp;
|
||||||
std::string signature;
|
std::string signature;
|
||||||
|
|
||||||
@@ -56,6 +58,8 @@ namespace zephyr_oracle
|
|||||||
KV_SERIALIZE(stable_ma)
|
KV_SERIALIZE(stable_ma)
|
||||||
KV_SERIALIZE(reserve)
|
KV_SERIALIZE(reserve)
|
||||||
KV_SERIALIZE(reserve_ma)
|
KV_SERIALIZE(reserve_ma)
|
||||||
|
KV_SERIALIZE(reserve_ratio)
|
||||||
|
KV_SERIALIZE(reserve_ratio_ma)
|
||||||
KV_SERIALIZE(timestamp)
|
KV_SERIALIZE(timestamp)
|
||||||
KV_SERIALIZE(signature)
|
KV_SERIALIZE(signature)
|
||||||
END_KV_SERIALIZE_MAP()
|
END_KV_SERIALIZE_MAP()
|
||||||
@@ -69,6 +73,8 @@ namespace zephyr_oracle
|
|||||||
, stable_ma(0)
|
, stable_ma(0)
|
||||||
, reserve(0)
|
, reserve(0)
|
||||||
, reserve_ma(0)
|
, reserve_ma(0)
|
||||||
|
, reserve_ratio(0)
|
||||||
|
, reserve_ratio_ma(0)
|
||||||
, timestamp(0)
|
, timestamp(0)
|
||||||
{
|
{
|
||||||
std::memset(signature, 0, sizeof(signature));
|
std::memset(signature, 0, sizeof(signature));
|
||||||
@@ -86,6 +92,8 @@ namespace zephyr_oracle
|
|||||||
stable_ma = in.stable_ma;
|
stable_ma = in.stable_ma;
|
||||||
reserve = in.reserve;
|
reserve = in.reserve;
|
||||||
reserve_ma = in.reserve_ma;
|
reserve_ma = in.reserve_ma;
|
||||||
|
reserve_ratio = in.reserve_ratio;
|
||||||
|
reserve_ratio_ma = in.reserve_ratio_ma;
|
||||||
timestamp = in.timestamp;
|
timestamp = in.timestamp;
|
||||||
for (unsigned int i = 0; i < in.signature.length(); i += 2) {
|
for (unsigned int i = 0; i < in.signature.length(); i += 2) {
|
||||||
std::string byteString = in.signature.substr(i, 2);
|
std::string byteString = in.signature.substr(i, 2);
|
||||||
@@ -106,7 +114,7 @@ namespace zephyr_oracle
|
|||||||
ss << std::hex << std::setw(2) << std::setfill('0') << (0xff & signature[i]);
|
ss << std::hex << std::setw(2) << std::setfill('0') << (0xff & signature[i]);
|
||||||
sig_hex += ss.str();
|
sig_hex += ss.str();
|
||||||
}
|
}
|
||||||
const pr_serialized out{spot,moving_average,stable,stable_ma,reserve,reserve_ma,timestamp,sig_hex};
|
const pr_serialized out{spot,moving_average,stable,stable_ma,reserve,reserve_ma,reserve_ratio,reserve_ratio_ma,timestamp,sig_hex};
|
||||||
return out.store(dest, hparent);
|
return out.store(dest, hparent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,6 +125,8 @@ namespace zephyr_oracle
|
|||||||
, stable_ma(orig.stable_ma)
|
, stable_ma(orig.stable_ma)
|
||||||
, reserve(orig.reserve)
|
, reserve(orig.reserve)
|
||||||
, reserve_ma(orig.reserve_ma)
|
, reserve_ma(orig.reserve_ma)
|
||||||
|
, reserve_ratio(orig.reserve_ratio)
|
||||||
|
, reserve_ratio_ma(orig.reserve_ratio_ma)
|
||||||
, timestamp(orig.timestamp)
|
, timestamp(orig.timestamp)
|
||||||
{
|
{
|
||||||
std::memcpy(signature, orig.signature, sizeof(signature));
|
std::memcpy(signature, orig.signature, sizeof(signature));
|
||||||
@@ -130,6 +140,8 @@ namespace zephyr_oracle
|
|||||||
stable_ma = orig.stable_ma;
|
stable_ma = orig.stable_ma;
|
||||||
reserve = orig.reserve;
|
reserve = orig.reserve;
|
||||||
reserve_ma = orig.reserve_ma;
|
reserve_ma = orig.reserve_ma;
|
||||||
|
reserve_ratio = orig.reserve_ratio;
|
||||||
|
reserve_ratio_ma = orig.reserve_ratio_ma;
|
||||||
timestamp = orig.timestamp;
|
timestamp = orig.timestamp;
|
||||||
::memcpy(signature, orig.signature, sizeof(signature));
|
::memcpy(signature, orig.signature, sizeof(signature));
|
||||||
return *this;
|
return *this;
|
||||||
@@ -143,6 +155,8 @@ namespace zephyr_oracle
|
|||||||
(stable_ma == other.stable_ma) &&
|
(stable_ma == other.stable_ma) &&
|
||||||
(reserve == other.reserve) &&
|
(reserve == other.reserve) &&
|
||||||
(reserve_ma == other.reserve_ma) &&
|
(reserve_ma == other.reserve_ma) &&
|
||||||
|
(reserve_ratio == other.reserve_ratio) &&
|
||||||
|
(reserve_ratio_ma == other.reserve_ratio_ma) &&
|
||||||
(timestamp == other.timestamp) &&
|
(timestamp == other.timestamp) &&
|
||||||
!::memcmp(signature, other.signature, sizeof(signature)));
|
!::memcmp(signature, other.signature, sizeof(signature)));
|
||||||
}
|
}
|
||||||
@@ -153,7 +167,7 @@ namespace zephyr_oracle
|
|||||||
return (*this).equal(empty_pr);
|
return (*this).equal(empty_pr);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pricing_record::verifySignature(const std::string& public_key) const
|
bool pricing_record::verifySignature(const std::string& public_key, const uint8_t hf_version) 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?
|
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?
|
||||||
|
|
||||||
@@ -178,7 +192,9 @@ namespace zephyr_oracle
|
|||||||
// Build the JSON string, so that we can verify the signature
|
// Build the JSON string, so that we can verify the signature
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << "{\"spot\":" << spot;
|
oss << "{\"spot\":" << spot;
|
||||||
oss << ",\"moving_average\":" << moving_average;
|
if (hf_version <= 4) {
|
||||||
|
oss << ",\"moving_average\":" << moving_average;
|
||||||
|
}
|
||||||
oss << ",\"timestamp\":" << timestamp;
|
oss << ",\"timestamp\":" << timestamp;
|
||||||
oss << "}";
|
oss << "}";
|
||||||
std::string message = oss.str();
|
std::string message = oss.str();
|
||||||
@@ -210,9 +226,24 @@ namespace zephyr_oracle
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pricing_record::has_missing_rates() const noexcept
|
bool pricing_record::has_missing_rates(const uint8_t hf_version) const noexcept
|
||||||
{
|
{
|
||||||
return (spot == 0) || (moving_average == 0) || (stable == 0) || (stable_ma == 0) || (reserve == 0) || (reserve_ma == 0);
|
bool missing_rates = (spot == 0) || (moving_average == 0) || (stable == 0) || (stable_ma == 0) || (reserve == 0) || (reserve_ma == 0);
|
||||||
|
if (hf_version <= 3) {
|
||||||
|
return missing_rates;
|
||||||
|
} else if (hf_version <= 4) {
|
||||||
|
return missing_rates || (reserve_ratio == 0);
|
||||||
|
}
|
||||||
|
return missing_rates || (reserve_ratio == 0) || (reserve_ratio_ma == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pricing_record::has_essential_rates(const uint8_t hf_version) const noexcept
|
||||||
|
{
|
||||||
|
bool essential_rates = (spot != 0) && (stable != 0) && (reserve != 0);
|
||||||
|
if (hf_version <= 3) {
|
||||||
|
return essential_rates;
|
||||||
|
}
|
||||||
|
return essential_rates && (reserve_ratio != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// overload for pr validation for block
|
// overload for pr validation for block
|
||||||
@@ -226,9 +257,11 @@ namespace zephyr_oracle
|
|||||||
if (this->empty())
|
if (this->empty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (this->has_missing_rates()) {
|
if (this->has_missing_rates(hf_version)) {
|
||||||
LOG_ERROR("Pricing record has missing rates.");
|
if (hf_version < 4 || !this->has_essential_rates(hf_version)) {
|
||||||
return false;
|
LOG_ERROR("Pricing record has missing rates.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string const MAINNET_ORACLE_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n"
|
std::string const MAINNET_ORACLE_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n"
|
||||||
@@ -236,7 +269,7 @@ namespace zephyr_oracle
|
|||||||
"edsUmhQeYwBkelAaFyxhX4ZotP+b/cFr2mX5iuND1znEnMZkyg+YmtkCAwEAAQ==\n"
|
"edsUmhQeYwBkelAaFyxhX4ZotP+b/cFr2mX5iuND1znEnMZkyg+YmtkCAwEAAQ==\n"
|
||||||
"-----END PUBLIC KEY-----\n";
|
"-----END PUBLIC KEY-----\n";
|
||||||
|
|
||||||
if (!verifySignature(MAINNET_ORACLE_PUBLIC_KEY)) {
|
if (!verifySignature(MAINNET_ORACLE_PUBLIC_KEY, hf_version)) {
|
||||||
LOG_ERROR("Invalid pricing record signature.");
|
LOG_ERROR("Invalid pricing record signature.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,16 @@ namespace zephyr_oracle
|
|||||||
uint64_t zEPHRSV;
|
uint64_t zEPHRSV;
|
||||||
uint64_t timestamp;
|
uint64_t timestamp;
|
||||||
};
|
};
|
||||||
|
POD_CLASS pricing_record_pre_v2 {
|
||||||
|
uint64_t spot;
|
||||||
|
uint64_t moving_average;
|
||||||
|
uint64_t stable;
|
||||||
|
uint64_t stable_ma;
|
||||||
|
uint64_t reserve;
|
||||||
|
uint64_t reserve_ma;
|
||||||
|
uint64_t timestamp;
|
||||||
|
unsigned char signature[64];
|
||||||
|
};
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
class pricing_record
|
class pricing_record
|
||||||
{
|
{
|
||||||
@@ -76,6 +86,8 @@ namespace zephyr_oracle
|
|||||||
uint64_t stable_ma;
|
uint64_t stable_ma;
|
||||||
uint64_t reserve;
|
uint64_t reserve;
|
||||||
uint64_t reserve_ma;
|
uint64_t reserve_ma;
|
||||||
|
uint64_t reserve_ratio;
|
||||||
|
uint64_t reserve_ratio_ma;
|
||||||
uint64_t timestamp;
|
uint64_t timestamp;
|
||||||
unsigned char signature[64];
|
unsigned char signature[64];
|
||||||
|
|
||||||
@@ -89,8 +101,9 @@ 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 verifySignature(const std::string& public_key, const uint8_t hf_version) const;
|
||||||
bool has_missing_rates() const noexcept;
|
bool has_missing_rates(const uint8_t hf_version) const noexcept;
|
||||||
|
bool has_essential_rates(const uint8_t hf_version) 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;
|
||||||
@@ -137,4 +150,46 @@ namespace zephyr_oracle
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class pricing_record_v2
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
uint64_t spot;
|
||||||
|
uint64_t moving_average;
|
||||||
|
uint64_t stable;
|
||||||
|
uint64_t stable_ma;
|
||||||
|
uint64_t reserve;
|
||||||
|
uint64_t reserve_ma;
|
||||||
|
uint64_t timestamp;
|
||||||
|
unsigned char signature[64];
|
||||||
|
|
||||||
|
bool write_to_pr(zephyr_oracle::pricing_record &pr)
|
||||||
|
{
|
||||||
|
pr.spot = spot;
|
||||||
|
pr.moving_average = moving_average;
|
||||||
|
pr.stable = stable;
|
||||||
|
pr.stable_ma = stable_ma;
|
||||||
|
pr.reserve = reserve;
|
||||||
|
pr.reserve_ma = reserve_ma;
|
||||||
|
pr.reserve_ratio = 0;
|
||||||
|
pr.reserve_ratio_ma = 0;
|
||||||
|
pr.timestamp = timestamp;
|
||||||
|
std::memcpy(pr.signature, signature, sizeof(pr.signature));
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool read_from_pr(zephyr_oracle::pricing_record &pr)
|
||||||
|
{
|
||||||
|
spot = pr.spot;
|
||||||
|
moving_average = pr.moving_average;
|
||||||
|
stable = pr.stable;
|
||||||
|
stable_ma = pr.stable_ma;
|
||||||
|
reserve = pr.reserve;
|
||||||
|
reserve_ma = pr.reserve_ma;
|
||||||
|
timestamp = pr.timestamp;
|
||||||
|
std::memcpy(signature, pr.signature, sizeof(signature));
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
} // oracle
|
} // oracle
|
||||||
|
|||||||
Reference in New Issue
Block a user