Added ZEPH fork support

This commit is contained in:
MoneroOcean
2023-10-01 14:24:39 +00:00
parent 5ca2284583
commit 278654276e
5 changed files with 283 additions and 50 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "cryptoforknote-util",
"version": "15.1.0",
"version": "15.2.0",
"main": "cryptoforknote-util",
"author": {
"name": "LucasJones",
+20 -1
View File
@@ -957,7 +957,26 @@ namespace cryptonote
if (blob_type == BLOB_TYPE_CRYPTONOTE_TUBE) FIELD(cycle40)
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_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()
+67 -11
View File
@@ -40,16 +40,33 @@
template <template <bool> class Archive>
bool do_serialize(Archive<false> &ar, zephyr_oracle::pricing_record &pr, uint8_t version)
{
// very basic sanity check
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record)) {
ar.stream().setstate(std::ios::failbit);
return false;
if (version < 3)
{
// very basic sanity check
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;
}
@@ -59,12 +76,51 @@ bool do_serialize(Archive<true> &ar, zephyr_oracle::pricing_record &pr, uint8_t
{
ar.begin_string();
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
if (!ar.stream().good())
if (version < 3)
{
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;
ar.end_string();
return true;
}
BLOB_SERIALIZER(zephyr_oracle::pricing_record);
BLOB_SERIALIZER(zephyr_oracle::pricing_record_v1);
+148 -35
View File
@@ -40,22 +40,39 @@ namespace zephyr_oracle
{
struct pr_serialized
{
uint64_t zEPHUSD;
uint64_t zEPHRSV;
uint64_t spot;
uint64_t moving_average;
uint64_t stable;
uint64_t stable_ma;
uint64_t reserve;
uint64_t reserve_ma;
uint64_t timestamp;
std::string signature;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(zEPHUSD)
KV_SERIALIZE(zEPHRSV)
KV_SERIALIZE(spot)
KV_SERIALIZE(moving_average)
KV_SERIALIZE(stable)
KV_SERIALIZE(stable_ma)
KV_SERIALIZE(reserve)
KV_SERIALIZE(reserve_ma)
KV_SERIALIZE(timestamp)
KV_SERIALIZE(signature)
END_KV_SERIALIZE_MAP()
};
}
pricing_record::pricing_record() noexcept
: zEPHUSD(0)
, zEPHRSV(0)
, timestamp(0) {}
: spot(0)
, moving_average(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)
{
@@ -63,52 +80,71 @@ namespace zephyr_oracle
if (in._load(src, hparent))
{
// Copy everything into the local instance
zEPHUSD = in.zEPHUSD;
zEPHRSV = in.zEPHRSV;
spot = in.spot;
moving_average = in.moving_average;
stable = in.stable;
stable_ma = in.stable_ma;
reserve = in.reserve;
reserve_ma = in.reserve_ma;
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;
}
// Report error here?
return false;
}
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);
}
pricing_record::pricing_record(const pricing_record& orig) noexcept
: zEPHUSD(orig.zEPHUSD)
, zEPHRSV(orig.zEPHRSV)
, timestamp(orig.timestamp) {}
: spot(orig.spot)
, moving_average(orig.moving_average)
, 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
{
zEPHUSD = orig.zEPHUSD;
zEPHRSV = orig.zEPHRSV;
spot = orig.spot;
moving_average = orig.moving_average;
stable = orig.stable;
stable_ma = orig.stable_ma;
reserve = orig.reserve;
reserve_ma = orig.reserve_ma;
timestamp = orig.timestamp;
::memcpy(signature, orig.signature, sizeof(signature));
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
{
return ((zEPHUSD == other.zEPHUSD) &&
(zEPHRSV == other.zEPHRSV) &&
(timestamp == other.timestamp));
return ((spot == other.spot) &&
(moving_average == other.moving_average) &&
(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
@@ -117,6 +153,69 @@ namespace zephyr_oracle
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
{
if (hf_version < 3) {
@@ -126,16 +225,29 @@ namespace zephyr_oracle
if (this->empty())
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
if (this->timestamp > bl_timestamp + PRICING_RECORD_VALID_TIME_DIFF_FROM_BLOCK) {
LOG_ERROR("Pricing record timestamp is too far in the future.");
return false;
}
if (this->timestamp <= last_bl_timestamp - PRICING_RECORD_VALID_TIME_DIFF_FROM_BLOCK) {
if (this->timestamp <= last_bl_timestamp) {
LOG_ERROR("Pricing record timestamp: " << this->timestamp << ", block timestamp: " << bl_timestamp);
LOG_ERROR("Pricing record timestamp is too old.");
return false;
@@ -144,3 +256,4 @@ namespace zephyr_oracle
return true;
}
}
+47 -2
View File
@@ -57,15 +57,28 @@ namespace epee
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
{
public:
// Fields
uint64_t zEPHUSD;
uint64_t zEPHRSV;
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];
// Default c'tor
pricing_record() noexcept;
//! Load from epee p2p format
@@ -76,6 +89,8 @@ namespace zephyr_oracle
~pricing_record() = default;
bool equal(const pricing_record& other) 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;
pricing_record& operator=(const pricing_record& orig) noexcept;
@@ -92,4 +107,34 @@ namespace zephyr_oracle
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