Skip to content

Commit 3c9f7ff

Browse files
committed
Merge pull request forknote#1 from LucasJones/dev
Initial boolberry support
2 parents e421f31 + e7edc17 commit 3c9f7ff

7 files changed

Lines changed: 216 additions & 11 deletions

File tree

binding.gyp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88
"src/crypto/tree-hash.c",
99
"src/crypto/hash.c",
1010
"src/crypto/keccak.c",
11-
"src/common/base58.cpp"
11+
"src/common/base58.cpp",
1212
],
1313
"include_dirs": [
1414
"src",
15-
"src/contrib/epee/include"
15+
"src/contrib/epee/include",
1616
],
1717
"link_settings": {
1818
"libraries": [
1919
"-lboost_system",
20-
"-lboost_date_time"
20+
"-lboost_date_time",
2121
]
2222
},
2323
"cflags_cc!": [ "-fno-exceptions", "-fno-rtti" ],
2424
"cflags_cc": [
2525
"-std=c++0x",
2626
"-fexceptions",
27-
"-frtti"
28-
]
27+
"-frtti",
28+
],
2929
}
3030
]
3131
}

src/cryptonote_core/cryptonote_basic.h

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ namespace cryptonote
5959
crypto::public_key key;
6060
};
6161

62+
#pragma pack(push, 1)
63+
struct bb_txout_to_key
64+
{
65+
bb_txout_to_key() { }
66+
bb_txout_to_key(const crypto::public_key &_key) : key(_key) { }
67+
crypto::public_key key;
68+
uint8_t mix_attr;
69+
};
70+
#pragma pack(pop)
71+
6272

6373
/* inputs */
6474

@@ -116,6 +126,7 @@ namespace cryptonote
116126
typedef boost::variant<txin_gen, txin_to_script, txin_to_scripthash, txin_to_key> txin_v;
117127

118128
typedef boost::variant<txout_to_script, txout_to_scripthash, txout_to_key> txout_target_v;
129+
typedef boost::variant<txout_to_script, txout_to_scripthash, bb_txout_to_key> bb_txout_target_v;
119130

120131
//typedef std::pair<uint64_t, txout> out_t;
121132
struct tx_out
@@ -127,8 +138,16 @@ namespace cryptonote
127138
VARINT_FIELD(amount)
128139
FIELD(target)
129140
END_SERIALIZE()
141+
};
142+
struct bb_tx_out
143+
{
144+
uint64_t amount;
145+
bb_txout_target_v target;
130146

131-
147+
BEGIN_SERIALIZE_OBJECT()
148+
VARINT_FIELD(amount)
149+
FIELD(target)
150+
END_SERIALIZE()
132151
};
133152

134153
class transaction_prefix
@@ -204,6 +223,49 @@ namespace cryptonote
204223
static size_t get_signature_size(const txin_v& tx_in);
205224
};
206225

226+
class bb_transaction_prefix
227+
{
228+
229+
public:
230+
// tx information
231+
size_t version;
232+
uint64_t unlock_time; //number of block (or time), used as a limitation like: spend this tx not early then block/time
233+
234+
std::vector<txin_v> vin;
235+
std::vector<bb_tx_out> vout;
236+
//extra
237+
std::vector<uint8_t> extra;
238+
239+
BEGIN_SERIALIZE()
240+
VARINT_FIELD(version)
241+
VARINT_FIELD(unlock_time)
242+
FIELD(vin)
243+
FIELD(vout)
244+
FIELD(extra)
245+
END_SERIALIZE()
246+
247+
248+
protected:
249+
bb_transaction_prefix(){}
250+
};
251+
252+
class bb_transaction: public bb_transaction_prefix
253+
{
254+
public:
255+
std::vector<std::vector<crypto::signature> > signatures; //count signatures always the same as inputs count
256+
257+
bb_transaction();
258+
virtual ~bb_transaction();
259+
void set_null();
260+
261+
BEGIN_SERIALIZE_OBJECT()
262+
FIELDS(*static_cast<bb_transaction_prefix *>(this))
263+
FIELD(signatures)
264+
END_SERIALIZE()
265+
266+
static size_t get_signature_size(const txin_v& tx_in);
267+
};
268+
207269

208270
inline
209271
transaction::transaction()
@@ -242,6 +304,43 @@ namespace cryptonote
242304
return boost::apply_visitor(txin_signature_size_visitor(), tx_in);
243305
}
244306

307+
inline
308+
bb_transaction::bb_transaction()
309+
{
310+
set_null();
311+
}
312+
313+
inline
314+
bb_transaction::~bb_transaction()
315+
{
316+
//set_null();
317+
}
318+
319+
inline
320+
void bb_transaction::set_null()
321+
{
322+
version = 0;
323+
unlock_time = 0;
324+
vin.clear();
325+
vout.clear();
326+
extra.clear();
327+
signatures.clear();
328+
}
329+
330+
inline
331+
size_t bb_transaction::get_signature_size(const txin_v& tx_in)
332+
{
333+
struct txin_signature_size_visitor : public boost::static_visitor<size_t>
334+
{
335+
size_t operator()(const txin_gen& txin) const{return 0;}
336+
size_t operator()(const txin_to_script& txin) const{return 0;}
337+
size_t operator()(const txin_to_scripthash& txin) const{return 0;}
338+
size_t operator()(const txin_to_key& txin) const {return txin.key_offsets.size();}
339+
};
340+
341+
return boost::apply_visitor(txin_signature_size_visitor(), tx_in);
342+
}
343+
245344

246345

247346
/************************************************************************/
@@ -257,7 +356,6 @@ namespace cryptonote
257356

258357
BEGIN_SERIALIZE()
259358
VARINT_FIELD(major_version)
260-
if(major_version > CURRENT_BLOCK_MAJOR_VERSION) return false;
261359
VARINT_FIELD(minor_version)
262360
VARINT_FIELD(timestamp)
263361
FIELD(prev_id)
@@ -277,6 +375,37 @@ namespace cryptonote
277375
END_SERIALIZE()
278376
};
279377

378+
struct bb_block_header
379+
{
380+
uint8_t major_version;
381+
uint8_t minor_version;
382+
uint64_t timestamp;
383+
crypto::hash prev_id;
384+
uint64_t nonce;
385+
uint8_t flags;
386+
387+
BEGIN_SERIALIZE()
388+
FIELD(major_version)
389+
FIELD(nonce)
390+
FIELD(prev_id)
391+
VARINT_FIELD(minor_version)
392+
VARINT_FIELD(timestamp)
393+
FIELD(flags)
394+
END_SERIALIZE()
395+
};
396+
397+
struct bb_block: public bb_block_header
398+
{
399+
bb_transaction miner_tx;
400+
std::vector<crypto::hash> tx_hashes;
401+
402+
BEGIN_SERIALIZE()
403+
FIELDS(*static_cast<bb_block_header *>(this))
404+
FIELD(miner_tx)
405+
FIELD(tx_hashes)
406+
END_SERIALIZE()
407+
};
408+
280409

281410
/************************************************************************/
282411
/* */
@@ -314,6 +443,7 @@ namespace cryptonote
314443
}
315444

316445
BLOB_SERIALIZER(cryptonote::txout_to_key);
446+
BLOB_SERIALIZER(cryptonote::bb_txout_to_key);
317447
BLOB_SERIALIZER(cryptonote::txout_to_scripthash);
318448

319449
VARIANT_TAG(binary_archive, cryptonote::txin_gen, 0xff);
@@ -323,6 +453,7 @@ VARIANT_TAG(binary_archive, cryptonote::txin_to_key, 0x2);
323453
VARIANT_TAG(binary_archive, cryptonote::txout_to_script, 0x0);
324454
VARIANT_TAG(binary_archive, cryptonote::txout_to_scripthash, 0x1);
325455
VARIANT_TAG(binary_archive, cryptonote::txout_to_key, 0x2);
456+
VARIANT_TAG(binary_archive, cryptonote::bb_txout_to_key, 0x2);
326457
VARIANT_TAG(binary_archive, cryptonote::transaction, 0xcc);
327458
VARIANT_TAG(binary_archive, cryptonote::block, 0xbb);
328459

@@ -333,6 +464,7 @@ VARIANT_TAG(json_archive, cryptonote::txin_to_key, "key");
333464
VARIANT_TAG(json_archive, cryptonote::txout_to_script, "script");
334465
VARIANT_TAG(json_archive, cryptonote::txout_to_scripthash, "scripthash");
335466
VARIANT_TAG(json_archive, cryptonote::txout_to_key, "key");
467+
VARIANT_TAG(json_archive, cryptonote::bb_txout_to_key, "key");
336468
VARIANT_TAG(json_archive, cryptonote::transaction, "tx");
337469
VARIANT_TAG(json_archive, cryptonote::block, "block");
338470

@@ -343,5 +475,6 @@ VARIANT_TAG(debug_archive, cryptonote::txin_to_key, "key");
343475
VARIANT_TAG(debug_archive, cryptonote::txout_to_script, "script");
344476
VARIANT_TAG(debug_archive, cryptonote::txout_to_scripthash, "scripthash");
345477
VARIANT_TAG(debug_archive, cryptonote::txout_to_key, "key");
478+
VARIANT_TAG(debug_archive, cryptonote::bb_txout_to_key, "key");
346479
VARIANT_TAG(debug_archive, cryptonote::transaction, "tx");
347480
VARIANT_TAG(debug_archive, cryptonote::block, "block");

src/cryptonote_core/cryptonote_boost_serialization.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ namespace boost
6868
a & x.key;
6969
}
7070

71+
72+
template <class Archive>
73+
inline void serialize(Archive &a, cryptonote::bb_txout_to_key &x, const boost::serialization::version_type ver)
74+
{
75+
a & x.key;
76+
a & x.mix_attr;
77+
}
78+
7179
template <class Archive>
7280
inline void serialize(Archive &a, cryptonote::txout_to_scripthash &x, const boost::serialization::version_type ver)
7381
{
@@ -124,7 +132,6 @@ namespace boost
124132
a & x.signatures;
125133
}
126134

127-
128135
template <class Archive>
129136
inline void serialize(Archive &a, cryptonote::block &b, const boost::serialization::version_type ver)
130137
{

src/cryptonote_core/cryptonote_format_utils.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,13 @@ namespace cryptonote
586586
size_t blob_size = 0;
587587
return get_object_hash(t, res, blob_size);
588588
}
589+
590+
bool get_transaction_hash(const bb_transaction& t, crypto::hash& res)
591+
{
592+
size_t blob_size = 0;
593+
return get_object_hash(t, res, blob_size);
594+
}
595+
589596
//---------------------------------------------------------------
590597
bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t& blob_size)
591598
{
@@ -600,6 +607,14 @@ namespace cryptonote
600607
blob.append(tools::get_varint_data(b.tx_hashes.size()+1));
601608
return blob;
602609
}
610+
blobdata get_block_hashing_blob(const bb_block& b)
611+
{
612+
blobdata blob = t_serializable_object_to_blob(static_cast<bb_block_header>(b));
613+
crypto::hash tree_root_hash = get_tx_tree_hash(b);
614+
blob.append((const char*)&tree_root_hash, sizeof(tree_root_hash ));
615+
blob.append(tools::get_varint_data(b.tx_hashes.size()+1));
616+
return blob;
617+
}
603618
//---------------------------------------------------------------
604619
bool get_block_hash(const block& b, crypto::hash& res)
605620
{
@@ -684,6 +699,15 @@ namespace cryptonote
684699
CHECK_AND_ASSERT_MES(r, false, "Failed to parse block from blob");
685700
return true;
686701
}
702+
bool parse_and_validate_block_from_blob(const blobdata& b_blob, bb_block& b)
703+
{
704+
std::stringstream ss;
705+
ss << b_blob;
706+
binary_archive<false> ba(ss);
707+
bool r = ::serialization::serialize(ba, b);
708+
CHECK_AND_ASSERT_MES(r, false, "Failed to parse block from blob");
709+
return true;
710+
}
687711
//---------------------------------------------------------------
688712
blobdata block_to_blob(const block& b)
689713
{
@@ -728,5 +752,15 @@ namespace cryptonote
728752
txs_ids.push_back(th);
729753
return get_tx_tree_hash(txs_ids);
730754
}
755+
crypto::hash get_tx_tree_hash(const bb_block& b)
756+
{
757+
std::vector<crypto::hash> txs_ids;
758+
crypto::hash h = null_hash;
759+
get_transaction_hash(b.miner_tx, h);
760+
txs_ids.push_back(h);
761+
BOOST_FOREACH(auto& th, b.tx_hashes)
762+
txs_ids.push_back(th);
763+
return get_tx_tree_hash(txs_ids);
764+
}
731765
//---------------------------------------------------------------
732766
}

src/cryptonote_core/cryptonote_format_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ namespace cryptonote
7575
bool get_transaction_hash(const transaction& t, crypto::hash& res);
7676
bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t& blob_size);
7777
blobdata get_block_hashing_blob(const block& b);
78+
blobdata get_block_hashing_blob(const bb_block& b);
7879
bool get_block_hash(const block& b, crypto::hash& res);
7980
crypto::hash get_block_hash(const block& b);
8081
bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height);
8182
crypto::hash get_block_longhash(const block& b, uint64_t height);
8283
bool generate_genesis_block(block& bl);
8384
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b);
85+
bool parse_and_validate_block_from_blob(const blobdata& b_blob, bb_block& b);
8486
bool get_inputs_money_amount(const transaction& tx, uint64_t& money);
8587
uint64_t get_outs_money_amount(const transaction& tx);
8688
bool check_inputs_types_supported(const transaction& tx);
@@ -196,6 +198,7 @@ namespace cryptonote
196198
void get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes, crypto::hash& h);
197199
crypto::hash get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes);
198200
crypto::hash get_tx_tree_hash(const block& b);
201+
crypto::hash get_tx_tree_hash(const bb_block& b);
199202

200203
#define CHECKED_GET_SPECIFIC_VARIANT(variant_var, specific_type, variable_name, fail_return_val) \
201204
CHECK_AND_ASSERT_MES(variant_var.type() == typeid(specific_type), fail_return_val, "wrong variant type: " << variant_var.type().name() << ", expected " << typeid(specific_type).name()); \

src/main.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,31 @@ Handle<Value> convert_blob(const Arguments& args) {
5252
return except("Failed to parse block");
5353
}
5454
output = get_block_hashing_blob(b);
55-
// crypto::hash tree_root_hash = get_tx_tree_hash(b);
56-
// blob.append((const char*)&tree_root_hash, sizeof(tree_root_hash ));
57-
// blob.append(tools::get_varint_data(b.tx_hashes.size()+1));
55+
56+
Buffer* buff = Buffer::New(output.data(), output.size());
57+
return scope.Close(buff->handle_);
58+
}
59+
60+
Handle<Value> convert_blob_bb(const Arguments& args) {
61+
HandleScope scope;
62+
63+
if (args.Length() < 1)
64+
return except("You must provide one argument.");
65+
66+
Local<Object> target = args[0]->ToObject();
67+
68+
if (!Buffer::HasInstance(target))
69+
return except("Argument should be a buffer object.");
70+
71+
blobdata input = std::string(Buffer::Data(target), Buffer::Length(target));
72+
blobdata output = "";
73+
74+
//convert
75+
bb_block b = AUTO_VAL_INIT(b);
76+
if (!parse_and_validate_block_from_blob(input, b)) {
77+
return except("Failed to parse block");
78+
}
79+
output = get_block_hashing_blob(b);
5880

5981
Buffer* buff = Buffer::New(output.data(), output.size());
6082
return scope.Close(buff->handle_);
@@ -87,6 +109,7 @@ Handle<Value> address_decode(const Arguments& args) {
87109

88110
void init(Handle<Object> exports) {
89111
exports->Set(String::NewSymbol("convert_blob"), FunctionTemplate::New(convert_blob)->GetFunction());
112+
exports->Set(String::NewSymbol("convert_blob_bb"), FunctionTemplate::New(convert_blob_bb)->GetFunction());
90113
exports->Set(String::NewSymbol("address_decode"), FunctionTemplate::New(address_decode)->GetFunction());
91114
}
92115

0 commit comments

Comments
 (0)