Skip to content

Commit

Permalink
fluid: move transaction payload extraction to template
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Aug 30, 2021
1 parent 6c3da39 commit 1abce98
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 89 deletions.
24 changes: 2 additions & 22 deletions src/fluid/dynode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,15 @@

CFluidDynodeDB* pFluidDynodeDB = NULL;

bool GetFluidDynodeData(const CTransaction& tx, CFluidDynode& entry, int& nOut)
{
int n = 0;
for (const CTxOut& txout : tx.vout) {
CScript txOut = txout.scriptPubKey;
if (WithinFluidRange(txOut.GetFlag())) {
nOut = n;
return ParseScript(txOut, entry);
}
n++;
}
return false;
}

bool CFluidDynode::UnserializeFromTx(const CTransaction& tx)
{
int nOut;
if (!GetFluidDynodeData(tx, *this, nOut)) {
return false;
}
return true;
return ParseData(tx, *this, nOut);
}

bool CFluidDynode::UnserializeFromScript(const CScript& fluidScript)
{
if (!ParseScript(fluidScript, *this)) {
return false;
}
return true;
return ParseScript(fluidScript, *this);
}

void CFluidDynode::Serialize(std::vector<unsigned char>& vchData)
Expand Down
19 changes: 19 additions & 0 deletions src/fluid/fluid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,26 @@ bool ParseScript(const CScript& scriptPubKey, T1& object)
return object.IsNull();
}

template <typename T1>
bool ParseData(const CTransaction& tx, T1& entry, int& nOut)
{
nOut = 0;
for (const CTxOut& txout : tx.vout) {
CScript txOut = txout.scriptPubKey;
if (WithinFluidRange(txOut.GetFlag())) {
return ParseScript(txOut, entry);
}
nOut++;
}
return false;
}

template bool ParseScript<CFluidMint>(const CScript& scriptPubKey, CFluidMint& object);
template bool ParseScript<CFluidMining>(const CScript& scriptPubKey, CFluidMining& object);
template bool ParseScript<CFluidDynode>(const CScript& scriptPubKey, CFluidDynode& object);
template bool ParseScript<CFluidSovereign>(const CScript& scriptPubKey, CFluidSovereign& object);

template bool ParseData<CFluidMint>(const CTransaction& tx, CFluidMint& entry, int& nOut);
template bool ParseData<CFluidMining>(const CTransaction& tx, CFluidMining& entry, int& nOut);
template bool ParseData<CFluidDynode>(const CTransaction& tx, CFluidDynode& entry, int& nOut);
template bool ParseData<CFluidSovereign>(const CTransaction& tx, CFluidSovereign& entry, int& nOut);
3 changes: 3 additions & 0 deletions src/fluid/fluid.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ std::vector<unsigned char> FluidScriptToCharVector(const CScript& fluidScript);
template <typename T1>
bool ParseScript(const CScript& scriptPubKey, T1& object);

template <typename T1>
bool ParseData(const CTransaction& tx, T1& entry, int& nOut);

extern CFluid fluid;

#endif // FLUID_PROTOCOL_H
24 changes: 2 additions & 22 deletions src/fluid/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,15 @@

CFluidMiningDB* pFluidMiningDB = NULL;

bool GetFluidMiningData(const CTransaction& tx, CFluidMining& entry, int& nOut)
{
int n = 0;
for (const CTxOut& txout : tx.vout) {
CScript txOut = txout.scriptPubKey;
if (WithinFluidRange(txOut.GetFlag())) {
nOut = n;
return ParseScript(txOut, entry);
}
n++;
}
return false;
}

bool CFluidMining::UnserializeFromTx(const CTransaction& tx)
{
int nOut;
if (!GetFluidMiningData(tx, *this, nOut)) {
return false;
}
return true;
return ParseData(tx, *this, nOut);
}

bool CFluidMining::UnserializeFromScript(const CScript& fluidScript)
{
if (!ParseScript(fluidScript, *this)) {
return false;
}
return true;
return ParseScript(fluidScript, *this);
}

void CFluidMining::Serialize(std::vector<unsigned char>& vchData)
Expand Down
25 changes: 2 additions & 23 deletions src/fluid/mint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,15 @@

CFluidMintDB* pFluidMintDB = NULL;

bool GetFluidMintData(const CTransaction& tx, CFluidMint& entry, int& nOut)
{
int n = 0;
for (const CTxOut& txout : tx.vout) {
CScript txOut = txout.scriptPubKey;
if (WithinFluidRange(txOut.GetFlag())) {
nOut = n;
return ParseScript(txOut, entry);
}
n++;
}
return false;
}

bool CFluidMint::UnserializeFromTx(const CTransaction& tx)
{
int nOut;
if (!GetFluidMintData(tx, *this, nOut)) {
return false;
}
return true;
return ParseData(tx, *this, nOut);
}

bool CFluidMint::UnserializeFromScript(const CScript& fluidScript)
{
if (!ParseScript(fluidScript, *this)) {
return false;
}
return true;
return ParseScript(fluidScript, *this);
}

void CFluidMint::Serialize(std::vector<unsigned char>& vchData)
Expand All @@ -68,7 +48,6 @@ bool CFluidMintDB::AddFluidMintEntry(const CFluidMint& entry, const int op)
LOCK(cs_fluid_mint);
writeState = Write(make_pair(std::string("script"), entry.GetTransactionScript()), entry) && Write(make_pair(std::string("txid"), entry.GetTransactionHash()), entry.GetTransactionScript());
}

return writeState;
}

Expand Down
24 changes: 2 additions & 22 deletions src/fluid/sovereign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,15 @@

CFluidSovereignDB* pFluidSovereignDB = NULL;

bool GetFluidSovereignData(const CTransaction& tx, CFluidSovereign& entry, int& nOut)
{
int n = 0;
for (const CTxOut& txout : tx.vout) {
CScript txOut = txout.scriptPubKey;
if (WithinFluidRange(txOut.GetFlag())) {
nOut = n;
return ParseScript(txOut, entry);
}
n++;
}
return false;
}

bool CFluidSovereign::UnserializeFromTx(const CTransaction& tx)
{
int nOut;
if (!GetFluidSovereignData(tx, *this, nOut)) {
return false;
}
return true;
return ParseData(tx, *this, nOut);
}

bool CFluidSovereign::UnserializeFromScript(const CScript& fluidScript)
{
if (!ParseScript(fluidScript, *this)) {
return false;
}
return true;
return ParseScript(fluidScript, *this);
}

void CFluidSovereign::Serialize(std::vector<unsigned char>& vchData)
Expand Down

0 comments on commit 1abce98

Please sign in to comment.