hdac SDK
SDK for hdac blockchain development
모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 속성 이벤트 Friends 페이지들
transaction.h
1 #ifndef TRANSACTION_H
2 #define TRANSACTION_H
3 
4 #include <string>
5 #include <script/script.h>
6 #include <utils/serialize.h>
7 #include <structs/amount.h>
8 #include <structs/uint256.h>
9 
11 class COutPoint
12 {
13 public:
14  uint256 hash;
15  uint32_t n;
16 
17  COutPoint() { SetNull(); }
18  COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
19 
20  ADD_SERIALIZE_METHODS;
21 
22  template <typename Stream, typename Operation>
23  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
24  READWRITE(FLATDATA(*this));
25  }
26 
27  void SetNull() { hash = 0; n = (uint32_t) -1; }
28  bool IsNull() const { return (hash == 0 && n == (uint32_t) -1); }
29 
30  friend bool operator<(const COutPoint& a, const COutPoint& b)
31  {
32  return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
33  }
34 
35  friend bool operator==(const COutPoint& a, const COutPoint& b)
36  {
37  return (a.hash == b.hash && a.n == b.n);
38  }
39 
40  friend bool operator!=(const COutPoint& a, const COutPoint& b)
41  {
42  return !(a == b);
43  }
44 
45  std::string ToString() const;
46 };
47 
52 class CTxIn
53 {
54 public:
55  COutPoint prevout;
56  CScript scriptSig;
57  uint32_t nSequence;
58 
59  CTxIn()
60  {
61  nSequence = std::numeric_limits<unsigned int>::max();
62  }
63 
64  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max());
65  CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<uint32_t>::max());
66 
67  ADD_SERIALIZE_METHODS;
68 
69  template <typename Stream, typename Operation>
70  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
71  READWRITE(prevout);
72  READWRITE(scriptSig);
73  READWRITE(nSequence);
74  }
75 
76  bool IsFinal() const
77  {
78  return (nSequence == std::numeric_limits<uint32_t>::max());
79  }
80 
81  friend bool operator==(const CTxIn& a, const CTxIn& b)
82  {
83  return (a.prevout == b.prevout &&
84  a.scriptSig == b.scriptSig &&
85  a.nSequence == b.nSequence);
86  }
87 
88  friend bool operator!=(const CTxIn& a, const CTxIn& b)
89  {
90  return !(a == b);
91  }
92 
93  std::string ToString() const;
94 };
95 
99 class CTxOut
100 {
101 public:
102  CAmount nValue;
103  CScript scriptPubKey;
104 
105  CTxOut()
106  {
107  SetNull();
108  }
109 
110  CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
111 
112  ADD_SERIALIZE_METHODS;
113 
114  template <typename Stream, typename Operation>
115  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
116  READWRITE(nValue);
117  READWRITE(scriptPubKey);
118  }
119 
120  void SetNull()
121  {
122  nValue = -1;
123  scriptPubKey.clear();
124  }
125 
126  bool IsNull() const
127  {
128  return (nValue == -1);
129  }
130 
131  uint256 GetHash() const;
132 
133  CAmount GetDustThreshold(const CFeeRate &minRelayTxFee) const
134  {
135  // TODO : should get from blockchain param, "minimum-per-output"
136  static int64_t MCP_MINIMUM_PER_OUTPUT = 0;
137  int64_t minOutput=MCP_MINIMUM_PER_OUTPUT;
138  if(minOutput >= 0)
139  {
140  return minOutput;
141  }
142  size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
143  return 3*minRelayTxFee.GetFee(nSize);
144  }
145 
146  bool IsDust(CFeeRate minRelayTxFee) const
147  {
148  // "Dust" is defined in terms of CTransaction::minRelayTxFee,
149  // which has units satoshis-per-kilobyte.
150  // If you'd pay more than 1/3 in fees
151  // to spend something, then we consider it dust.
152  // A typical txout is 34 bytes big, and will
153  // need a CTxIn of at least 148 bytes to spend:
154  // so dust is a txout less than 546 satoshis
155  // with default minRelayTxFee.
156  /*
157  int64_t minOutput=MCP_MINIMUM_PER_OUTPUT;
158  if(minOutput >= 0)
159  {
160  return (nValue < minOutput);
161  }
162  size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
163  return (nValue < 3*minRelayTxFee.GetFee(nSize));
164  */
165  return (nValue < GetDustThreshold(minRelayTxFee));
166  }
167 
168  friend bool operator==(const CTxOut& a, const CTxOut& b)
169  {
170  return (a.nValue == b.nValue &&
171  a.scriptPubKey == b.scriptPubKey);
172  }
173 
174  friend bool operator!=(const CTxOut& a, const CTxOut& b)
175  {
176  return !(a == b);
177  }
178 
179  std::string ToString() const;
180 };
181 
182 struct CMutableTransaction;
183 
188 {
189 private:
191  const uint256 hash;
192  void UpdateHash() const;
193 
194 public:
195  static const int32_t CURRENT_VERSION=1;
196 
197  // The local variables are made const to prevent unintended modification
198  // without updating the cached hash value. However, CTransaction is not
199  // actually immutable; deserialization and assignment are implemented,
200  // and bypass the constness. This is safe, as they update the entire
201  // structure, including the hash.
202  const int32_t nVersion;
203  const std::vector<CTxIn> vin;
204  const std::vector<CTxOut> vout;
205  const uint32_t nLockTime;
206 
208  CTransaction();
209 
212 
213  CTransaction& operator=(const CTransaction& tx);
214 
215  ADD_SERIALIZE_METHODS;
216 
217  template <typename Stream, typename Operation>
218  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
219  READWRITE(*const_cast<int32_t*>(&this->nVersion));
220  nVersion = this->nVersion;
221  READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
222  READWRITE(*const_cast<std::vector<CTxOut>*>(&vout));
223  READWRITE(*const_cast<uint32_t*>(&nLockTime));
224  if (ser_action.ForRead())
225  UpdateHash();
226  }
227 
228  bool IsNull() const {
229  return vin.empty() && vout.empty();
230  }
231 
232  const uint256& GetHash() const {
233  return hash;
234  }
235 
236  // Return sum of txouts.
237  CAmount GetValueOut() const;
238  // GetValueIn() is a method on CCoinsViewCache, because
239  // inputs must be known to compute value in.
240 
241  // Compute priority, given priority of inputs and (optionally) tx size
242  double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
243 
244  // Compute modified tx size for priority calculation (optionally given tx size)
245  unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const;
246 
247  bool IsCoinBase() const
248  {
249  return (vin.size() == 1 && vin[0].prevout.IsNull());
250  }
251 
252  friend bool operator==(const CTransaction& a, const CTransaction& b)
253  {
254  return a.hash == b.hash;
255  }
256 
257  friend bool operator!=(const CTransaction& a, const CTransaction& b)
258  {
259  return a.hash != b.hash;
260  }
261 
262  std::string ToString() const;
263 };
264 
267 {
268  int32_t nVersion;
269  std::vector<CTxIn> vin;
270  std::vector<CTxOut> vout;
271  uint32_t nLockTime;
272 
275 
276  ADD_SERIALIZE_METHODS;
277 
278  template <typename Stream, typename Operation>
279  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
280  READWRITE(this->nVersion);
281  nVersion = this->nVersion;
282  READWRITE(vin);
283  READWRITE(vout);
284  READWRITE(nLockTime);
285  }
286 
290  uint256 GetHash() const;
291 };
292 
293 std::string EncodeHexTx(const CTransaction& tx);
294 bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx);
295 
296 #endif // TRANSACTION_H
Definition: transaction.h:52
Definition: transaction.h:99
Definition: transaction.h:11
Definition: uint256.h:294
Definition: script.h:336
Definition: amount.h:19
Definition: transaction.h:266
Definition: transaction.h:187