Skip to content
Merged
130 changes: 122 additions & 8 deletions src/addressindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,47 @@
#include "amount.h"
#include "script/script.h"

static const std::string RVN = "RVN";

struct CAddressUnspentKey {
unsigned int type;
uint160 hashBytes;
std::string asset;
uint256 txhash;
size_t index;

size_t GetSerializeSize() const {
return 57;
return 57 + asset.size();
}
template<typename Stream>
void Serialize(Stream& s) const {
ser_writedata8(s, type);
hashBytes.Serialize(s);
::Serialize(s, asset);
txhash.Serialize(s);
ser_writedata32(s, index);
}
template<typename Stream>
void Unserialize(Stream& s) {
type = ser_readdata8(s);
hashBytes.Unserialize(s);
::Unserialize(s, asset);
txhash.Unserialize(s);
index = ser_readdata32(s);
}

CAddressUnspentKey(unsigned int addressType, uint160 addressHash, uint256 txid, size_t indexValue) {
type = addressType;
hashBytes = addressHash;
asset = RVN;
txhash = txid;
index = indexValue;
}

CAddressUnspentKey(unsigned int addressType, uint160 addressHash, std::string assetName, uint256 txid, size_t indexValue) {
type = addressType;
hashBytes = addressHash;
asset = assetName;
txhash = txid;
index = indexValue;
}
Expand All @@ -48,6 +62,7 @@ struct CAddressUnspentKey {
void SetNull() {
type = 0;
hashBytes.SetNull();
asset.clear();
txhash.SetNull();
index = 0;
}
Expand Down Expand Up @@ -91,19 +106,21 @@ struct CAddressUnspentValue {
struct CAddressIndexKey {
unsigned int type;
uint160 hashBytes;
std::string asset;
int blockHeight;
unsigned int txindex;
uint256 txhash;
size_t index;
bool spending;

size_t GetSerializeSize() const {
return 66;
return 34 + asset.size();
}
template<typename Stream>
void Serialize(Stream& s) const {
ser_writedata8(s, type);
hashBytes.Serialize(s);
::Serialize(s, asset);
// Heights are stored big-endian for key sorting in LevelDB
ser_writedata32be(s, blockHeight);
ser_writedata32be(s, txindex);
Expand All @@ -116,6 +133,7 @@ struct CAddressIndexKey {
void Unserialize(Stream& s) {
type = ser_readdata8(s);
hashBytes.Unserialize(s);
::Unserialize(s, asset);
blockHeight = ser_readdata32be(s);
txindex = ser_readdata32be(s);
txhash.Unserialize(s);
Expand All @@ -128,6 +146,19 @@ struct CAddressIndexKey {
uint256 txid, size_t indexValue, bool isSpending) {
type = addressType;
hashBytes = addressHash;
asset = RVN;
blockHeight = height;
txindex = blockindex;
txhash = txid;
index = indexValue;
spending = isSpending;
}

CAddressIndexKey(unsigned int addressType, uint160 addressHash, std::string assetName, int height, int blockindex,
uint256 txid, size_t indexValue, bool isSpending) {
type = addressType;
hashBytes = addressHash;
asset = assetName;
blockHeight = height;
txindex = blockindex;
txhash = txid;
Expand All @@ -142,6 +173,7 @@ struct CAddressIndexKey {
void SetNull() {
type = 0;
hashBytes.SetNull();
asset.clear();
blockHeight = 0;
txindex = 0;
txhash.SetNull();
Expand Down Expand Up @@ -184,30 +216,85 @@ struct CAddressIndexIteratorKey {
}
};

struct CAddressIndexIteratorAssetKey {
unsigned int type;
uint160 hashBytes;
std::string asset;

size_t GetSerializeSize() const {
return 21 + asset.size();
}
template<typename Stream>
void Serialize(Stream& s) const {
ser_writedata8(s, type);
hashBytes.Serialize(s);
::Serialize(s, asset);
}
template<typename Stream>
void Unserialize(Stream& s) {
type = ser_readdata8(s);
hashBytes.Unserialize(s);
::Unserialize(s, asset);
}

CAddressIndexIteratorAssetKey(unsigned int addressType, uint160 addressHash) {
type = addressType;
hashBytes = addressHash;
asset = RVN;
}

CAddressIndexIteratorAssetKey(unsigned int addressType, uint160 addressHash, std::string assetName) {
type = addressType;
hashBytes = addressHash;
asset = assetName;
}

CAddressIndexIteratorAssetKey() {
SetNull();
}

void SetNull() {
type = 0;
hashBytes.SetNull();
asset.clear();
}
};

struct CAddressIndexIteratorHeightKey {
unsigned int type;
uint160 hashBytes;
std::string asset;
int blockHeight;

size_t GetSerializeSize() const {
return 25;
return 25 + asset.size();
}
template<typename Stream>
void Serialize(Stream& s) const {
ser_writedata8(s, type);
hashBytes.Serialize(s);
::Serialize(s, asset);
ser_writedata32be(s, blockHeight);
}
template<typename Stream>
void Unserialize(Stream& s) {
type = ser_readdata8(s);
hashBytes.Unserialize(s);
::Unserialize(s, asset);
blockHeight = ser_readdata32be(s);
}

CAddressIndexIteratorHeightKey(unsigned int addressType, uint160 addressHash, int height) {
type = addressType;
hashBytes = addressHash;
asset = RVN;
blockHeight = height;
}

CAddressIndexIteratorHeightKey(unsigned int addressType, uint160 addressHash, std::string assetName, int height) {
type = addressType;
hashBytes = addressHash;
asset = assetName;
blockHeight = height;
}

Expand All @@ -218,6 +305,7 @@ struct CAddressIndexIteratorHeightKey {
void SetNull() {
type = 0;
hashBytes.SetNull();
asset.clear();
blockHeight = 0;
}
};
Expand Down Expand Up @@ -248,21 +336,43 @@ struct CMempoolAddressDeltaKey
{
int type;
uint160 addressBytes;
std::string asset;
uint256 txhash;
unsigned int index;
int spending;

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, std::string assetName,
uint256 hash, unsigned int i, int s) {
type = addressType;
addressBytes = addressHash;
asset = assetName;
txhash = hash;
index = i;
spending = s;
}

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, int s) {
type = addressType;
addressBytes = addressHash;
asset = "";
txhash = hash;
index = i;
spending = s;
}

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, std::string assetName) {
type = addressType;
addressBytes = addressHash;
asset = assetName;
txhash.SetNull();
index = 0;
spending = 0;
}

CMempoolAddressDeltaKey(int addressType, uint160 addressHash) {
type = addressType;
addressBytes = addressHash;
asset = "";
txhash.SetNull();
index = 0;
spending = 0;
Expand All @@ -274,14 +384,18 @@ struct CMempoolAddressDeltaKeyCompare
bool operator()(const CMempoolAddressDeltaKey& a, const CMempoolAddressDeltaKey& b) const {
if (a.type == b.type) {
if (a.addressBytes == b.addressBytes) {
if (a.txhash == b.txhash) {
if (a.index == b.index) {
return a.spending < b.spending;
if (a.asset == b.asset) {
if (a.txhash == b.txhash) {
if (a.index == b.index) {
return a.spending < b.spending;
} else {
return a.index < b.index;
}
} else {
return a.index < b.index;
return a.txhash < b.txhash;
}
} else {
return a.txhash < b.txhash;
return a.asset < b.asset;
}
} else {
return a.addressBytes < b.addressBytes;
Expand Down
57 changes: 57 additions & 0 deletions src/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2899,4 +2899,61 @@ void GetTxOutAssetTypes(const std::vector<CTxOut>& vout, int& issues, int& reiss
reissues++;
}
}
}

bool ParseAssetScript(CScript scriptPubKey, uint160 &hashBytes, std::string &assetName, CAmount &assetAmount) {
int nType;
bool fIsOwner;
int _nStartingPoint;
std::string _strAddress;
bool isAsset = false;
if (scriptPubKey.IsAssetScript(nType, fIsOwner, _nStartingPoint)) {
if (nType == TX_NEW_ASSET) {
if (fIsOwner) {
if (OwnerAssetFromScript(scriptPubKey, assetName, _strAddress)) {
assetAmount = OWNER_ASSET_AMOUNT;
isAsset = true;
} else {
LogPrintf("%s : Couldn't get new owner asset from script: %s", __func__, HexStr(scriptPubKey));
}
} else {
CNewAsset asset;
if (AssetFromScript(scriptPubKey, asset, _strAddress)) {
assetName = asset.strName;
assetAmount = asset.nAmount;
isAsset = true;
} else {
LogPrintf("%s : Couldn't get new asset from script: %s", __func__, HexStr(scriptPubKey));
}
}
} else if (nType == TX_REISSUE_ASSET) {
CReissueAsset asset;
if (ReissueAssetFromScript(scriptPubKey, asset, _strAddress)) {
assetName = asset.strName;
assetAmount = asset.nAmount;
isAsset = true;
} else {
LogPrintf("%s : Couldn't get reissue asset from script: %s", __func__, HexStr(scriptPubKey));
}
} else if (nType == TX_TRANSFER_ASSET) {
CAssetTransfer asset;
if (TransferAssetFromScript(scriptPubKey, asset, _strAddress)) {
assetName = asset.strName;
assetAmount = asset.nAmount;
isAsset = true;
} else {
LogPrintf("%s : Couldn't get transfer asset from script: %s", __func__, HexStr(scriptPubKey));
}
} else {
LogPrintf("%s : Unsupported asset type: %s", __func__, nType);
}
} else {
// LogPrintf("%s : Found no asset in script: %s", __func__, HexStr(scriptPubKey));
}
if (isAsset) {
// LogPrintf("%s : Found assets in script at address %s : %s (%s)", __func__, _strAddress, assetName, assetAmount);
hashBytes = uint160(std::vector <unsigned char>(scriptPubKey.begin()+3, scriptPubKey.begin()+23));
return true;
}
return false;
}
3 changes: 3 additions & 0 deletions src/assets/assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,7 @@ bool CreateAssetTransaction(CWallet* pwallet, CCoinControl& coinControl, const s
bool CreateReissueAssetTransaction(CWallet* pwallet, CCoinControl& coinControl, const CReissueAsset& asset, const std::string& address, std::pair<int, std::string>& error, CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRequired);
bool CreateTransferAssetTransaction(CWallet* pwallet, const CCoinControl& coinControl, const std::vector< std::pair<CAssetTransfer, std::string> >vTransfers, const std::string& changeAddress, std::pair<int, std::string>& error, CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRequired);
bool SendAssetTransaction(CWallet* pwallet, CWalletTx& transaction, CReserveKey& reserveKey, std::pair<int, std::string>& error, std::string& txid);

/** Helper method for extracting address bytes, asset name and amount from an asset script */
bool ParseAssetScript(CScript scriptPubKey, uint160 &hashBytes, std::string &assetName, CAmount &assetAmount);
#endif //RAVENCOIN_ASSET_PROTOCOL_H
1 change: 1 addition & 0 deletions src/assets/assettypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <list>
#include <unordered_map>
#include "amount.h"
#include "script/standard.h"
#include "primitives/transaction.h"

#define MAX_UNIT 8
Expand Down
Loading