////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2018, goatpig. //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#include "ClientClasses.h"
#include "WebSocketClient.h"
#include "protobuf/BDVCommand.pb.h"
using namespace ClientClasses;
using namespace ::Codec_BDVCommand;
///////////////////////////////////////////////////////////////////////////////
//
// BlockHeader
//
///////////////////////////////////////////////////////////////////////////////
BlockHeader::BlockHeader(
const BinaryData& rawheader, unsigned height)
{
unserialize(rawheader.getRef());
blockHeight_ = height;
}
////////////////////////////////////////////////////////////////////////////////
void BlockHeader::unserialize(uint8_t const * ptr, uint32_t size)
{
if (size < HEADER_SIZE)
throw BlockDeserializingException();
dataCopy_.copyFrom(ptr, HEADER_SIZE);
BtcUtils::getHash256(dataCopy_.getPtr(), HEADER_SIZE, thisHash_);
difficultyDbl_ = BtcUtils::convertDiffBitsToDouble(
BinaryDataRef(dataCopy_.getPtr() + 72, 4));
isInitialized_ = true;
blockHeight_ = UINT32_MAX;
}
///////////////////////////////////////////////////////////////////////////////
//
// LedgerEntry
//
///////////////////////////////////////////////////////////////////////////////
LedgerEntry::LedgerEntry(shared_ptr<::Codec_LedgerEntry::LedgerEntry> msg) :
msgPtr_(msg), ptr_(msg.get())
{}
///////////////////////////////////////////////////////////////////////////////
LedgerEntry::LedgerEntry(BinaryDataRef bdr)
{
auto msg = make_shared<::Codec_LedgerEntry::LedgerEntry>();
msg->ParseFromArray(bdr.getPtr(), bdr.getSize());
ptr_ = msg.get();
msgPtr_ = msg;
}
///////////////////////////////////////////////////////////////////////////////
LedgerEntry::LedgerEntry(
shared_ptr<::Codec_LedgerEntry::ManyLedgerEntry> msg, unsigned index) :
msgPtr_(msg)
{
ptr_ = &msg->values(index);
}
///////////////////////////////////////////////////////////////////////////////
LedgerEntry::LedgerEntry(
shared_ptr<::Codec_BDVCommand::BDVCallback> msg, unsigned i, unsigned y) :
msgPtr_(msg)
{
auto& notif = msg->notification(i);
auto& ledgers = notif.ledgers();
ptr_ = &ledgers.values(y);
}
///////////////////////////////////////////////////////////////////////////////
const string& LedgerEntry::getID() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->id();
}
///////////////////////////////////////////////////////////////////////////////
int64_t LedgerEntry::getValue() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->balance();
}
///////////////////////////////////////////////////////////////////////////////
uint32_t LedgerEntry::getBlockNum() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->txheight();
}
///////////////////////////////////////////////////////////////////////////////
BinaryDataRef LedgerEntry::getTxHash() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
auto& val = ptr_->txhash();
BinaryDataRef bdr;
bdr.setRef(val);
return bdr;
}
///////////////////////////////////////////////////////////////////////////////
uint32_t LedgerEntry::getIndex() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->index();
}
///////////////////////////////////////////////////////////////////////////////
uint32_t LedgerEntry::getTxTime() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->txtime();
}
///////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::isCoinbase() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->iscoinbase();
}
///////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::isSentToSelf() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->issts();
}
///////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::isChangeBack() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->ischangeback();
}
///////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::isOptInRBF() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->optinrbf();
}
///////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::isChainedZC() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->ischainedzc();
}
///////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::isWitness() const
{
if (ptr_ == nullptr)
throw runtime_error("uninitialized ledger entry");
return ptr_->iswitness();
}
///////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::operator==(const LedgerEntry& rhs)
{
if (getTxHash() != rhs.getTxHash())
return false;
if (getIndex() != rhs.getIndex())
return false;
return true;
}
///////////////////////////////////////////////////////////////////////////////
//
// RemoteCallback
//
///////////////////////////////////////////////////////////////////////////////
RemoteCallback::~RemoteCallback(void)
{}
///////////////////////////////////////////////////////////////////////////////
bool RemoteCallback::processNotifications(
shared_ptr