Skip to content

Commit 5f11342

Browse files
committed
fluid: create base object structure, remove repetitive logic
1 parent abd543d commit 5f11342

6 files changed

Lines changed: 181 additions & 337 deletions

File tree

src/fluid/dynode.h

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,36 @@
66
#include "amount.h"
77
#include "dbwrapper.h"
88
#include "serialize.h"
9+
#include "fluid/fluid.h"
910

1011
#include "sync.h"
1112
#include "uint256.h"
1213

1314
class CScript;
1415
class CTransaction;
1516

16-
class CFluidDynode
17+
class CFluidDynode : public DSFluidObject
1718
{
1819
public:
19-
static const int CURRENT_VERSION = 1;
20-
int nVersion;
21-
std::vector<unsigned char> FluidScript;
22-
CAmount DynodeReward;
23-
int64_t nTimeStamp;
24-
std::vector<std::vector<unsigned char> > SovereignAddresses;
25-
uint256 txHash;
26-
unsigned int nHeight;
27-
std::vector<unsigned char> DestinationAddress;
28-
29-
CFluidDynode()
30-
{
31-
SetNull();
32-
}
33-
34-
CFluidDynode(const CTransaction& tx)
35-
{
36-
SetNull();
37-
UnserializeFromTx(tx);
38-
}
39-
40-
CFluidDynode(const CScript& fluidScript)
41-
{
42-
SetNull();
43-
UnserializeFromScript(fluidScript);
44-
}
45-
46-
void Initialise(std::vector<unsigned char> _vch, CAmount _amt, int64_t _t)
47-
{
48-
FluidScript = _vch;
49-
DynodeReward = _amt;
50-
nTimeStamp = _t;
51-
}
52-
53-
inline void SetNull()
54-
{
55-
nVersion = CFluidDynode::CURRENT_VERSION;
56-
FluidScript.clear();
57-
DynodeReward = -1;
58-
nTimeStamp = 0;
59-
SovereignAddresses.clear();
60-
txHash.SetNull();
61-
nHeight = 0;
62-
}
20+
CFluidDynode() = default;
21+
CFluidDynode(const CTransaction& tx) { UnserializeFromTx(tx); }
22+
CFluidDynode(const CScript& fluidScript) { UnserializeFromScript(fluidScript); }
23+
~CFluidDynode() = default;
6324

6425
ADD_SERIALIZE_METHODS;
6526

6627
template <typename Stream, typename Operation>
6728
inline void SerializationOp(Stream& s, Operation ser_action)
6829
{
69-
READWRITE(this->nVersion);
70-
READWRITE(FluidScript);
71-
READWRITE(DynodeReward);
72-
READWRITE(VARINT(nTimeStamp));
73-
READWRITE(SovereignAddresses);
74-
READWRITE(txHash);
75-
READWRITE(VARINT(nHeight));
76-
}
77-
78-
inline friend bool operator==(const CFluidDynode& a, const CFluidDynode& b)
79-
{
80-
return (a.FluidScript == b.FluidScript && a.DynodeReward == b.DynodeReward && a.nTimeStamp == b.nTimeStamp);
81-
}
82-
83-
inline friend bool operator!=(const CFluidDynode& a, const CFluidDynode& b)
84-
{
85-
return !(a == b);
86-
}
87-
88-
friend bool operator<(const CFluidDynode& a, const CFluidDynode& b)
89-
{
90-
return (a.nTimeStamp < b.nTimeStamp);
91-
}
92-
93-
friend bool operator>(const CFluidDynode& a, const CFluidDynode& b)
94-
{
95-
return (a.nTimeStamp > b.nTimeStamp);
96-
}
97-
98-
inline CFluidDynode operator=(const CFluidDynode& b)
99-
{
100-
FluidScript = b.FluidScript;
101-
DynodeReward = b.DynodeReward;
102-
nTimeStamp = b.nTimeStamp;
103-
SovereignAddresses.clear(); //clear out previous entries
104-
for (const std::vector<unsigned char>& vchAddress : b.SovereignAddresses) {
105-
SovereignAddresses.push_back(vchAddress);
106-
}
107-
txHash = b.txHash;
108-
nHeight = b.nHeight;
109-
return *this;
30+
READWRITE(this->version);
31+
READWRITE(tx_script);
32+
READWRITE(obj_reward);
33+
READWRITE(VARINT(obj_time));
34+
READWRITE(obj_sigs);
35+
READWRITE(tx_hash);
36+
READWRITE(VARINT(tx_height));
11037
}
11138

112-
inline bool IsNull() const { return (nTimeStamp == 0); }
11339
bool UnserializeFromTx(const CTransaction& tx);
11440
bool UnserializeFromScript(const CScript& fluidScript);
11541
void Serialize(std::vector<unsigned char>& vchData);

src/fluid/fluid.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ bool ParseScript(const CScript& scriptPubKey, T1& object)
634634
switch (scriptPubKey.GetFlag())
635635
{
636636
case OP_MINT:
637-
object.DestinationAddress = CharVectorFromString(ser_fields[2]);
637+
object.obj_address = CharVectorFromString(ser_fields[2]);
638638
case OP_REWARD_DYNODE:
639639
case OP_REWARD_MINING:
640640
object.Initialise(
@@ -659,7 +659,7 @@ bool ParseScript(const CScript& scriptPubKey, T1& object)
659659
}
660660

661661
for (; s > e; s++) {
662-
object.SovereignAddresses.push_back(
662+
object.obj_sigs.insert(
663663
CharVectorFromString(
664664
fluid.GetAddressFromDigestSignature(ser_fields[s], payload).ToString()
665665
)

src/fluid/fluid.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,131 @@ class CFluid
6565

6666
};
6767

68+
class DSFluidObject
69+
{
70+
typedef std::vector<unsigned char> byte_vec;
71+
static constexpr int CURRENT_VERSION = 1;
72+
73+
friend class CFluidDynode;
74+
friend class CFluidMining;
75+
friend class CFluidMint;
76+
friend class CFluidSovereign;
77+
78+
private:
79+
int version;
80+
81+
uint256 tx_hash;
82+
uint32_t tx_height;
83+
byte_vec tx_script;
84+
85+
uint64_t obj_reward;
86+
uint64_t obj_time;
87+
88+
protected:
89+
DSFluidObject Null()
90+
{
91+
DSFluidObject obj;
92+
version = DSFluidObject::CURRENT_VERSION;
93+
tx_hash.SetNull();
94+
tx_script.clear();
95+
tx_height = 0;
96+
obj_reward = -1;
97+
obj_time = 0;
98+
obj_sigs.clear();
99+
obj_address.clear();
100+
101+
return obj;
102+
}
103+
104+
public:
105+
byte_vec obj_address;
106+
std::set<byte_vec> obj_sigs;
107+
108+
DSFluidObject()
109+
{
110+
*this = Null();
111+
}
112+
113+
DSFluidObject(DSFluidObject&& obj)
114+
{
115+
std::swap(version, obj.version);
116+
std::swap(tx_hash, obj.tx_hash);
117+
std::swap(tx_script, obj.tx_script);
118+
std::swap(obj_reward, obj.obj_reward);
119+
std::swap(obj_time, obj.obj_time);
120+
std::swap(obj_sigs, obj.obj_sigs);
121+
std::swap(obj_address, obj.obj_address);
122+
}
123+
124+
DSFluidObject& operator=(DSFluidObject&& obj)
125+
{
126+
std::swap(version, obj.version);
127+
std::swap(tx_hash, obj.tx_hash);
128+
std::swap(tx_script, obj.tx_script);
129+
std::swap(obj_reward, obj.obj_reward);
130+
std::swap(obj_time, obj.obj_time);
131+
std::swap(obj_sigs, obj.obj_sigs);
132+
std::swap(obj_address, obj.obj_address);
133+
return *this;
134+
}
135+
136+
DSFluidObject(const DSFluidObject& obj) = default;
137+
DSFluidObject& operator=(const DSFluidObject& obj) = default;
138+
139+
bool operator>(const DSFluidObject& obj) const
140+
{
141+
return obj_time > obj.obj_time;
142+
}
143+
144+
bool operator<(const DSFluidObject& obj) const
145+
{
146+
return obj_time < obj.obj_time;
147+
}
148+
149+
bool operator==(const DSFluidObject& obj) const
150+
{
151+
return tx_script == obj.tx_script;
152+
}
153+
154+
bool operator!=(const DSFluidObject& obj) const
155+
{
156+
return !((*this) == obj);
157+
}
158+
159+
void Initialise(byte_vec _vch, CAmount _amt, int64_t _t)
160+
{
161+
return InitialiseScriptRewardTime(_vch, _amt, _t);
162+
}
163+
164+
void InitialiseScriptRewardTime(byte_vec _vch, CAmount _amt, int64_t _t)
165+
{
166+
tx_script = _vch;
167+
obj_reward = _amt;
168+
obj_time = _t;
169+
}
170+
171+
void InitialiseHeightHash(CAmount _ht, uint256 _hash)
172+
{
173+
tx_height = _ht;
174+
tx_hash = _hash;
175+
}
176+
177+
void SetAddress(byte_vec _vch)
178+
{
179+
obj_address = _vch;
180+
}
181+
182+
uint64_t GetTime() const { return obj_time; }
183+
uint64_t GetHeight() const { return tx_height; }
184+
uint64_t GetReward() const { return obj_reward; }
185+
uint256 GetTransactionHash() const { return tx_hash; }
186+
uint32_t GetTransactionHeight() const { return tx_height; }
187+
byte_vec GetTransactionScript() const { return tx_script; }
188+
189+
bool IsNull() { return *this == Null(); }
190+
void SetNull() const { DSFluidObject(); }
191+
};
192+
68193
/** Standard Reward Payment Determination Functions */
69194
CAmount GetStandardPoWBlockPayment(const int& nHeight);
70195
CAmount GetStandardDynodePayment(const int& nHeight);

src/fluid/mining.h

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,36 @@
66
#include "amount.h"
77
#include "dbwrapper.h"
88
#include "serialize.h"
9+
#include "fluid/fluid.h"
910

1011
#include "sync.h"
1112
#include "uint256.h"
1213

1314
class CScript;
1415
class CTransaction;
1516

16-
class CFluidMining
17+
class CFluidMining : public DSFluidObject
1718
{
1819
public:
19-
static const int CURRENT_VERSION = 1;
20-
int nVersion;
21-
std::vector<unsigned char> FluidScript;
22-
CAmount MiningReward;
23-
int64_t nTimeStamp;
24-
std::vector<std::vector<unsigned char> > SovereignAddresses;
25-
uint256 txHash;
26-
unsigned int nHeight;
27-
std::vector<unsigned char> DestinationAddress;
28-
29-
CFluidMining()
30-
{
31-
SetNull();
32-
}
33-
34-
CFluidMining(const CTransaction& tx)
35-
{
36-
SetNull();
37-
UnserializeFromTx(tx);
38-
}
39-
40-
CFluidMining(const CScript& fluidScript)
41-
{
42-
SetNull();
43-
UnserializeFromScript(fluidScript);
44-
}
45-
46-
void Initialise(std::vector<unsigned char> _vch, CAmount _amt, int64_t _t)
47-
{
48-
FluidScript = _vch;
49-
MiningReward = _amt;
50-
nTimeStamp = _t;
51-
}
52-
53-
inline void SetNull()
54-
{
55-
nVersion = CFluidMining::CURRENT_VERSION;
56-
FluidScript.clear();
57-
MiningReward = -1;
58-
nTimeStamp = 0;
59-
SovereignAddresses.clear();
60-
txHash.SetNull();
61-
nHeight = 0;
62-
}
20+
CFluidMining() = default;
21+
CFluidMining(const CTransaction& tx) { UnserializeFromTx(tx); }
22+
CFluidMining(const CScript& fluidScript) { UnserializeFromScript(fluidScript); }
23+
~CFluidMining() = default;
6324

6425
ADD_SERIALIZE_METHODS;
6526

6627
template <typename Stream, typename Operation>
6728
inline void SerializationOp(Stream& s, Operation ser_action)
6829
{
69-
READWRITE(this->nVersion);
70-
READWRITE(FluidScript);
71-
READWRITE(MiningReward);
72-
READWRITE(VARINT(nTimeStamp));
73-
READWRITE(SovereignAddresses);
74-
READWRITE(txHash);
75-
READWRITE(VARINT(nHeight));
76-
}
77-
78-
inline friend bool operator==(const CFluidMining& a, const CFluidMining& b)
79-
{
80-
return (a.FluidScript == b.FluidScript && a.MiningReward == b.MiningReward && a.nTimeStamp == b.nTimeStamp);
81-
}
82-
83-
inline friend bool operator!=(const CFluidMining& a, const CFluidMining& b)
84-
{
85-
return !(a == b);
86-
}
87-
88-
friend bool operator<(const CFluidMining& a, const CFluidMining& b)
89-
{
90-
return (a.nTimeStamp < b.nTimeStamp);
91-
}
92-
93-
friend bool operator>(const CFluidMining& a, const CFluidMining& b)
94-
{
95-
return (a.nTimeStamp > b.nTimeStamp);
96-
}
97-
98-
inline CFluidMining operator=(const CFluidMining& b)
99-
{
100-
FluidScript = b.FluidScript;
101-
MiningReward = b.MiningReward;
102-
nTimeStamp = b.nTimeStamp;
103-
SovereignAddresses.clear(); //clear out previous entries
104-
for (const std::vector<unsigned char>& vchAddress : b.SovereignAddresses) {
105-
SovereignAddresses.push_back(vchAddress);
106-
}
107-
txHash = b.txHash;
108-
nHeight = b.nHeight;
109-
return *this;
30+
READWRITE(this->version);
31+
READWRITE(tx_script);
32+
READWRITE(obj_reward);
33+
READWRITE(VARINT(obj_time));
34+
READWRITE(obj_sigs);
35+
READWRITE(tx_hash);
36+
READWRITE(VARINT(tx_height));
11037
}
11138

112-
inline bool IsNull() const { return (nTimeStamp == 0); }
11339
bool UnserializeFromTx(const CTransaction& tx);
11440
bool UnserializeFromScript(const CScript& fluidScript);
11541
void Serialize(std::vector<unsigned char>& vchData);

0 commit comments

Comments
 (0)