plebble
tx.h
Go to the documentation of this file.
1 /*
2 -------------------------------------------------------------------------------
3  PLEBBLE
4 -------------------------------------------------------------------------------
5  Copyright (C) 2019-2020 KATLAS Technology. (http://katlastechnology.com)
6  Copyright (C) 2017-2020 Marcos Mayorga. (mm@mm-studios.com)
7 
8  This file is part of our Plebble(R) Platform.
9 
10  The code below cannot be copied, used for any purpose other than the one
11  agreed and/or distributed without the express permission of
12  KATLAS Technology.
13 -------------------------------------------------------------------------------
14 
15 
16 */
17 #ifndef USGOV_33de4fece6646b4a064a30507e7a1a35c3b5bcd4dc9c1c87059904e7ac05e335
18 #define USGOV_33de4fece6646b4a064a30507e7a1a35c3b5bcd4dc9c1c87059904e7ac05e335
19 
20 #include <us/gov/config.h>
21 #include <us/gov/auth.h>
22 #include <us/gov/engine.h>
23 #include <vector>
24 #include <thread>
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <us/gov/crypto/crypto.h>
28 #include "protocol.h"
29 #include <chrono>
30 #include <cassert>
31 #include <memory>
32 #include <map>
33 
34 namespace us { namespace gov {
35 namespace cash {
36  using namespace std;
37  using socket::datagram;
38  using engine::local_deltas;
39  using crypto::ripemd160;
40  using crypto::ec;
41  using engine::peer_t;
42  typedef crypto::ec::keys keys;
43  typedef keys::pub_t pubkey_t;
44 
45  struct tx final:engine::evidence {
47  typedef int64_t cash_t;
48 
49  static constexpr uint16_t magic{200};
50 
52  struct side_t: vector<uint8_t> {
53  void to_stream(ostream& os) const;
54  static void from_stream(side_t&, istream& is);
55  void dump(const string& prefix, ostream&) const;
56  };
59  void to_stream(ostream& os) const;
60  static void from_stream(sigcode_section_t&, istream& is);
61  void dump(const string& prefix, ostream&) const;
62  };
63 
64  struct sigcode_t:vector<sigcode_section_t> {
65  void to_stream(ostream& os) const;
66  static bool from_stream(sigcode_t&, istream& is);
67  void dump(ostream&) const;
68  string to_b58() const;
69  static pair<ko,sigcode_t> from_b58(const string&);
70 
71  bool ts{true};
72  };
73 
74  struct input_t final {
75  input_t() {}
76  input_t(const hash_t& address, const cash_t& amount): address(address), amount(amount) {}
77  input_t(const hash_t& address, const cash_t& amount, const string& locking_program_input): address(address), amount(amount), locking_program_input(locking_program_input) {}
79  void to_stream(ostream&) const;
80  void write_pretty(const string& prefix, ostream& os) const;
81  static pair<ko,input_t> from_stream(istream&);
82 
86  };
87 
88  struct output_t final {
89  output_t() {}
90  output_t(const hash_t& address, const cash_t& amount): address(address), amount(amount) {}
92  void to_stream(ostream&) const;
93  void write_pretty(const string& prefix, ostream& os) const;
94  static pair<ko,output_t> from_stream(istream&);
95 
98  };
99 
100  struct inputs_t final:vector<input_t> {
102  void to_stream(ostream&) const;
103  void write_pretty(const string& prefix, ostream& os) const;
104  ko from_stream(istream&);
105  };
106 
107  struct outputs_t final:vector<output_t> {
109  void to_stream(ostream&) const;
110  void write_pretty(const string& prefix, ostream& os) const;
111  ko from_stream(istream&);
112  };
113 
114  tx();
115  ~tx() override {}
116 
118  static pair<ko,tx*> from_stream(istream&);
119  static pair<ko,tx*> from_b58(const string&);
120  string to_b58() const;
122  hash_t compute_hash() const override;
123  string name() const override { return "cash::tx"; };
124  bool check_amounts() const;
126  void to_stream(ostream&) const;
127  void write_sigmsg(ec::sigmsg_hasher_t&, const sigcode_t& sh) const;
128  void write_pretty(ostream&) const override;
129  datagram* get_datagram(uint16_t seq) const;
130  void dump(const string& prefix, ostream&) const;
131 
132  struct section_t final {
134  section_t(const hash_t& token): token(token) {}
135  void add_input(const hash_t& addr, const cash_t& amount);
136  void add_input(const hash_t& addr, const cash_t& amount, const string& locking_program_input);
137  void add_output(const hash_t& addr, const cash_t& amount);
139  void write_pretty(const string& prefix, ostream& os) const;
140  void to_stream(ostream&) const;
141  ko from_stream(istream&);
144  bool check_amounts() const;
148  };
149 
150  struct sections_t final: vector<section_t> {
152  void write_pretty(const string& prefix, ostream& os) const;
153  void to_stream(ostream&) const;
154  ko from_stream(istream&);
155  bool check_amounts() const;
156  const_iterator find(const hash_t& token) const;
157  iterator find(const hash_t& token);
158  };
159 
160  bool verify(ostream& os) const override;
161  section_t& add_section(const hash_t& token);
163  };
164 
165  static ostream& operator << (ostream&os, const cash::tx& t) {
166  os << t.to_b58();
167  return os;
168  }
169 }
170 }}
171 
172 #endif
173 
app.h
us::gov::cash::tx::sigcode_section_t::outputs
side_t outputs
Definition: tx.h:58
us::gov::cash::tx::section_t::add_output
void add_output(const hash_t &addr, const cash_t &amount)
us::gov::cash::tx::output_t
Definition: tx.h:88
us::gov::cash::tx::sigcode_t::from_stream
static bool from_stream(sigcode_t &, istream &is)
us::gov::cash::tx::write_pretty
void write_pretty(ostream &) const override
us::gov::cash::tx::inputs_t::write_pretty
void write_pretty(const string &prefix, ostream &os) const
p2pkh.h
us::gov::cash::tx::cash_t
int64_t cash_t
Definition: tx.h:47
us::gov::cash::tx::outputs_t::write_sigmsg
void write_sigmsg(ec::sigmsg_hasher_t &, const sigcode_section_t &) const
us::gov::cash::tx::section_t::write_pretty
void write_pretty(const string &prefix, ostream &os) const
us::gov::cash::tx::sigcode_section_t::side_t::to_stream
void to_stream(ostream &os) const
us::gov::cash::tx::tx
tx()
us::gov::cash::tx::section_t::from_stream
ko from_stream(istream &)
us::gov::cash::tx::section_t::write_sigmsg
void write_sigmsg(ec::sigmsg_hasher_t &, const sigcode_section_t &) const
us.ko
Definition: ko.java:20
us::gov::crypto::ripemd160::finalize
void finalize(unsigned char hash[output_size])
us::gov::crypto::sha256::write
void write(const unsigned char *data, size_t len)
us::gov::cash::tx::output_t::address
hash_t address
Definition: tx.h:96
us::gov::cash::tx::output_t::output_t
output_t(const hash_t &address, const cash_t &amount)
Definition: tx.h:90
us::gov::cash::tx::name
string name() const override
Definition: tx.h:123
us::gov::cash::tx::input_t::write_pretty
void write_pretty(const string &prefix, ostream &os) const
us::gov::cash::tx::section_t::inputs
inputs_t inputs
Definition: tx.h:146
us::gov::cash::tx::sigcode_section_t::side_t::from_stream
static void from_stream(side_t &, istream &is)
us::gov::cash::tx::verify
bool verify(ostream &os) const override
protocol.h
us::gov::cash::tx::sigcode_section_t::dump
void dump(const string &prefix, ostream &) const
us::gov::crypto::b58::decode_string
std::string decode_string(const std::string &s)
Definition: base58.cpp:44
us::gov::dfs::peer_t
relay::peer_t peer_t
Definition: daemon.h:31
us
Definition: daemon.h:22
us::gov::crypto::ripemd160::value_type
Definition: ripemd160.h:38
us::gov::cash::tx::sections_t::write_pretty
void write_pretty(const string &prefix, ostream &os) const
us::gov::cash::tx::get_hash
ec::sigmsg_hasher_t::value_type get_hash(const sigcode_t &) const
unlikely
#define unlikely(x)
Definition: likely.h:30
us::gov::cash::tx::output_t::write_pretty
void write_pretty(const string &prefix, ostream &os) const
us::gov::cash::tx::sigcode_t::from_b58
static pair< ko, sigcode_t > from_b58(const string &)
us::gov::cash::tx::sigcode_section_t
Definition: tx.h:51
us::gov::cash::pubkey_t
keys::pub_t pubkey_t
Definition: app.h:53
us::gov::cash::tx::compute_hash
hash_t compute_hash() const override
us::gov::cash::tx::get_datagram
datagram * get_datagram(uint16_t seq) const
us::gov::crypto::sha256::value_type
Definition: sha256.h:38
us::gov::cash::tx::input_t::input_t
input_t()
Definition: tx.h:75
us::gov::cash
Definition: app.h:43
us::gov::cash::tx::section_t::add_input
void add_input(const hash_t &addr, const cash_t &amount)
us::gov::cash::tx::sigcode_t
Definition: tx.h:64
likely.h
us::gov::cash::tx::sigcode_t::dump
void dump(ostream &) const
us::gov::cash::tx::sigcode_section_t::side_t::dump
void dump(const string &prefix, ostream &) const
us::gov::cash::tx::sections_t::write_sigmsg
void write_sigmsg(ec::sigmsg_hasher_t &, const sigcode_t &) const
us::gov::cash::tx::from_b58
static pair< ko, tx * > from_b58(const string &)
us::gov::cash::tx::outputs_t::from_stream
ko from_stream(istream &)
us::gov::cash::tx::input_t::input_t
input_t(const hash_t &address, const cash_t &amount, const string &locking_program_input)
Definition: tx.h:77
us::gov::cash::tx::b
engine::evidence b
Definition: tx.h:46
us::gov::cash::tx::sigcode_section_t::inputs
side_t inputs
Definition: tx.h:57
us::gov::cash::app
Definition: app.h:65
us::gov::cash::tx::inputs_t::to_stream
void to_stream(ostream &) const
us::gov::cash::tx::sigcode_section_t::to_stream
void to_stream(ostream &os) const
us::gov::cash::tx::section_t
Definition: tx.h:132
us::gov::socket::datagram
Definition: datagram.h:44
us::gov::crypto::sha256
Definition: sha256.h:33
us::gov::cash::tx::output_t::write_sigmsg
void write_sigmsg(ec::sigmsg_hasher_t &) const
us::gov::cash::tx::output_t::amount
cash_t amount
Definition: tx.h:97
us::gov::cash::cash_t
tx::cash_t cash_t
Definition: app.h:56
us::gov::cash::tx::outputs_t::to_stream
void to_stream(ostream &) const
us::gov::cash::tx::input_t
Definition: tx.h:74
us::gov::cash::tx::sections
sections_t sections
Definition: tx.h:162
us::gov::cash::tx::output_t::to_stream
void to_stream(ostream &) const
us::gov::cash::tx::sigcode_t::to_b58
string to_b58() const
us::gov::cash::tx::check_amounts
bool check_amounts() const
us::gov::cash::tx::input_t::input_t
input_t(const hash_t &address, const cash_t &amount)
Definition: tx.h:76
us::gov::cash::tx::outputs_t::write_pretty
void write_pretty(const string &prefix, ostream &os) const
us::gov::engine::auth::pub_t
peer::daemon::pub_t pub_t
Definition: auth_app.h:53
us::gov::cash::tx::inputs_t::write_sigmsg
void write_sigmsg(ec::sigmsg_hasher_t &, const sigcode_section_t &) const
us::gov::cash::tx::input_t::locking_program_input
string locking_program_input
Definition: tx.h:85
us::gov::cash::tx::section_t::section_t
section_t(const hash_t &token)
Definition: tx.h:134
us::gov::cash::tx::get_sigcode_all
sigcode_t get_sigcode_all() const
us::gov::cash::tx::make_sigcode_all
sigcode_t make_sigcode_all() const
us::gov::cash::tx::section_t::check_amounts
bool check_amounts() const
us::gov
Definition: daemon.h:22
us::gov::cash::tx::add_section
section_t & add_section(const hash_t &token)
us::ko
const ko_t * ko
Definition: ko.h:27
us::gov::crypto::ripemd160
Definition: ripemd160.h:34
us::gov::cash::tx::input_t::amount
cash_t amount
Definition: tx.h:84
us::gov::cash::tx::sections_t::from_stream
ko from_stream(istream &)
us::ok
static constexpr ko ok
Definition: ko.h:28
us::gov::cash::tx::sections_t::to_stream
void to_stream(ostream &) const
us::gov::cash::tx::section_t::add_input
void add_input(const hash_t &addr, const cash_t &amount, const string &locking_program_input)
us::gov::cash::tx::section_t::required_input
cash_t required_input() const
us::gov::cash::keys
crypto::ec::keys keys
Definition: app.h:52
us::gov::cash::tx::input_t::address
hash_t address
Definition: tx.h:83
peer.h
us::gov::cash::tx::input_t::write_sigmsg
void write_sigmsg(ec::sigmsg_hasher_t &) const
us::gov::cash::tx::write_sigmsg
void write_sigmsg(ec::sigmsg_hasher_t &, const sigcode_t &sh) const
us::gov::cash::tx::sections_t
Definition: tx.h:150
us::gov::cash::tx::section_t::to_stream
void to_stream(ostream &) const
us::gov::cash::tx::output_t::from_stream
static pair< ko, output_t > from_stream(istream &)
std
Definition: app.h:380
auth.h
us::gov::crypto::b58::encode
std::string encode(const std::string &s)
Definition: base58.cpp:62
us::gov::cash::tx
Definition: tx.h:45
us::gov::cash::tx::inputs_t::from_stream
ko from_stream(istream &)
config.h
us::gov::cash::tx::to_stream
void to_stream(ostream &) const
base58.h
c
us::gov::cash::tx c
Definition: tx.cpp:32
engine.h
us::gov::crypto::ripemd160::write
void write(const vector< unsigned char > &)
us::gov::cash::tx::inputs_t
Definition: tx.h:100
us::gov::cash::tx::sections_t::check_amounts
bool check_amounts() const
us::gov::cash::tx::sections_t::find
iterator find(const hash_t &token)
us::gov::cash::tx::input_t::to_stream
void to_stream(ostream &) const
us::gov::relay::evidence
Definition: evidence.h:31
us::gov::cash::tx::sigcode_section_t::from_stream
static void from_stream(sigcode_section_t &, istream &is)
us::gov::cash::tx::dump
void dump(const string &prefix, ostream &) const
us::gov::cash::tx::input_t::from_stream
static pair< ko, input_t > from_stream(istream &)
us::gov::cash::tx::section_t::outputs
outputs_t outputs
Definition: tx.h:147
us.pair
Definition: pair.java:19
crypto.h
gov.h
us::gov::crypto::sha256::finalize
void finalize(unsigned char hash[output_size])
us::gov::cash::tx::section_t::token
hash_t token
Definition: tx.h:145
us::gov::cash::tx::output_t::output_t
output_t()
Definition: tx.h:89
us::gov::cash::tx::sections_t::find
const_iterator find(const hash_t &token) const
us::gov::cash::tx::outputs_t
Definition: tx.h:107
us::gov::cash::tx::sigcode_t::to_stream
void to_stream(ostream &os) const
us::gov::cash::tx::from_stream
static pair< ko, tx * > from_stream(istream &)
us::gov::cash::tx::~tx
~tx() override
Definition: tx.h:115
log
#define log
Definition: gov.h:83
us::gov::cash::tx::section_t::section_t
section_t()
Definition: tx.h:133
us::gov::cash::tx::section_t::required_output
cash_t required_output() const
UGAS
#define UGAS
Definition: config.h:22
us::gov::cash::tx::to_b58
string to_b58() const
us::gov::cash::tx::sigcode_section_t::side_t
Definition: tx.h:52
tx.h
us::gov::cash::operator<<
static ostream & operator<<(ostream &os, const cash::file_tx &t)
Definition: file_tx.h:71