ZEPH support

This commit is contained in:
MoneroOcean
2023-06-25 00:25:37 +00:00
parent 5747436dd3
commit 7fa30e45cc
4 changed files with 325 additions and 7 deletions

View File

@@ -33,20 +33,20 @@
#include <vector>
#include "serialization.h"
#include "oracle/pricing_record.h"
#include "zephyr_oracle/pricing_record.h"
#include "cryptonote_config.h"
// read
template <template <bool> class Archive>
bool do_serialize(Archive<false> &ar, 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 (ar.remaining_bytes() < sizeof(oracle::pricing_record)) {
if (ar.remaining_bytes() < sizeof(zephyr_oracle::pricing_record)) {
ar.stream().setstate(std::ios::failbit);
return false;
}
ar.serialize_blob(&pr, sizeof(oracle::pricing_record), "");
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
if (!ar.stream().good())
return false;
@@ -55,11 +55,11 @@ bool do_serialize(Archive<false> &ar, oracle::pricing_record &pr, uint8_t versio
// write
template <template <bool> class Archive>
bool do_serialize(Archive<true> &ar, oracle::pricing_record &pr, uint8_t version)
bool do_serialize(Archive<true> &ar, zephyr_oracle::pricing_record &pr, uint8_t version)
{
ar.begin_string();
ar.serialize_blob(&pr, sizeof(oracle::pricing_record), "");
ar.serialize_blob(&pr, sizeof(zephyr_oracle::pricing_record), "");
if (!ar.stream().good())
return false;
@@ -67,4 +67,4 @@ bool do_serialize(Archive<true> &ar, oracle::pricing_record &pr, uint8_t version
return true;
}
BLOB_SERIALIZER(oracle::pricing_record);
BLOB_SERIALIZER(zephyr_oracle::pricing_record);

View File

@@ -0,0 +1,77 @@
// Copyright (c) 2021, Haven Protocol
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <string>
#include <vector>
namespace zephyr_oracle {
const std::vector<std::string> ASSET_TYPES = {"ZEPH", "ZEPHUSD", "ZEPHRSV"};
class asset_type_counts
{
public:
// Fields
uint64_t ZEPH;
uint64_t ZEPHUSD;
uint64_t ZEPHRSV;
asset_type_counts() noexcept
: ZEPH(0)
, ZEPHUSD(0)
, ZEPHRSV(0)
{
}
uint64_t operator[](const std::string asset_type) const noexcept
{
if (asset_type == "ZEPH") {
return ZEPH;
} else if (asset_type == "ZEPHUSD") {
return ZEPHUSD;
} else if (asset_type == "ZEPHRSV") {
return ZEPHRSV;
}
return 0;
}
void add(const std::string asset_type, const uint64_t val)
{
if (asset_type == "ZEPH") {
ZEPH += val;
} else if (asset_type == "ZEPHUSD") {
ZEPHUSD += val;
} else if (asset_type == "ZEPHRSV") {
ZEPHRSV += val;
}
}
};
}

View File

@@ -0,0 +1,146 @@
// Copyright (c) 2019, Haven Protocol
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Portions of this code based upon code Copyright (c) 2019, The Monero Project
#include "pricing_record.h"
#include "serialization/keyvalue_serialization.h"
#include "storages/portable_storage.h"
#include "string_tools.h"
namespace zephyr_oracle
{
namespace
{
struct pr_serialized
{
uint64_t zEPHUSD;
uint64_t zEPHRSV;
uint64_t timestamp;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(zEPHUSD)
KV_SERIALIZE(zEPHRSV)
KV_SERIALIZE(timestamp)
END_KV_SERIALIZE_MAP()
};
}
pricing_record::pricing_record() noexcept
: zEPHUSD(0)
, zEPHRSV(0)
, timestamp(0) {}
bool pricing_record::_load(epee::serialization::portable_storage& src, epee::serialization::section* hparent)
{
pr_serialized in{};
if (in._load(src, hparent))
{
// Copy everything into the local instance
zEPHUSD = in.zEPHUSD;
zEPHRSV = in.zEPHRSV;
timestamp = in.timestamp;
return true;
}
return false;
}
bool pricing_record::store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const
{
const pr_serialized out{zEPHUSD,zEPHRSV,timestamp};
return out.store(dest, hparent);
}
pricing_record::pricing_record(const pricing_record& orig) noexcept
: zEPHUSD(orig.zEPHUSD)
, zEPHRSV(orig.zEPHRSV)
, timestamp(orig.timestamp) {}
pricing_record& pricing_record::operator=(const pricing_record& orig) noexcept
{
zEPHUSD = orig.zEPHUSD;
zEPHRSV = orig.zEPHRSV;
timestamp = orig.timestamp;
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));
}
bool pricing_record::empty() const noexcept
{
const pricing_record empty_pr = zephyr_oracle::pricing_record();
return (*this).equal(empty_pr);
}
bool pricing_record::valid(uint32_t hf_version, uint64_t bl_timestamp, uint64_t last_bl_timestamp) const
{
if (hf_version < 3) {
if (!this->empty())
return false;
}
if (this->empty())
return true;
// 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) {
LOG_ERROR("Pricing record timestamp: " << this->timestamp << ", block timestamp: " << bl_timestamp);
LOG_ERROR("Pricing record timestamp is too old.");
return false;
}
return true;
}
}

View File

@@ -0,0 +1,95 @@
// Copyright (c) 2019, Haven Protocol
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Portions of this code based upon code Copyright (c) 2019, The Monero Project
#pragma once
#include "common/pod-class.h"
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/ecdsa.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <cstdint>
#include <string>
#include <cstring>
#include "cryptonote_config.h"
#include "crypto/hash.h"
namespace epee
{
namespace serialization
{
class portable_storage;
struct section;
}
}
namespace zephyr_oracle
{
class pricing_record
{
public:
// Fields
uint64_t zEPHUSD;
uint64_t zEPHRSV;
uint64_t timestamp;
// Default c'tor
pricing_record() noexcept;
//! Load from epee p2p format
bool _load(epee::serialization::portable_storage& src, epee::serialization::section* hparent);
//! Store in epee p2p format
bool store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const;
pricing_record(const pricing_record& orig) noexcept;
~pricing_record() = default;
bool equal(const pricing_record& other) const noexcept;
bool empty() 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;
uint64_t operator[](const std::string& asset_type) const;
};
inline bool operator==(const pricing_record& a, const pricing_record& b) noexcept
{
return a.equal(b);
}
inline bool operator!=(const pricing_record& a, const pricing_record& b) noexcept
{
return !a.equal(b);
}
} // oracle