-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathnode.rs
1875 lines (1703 loc) · 59 KB
/
node.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(clippy::style)]
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{mpsc, Arc, Mutex};
use std::thread::JoinHandle;
use bit_vec::BitVec;
use primitive_types::U256;
use priority_queue::PriorityQueue;
use rand::seq::IteratorRandom;
use serde::{Deserialize, Serialize};
use sha3::Digest;
use thiserror::Error;
use kindelia_common::crypto::{self, Hashed, Keccakable};
use kindelia_common::Name;
use kindelia_lang::ast::Statement;
use kindelia_lang::parser;
use crate::api::{self, CtrInfo, RegInfo};
use crate::api::{BlockInfo, FuncInfo, NodeRequest};
use crate::bits;
use crate::bits::ProtoSerialize;
use crate::config::MineConfig;
use crate::constants;
use crate::net::{ProtoAddr, ProtoComm};
use crate::persistence::{BlockStorage, BlockStorageError};
use crate::runtime::*;
use crate::util::*;
use crate::events::{NodeEventEmittedInfo, NodeEventType};
use crate::heartbeat;
macro_rules! emit_event {
($tx: expr, $event: expr) => {
#[cfg(feature = "events")]
if let Some(ref tx) = $tx {
if let Err(_) = tx.send(($event, get_time_micro())) {
eprintln!("Could not send event");
}
}
};
($tx: expr, $event: expr, tags = $($tag:ident),+) => {
#[cfg(feature = "events")]
// #[cfg(any(all, $($tag),+))]
if let Some(ref tx) = $tx {
if let Err(_) = tx.send(($event, get_time_micro())) {
eprintln!("Could not send event");
}
}
};
}
// Block
// =====
// Kindelia's block format is agnostic to HVM. A Transaction is just a vector of
// bytes. A Body groups transactions in a single combined vector of bytes, using
// the following format:
//
// body ::= TX_COUNT | LEN(tx_0) | tx_0 | LEN(tx_1) | tx_1 | ...
//
// TX_COUNT is a single byte storing the number of transactions in this block.
// The length of each transaction is stored using 2 bytes, called LEN.
// Transaction
// -----------
/// Represents transaction inside a block. It's a list of bytes with non-zero
/// multiple-of-5 number of bytes.
#[derive(Debug, Clone, Eq)]
pub struct Transaction {
data: Vec<u8>,
pub hash: U256,
}
/// Transaction's error handling
#[derive(Debug, Error, Serialize, Deserialize)]
pub enum TransactionError {
#[error(
"Transaction size ({len}) is greater than max transaction size ({})",
MAX_TRANSACTION_SIZE
)]
Oversize { len: usize },
}
impl Transaction {
pub fn new(mut data: Vec<u8>) -> Result<Self, TransactionError> {
// Transaction length is always a non-zero multiple of 5
while data.len() == 0 || data.len() % 5 != 0 {
data.push(0);
}
if data.len() > MAX_TRANSACTION_SIZE {
return Err(TransactionError::Oversize { len: data.len() });
}
let hash = hash_bytes(&data);
Ok(Transaction { data, hash })
}
// Encodes a transaction length as a pair of 2 bytes
pub fn encode_length(&self) -> (u8, u8) {
let len = self.data.len() as u16;
let num = (len as u16).reverse_bits();
(((num >> 8) & 0xFF) as u8, (num & 0xFF) as u8)
}
// Decodes an encoded transaction length
fn decode_length(pair: (u8, u8)) -> usize {
(((pair.0 as u16) << 8) | (pair.1 as u16)).reverse_bits() as usize
}
pub fn to_statement(&self) -> Option<Statement> {
Statement::proto_deserialized(&BitVec::from_bytes(&self.data))
}
}
impl Deref for Transaction {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.data
}
}
impl TryFrom<&Statement> for Transaction {
type Error = TransactionError;
fn try_from(stmt: &Statement) -> Result<Self, Self::Error> {
Transaction::new(stmt.proto_serialized().to_bytes())
}
}
impl PartialEq for Transaction {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
}
impl std::hash::Hash for Transaction {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.hash.hash(state);
}
}
// Block body
// ----------
/// Errors associated with Block Body
#[derive(Error, Debug)]
pub enum BlockBodyError {
#[error(
"Block full. no space left for transaction. ({len} exceeds {maxlen})"
)]
BlockFull { len: usize, maxlen: usize },
#[error("Tx count of {count} exceeds limit: {limit}")]
TooManyTx { count: usize, limit: usize },
#[error(transparent)]
Transaction(#[from] TransactionError),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Body {
pub data: Vec<u8>,
}
impl Body {
/// Fills block body with first transactions from iterator that fit.
pub fn fill_from<I, T>(transactions: I) -> Body
where
I: IntoIterator<Item = T>,
T: Into<Transaction>,
{
let mut body_vec = vec![0];
let mut tx_count = 0;
for transaction in transactions.into_iter() {
let transaction = transaction.into();
let tx_len = transaction.data.len();
if tx_len == 0 {
continue;
}
if tx_count + 1 > 255 {
break;
}
if add_transaction_to_body_vec(&mut body_vec, &transaction).is_err() {
break;
}
tx_count += 1;
}
body_vec[0] = (tx_count as u8).reverse_bits();
Body { data: body_vec }
}
/// Build a body from a sequence of transactions.
/// Fails if they can't fit in a block body.
pub fn from_transactions_iter<I, T>(
transactions: I,
) -> Result<Body, BlockBodyError>
where
I: IntoIterator<Item = T>,
T: TryInto<Transaction, Error = TransactionError>,
{
// Reserve a byte in the beginning for the number of transactions
let mut data = vec![0];
let mut tx_count = 0;
for transaction in transactions.into_iter() {
let transaction = transaction.try_into()?;
// Ignore transaction if it's empty
let tx_len = transaction.data.len();
if tx_len == 0 {
continue;
}
// Fails if there's no space left for the transaction
if data.len() + 2 + tx_len > MAX_BODY_SIZE {
return Err(BlockBodyError::BlockFull {
len: data.len() + 2 + tx_len,
maxlen: MAX_BODY_SIZE,
});
}
// Fails if tx count overflows 255, as we store it in a single byte.
if tx_count + 1 > 255 {
return Err(BlockBodyError::TooManyTx {
count: tx_count + 1,
limit: 255,
});
}
tx_count += 1;
// Pair of bytes we will store as the length
let len_bytes = transaction.encode_length();
data.push(len_bytes.0);
data.push(len_bytes.1);
data.extend_from_slice(&transaction.data);
}
// Finally stores resulting transaction count on the first byte
data[0] = (tx_count as u8).reverse_bits();
Ok(Body { data })
}
}
// The Block itself
// ----------------
pub type HashedBlock = Hashed<Block>;
#[derive(Debug, Clone)]
pub struct Block {
/// 32 bytes hash of previous block.
pub prev: U256,
/// Block timestamp.
pub time: u128,
/// Block metadata.
pub meta: u128,
/// Block contents. 1280 bytes max.
pub body: Body,
}
impl Block {
pub fn new(prev: U256, time: u128, meta: u128, body: Body) -> Block {
Block { prev, time, meta, body }
}
}
impl crypto::Keccakable for Block {
fn keccak256(&self) -> crypto::Hash {
let mut bytes: Vec<u8> = Vec::new();
bytes.extend_from_slice(&u256_to_bytes(self.prev));
bytes.extend_from_slice(&u128_to_bytes(self.time));
bytes.extend_from_slice(&u128_to_bytes(self.meta));
bytes.extend_from_slice(&self.body.data);
crypto::Hash::keccak256_from_bytes(&bytes)
}
}
// Node
// ====
/// Blocks have 4 states of inclusion:
///
/// has wait_list? | is on .pending? | is on .block? | meaning
/// -------------- | --------------- | ------------- | ------------------------------------------------------
/// no | no | no | UNSEEN : never seen, may not exist
/// yes | no | no | MISSING : some block cited it, but it wasn't downloaded
/// yes | yes | no | PENDING : downloaded, but waiting ancestors for inclusion
/// no | no | yes | INCLUDED : fully included, as well as all its ancestors
#[derive(Debug, Clone, PartialEq)]
pub enum InclusionState {
UNSEEN,
MISSING,
PENDING,
INCLUDED,
}
// TODO: refactor .block as map to struct? Better safety. Why not?
#[rustfmt::skip]
pub struct Node<C: ProtoComm, S: BlockStorage> {
pub network_id : u32, // Network ID / magic number
pub comm : C, // UDP socket
pub addr : C::Address, // UDP port
pub storage : S, // A `BlockStorage` implementation
pub runtime : Runtime, // Kindelia's runtime
pub query_recv : mpsc::Receiver<NodeRequest<C>>, // Receives an API request
pub pool : PriorityQueue<Transaction, u64>, // transactions to be mined
pub peers : PeersStore<C::Address>, // peers store and state control
pub genesis_hash : U256,
pub tip : U256, // current tip
pub block : U256Map<HashedBlock>, // block hash -> block
pub pending : U256Map<HashedBlock>, // block hash -> downloaded block, waiting for ancestors
pub ancestor : U256Map<U256>, // block hash -> hash of its most recent missing ancestor (shortcut jump table)
pub wait_list : U256Map<Vec<U256>>, // block hash -> hashes of blocks that are waiting for this one
pub children : U256Map<Vec<U256>>, // block hash -> hashes of this block's children
pub work : U256Map<U256>, // block hash -> accumulated work
pub target : U256Map<U256>, // block hash -> this block's target
pub height : U256Map<u128>, // block hash -> cached height
pub results : U256Map<Vec<StatementResult>>, // block hash -> results of the statements in this block
#[cfg(feature = "events")]
pub event_emitter : Option<mpsc::Sender<NodeEventEmittedInfo>>,
pub miner_comm : Option<MinerCommunication>,
}
/// Pool's error handling
#[derive(Debug, Error, Serialize, Deserialize)]
pub enum PoolError {
#[error("Transaction {hash} already included on pool")]
AlreadyIncluded { hash: api::Hash },
}
// Peers
// -----
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct Peer<A: ProtoAddr> {
pub seen_at: u128,
pub address: A,
}
pub struct PeersStore<A: ProtoAddr> {
seen: HashMap<A, Peer<A>>,
active: HashMap<A, Peer<A>>,
}
impl<A: ProtoAddr> PeersStore<A> {
pub fn new() -> PeersStore<A> {
PeersStore { seen: HashMap::new(), active: HashMap::new() }
}
/// This function checks and puts a peer as active on `PeerStore`.
pub fn activate(&mut self, addr: &A, peer: Peer<A>) {
let now = get_time();
// Only activate if its `seen_at` is newer than `now - TIMEOUT`
if peer.seen_at >= now - PEER_TIMEOUT {
self.active.insert(*addr, peer);
}
}
pub fn see_peer(
&mut self,
peer: Peer<A>,
#[cfg(feature = "events")] event_emitter: Option<
mpsc::Sender<NodeEventEmittedInfo>,
>,
) {
let addr = peer.address;
match self.seen.get(&addr) {
// New peer, not seen before
None => {
self.seen.insert(addr, peer);
emit_event!(
event_emitter,
NodeEventType::see_peer_not_seen(&peer),
tags = peers,
see_peer
);
self.activate(&addr, peer);
}
// Peer seen before, but maybe not active
Some(_) => {
let old_peer = self.active.get_mut(&addr);
match old_peer {
// Peer not active, so activate it
None => {
emit_event!(
event_emitter,
NodeEventType::see_peer_activated(&peer),
tags = peers,
see_peer
);
self.activate(&addr, peer);
}
// Peer already active, so update it
Some(old_peer) => {
let new_seen_at = std::cmp::max(peer.seen_at, old_peer.seen_at);
emit_event!(
event_emitter,
NodeEventType::see_peer_already_active(&old_peer, new_seen_at),
tags = peers,
see_peer
);
old_peer.seen_at = new_seen_at;
}
}
}
}
}
fn timeout(
&mut self,
#[cfg(feature = "events")] event_emitter: Option<
mpsc::Sender<NodeEventEmittedInfo>,
>,
) {
let mut forget = Vec::new();
for (_, peer) in &self.active {
if peer.seen_at < get_time() - PEER_TIMEOUT {
emit_event!(
event_emitter,
NodeEventType::timeout(&peer),
tags = peers,
timeout
);
forget.push(peer.address);
}
}
for addr in forget {
self.inactivate_peer(&addr);
}
}
pub fn inactivate_peer(&mut self, addr: &A) {
self.active.remove(addr);
}
pub fn get_all_active(&self) -> Vec<Peer<A>> {
self.active.values().cloned().collect()
}
pub fn get_all(&self) -> Vec<Peer<A>> {
self.seen.values().cloned().collect()
}
pub fn get_random_active(&self, amount: u128) -> Vec<Peer<A>> {
let amount = amount as usize;
let mut rng = rand::thread_rng();
let peers = self.active.values().cloned().choose_multiple(&mut rng, amount);
peers
}
}
// Communication with miner thread
// -------------------------------
#[derive(Debug, Clone)]
pub enum MinerMessage {
Request { prev: U256, body: Body, targ: U256 },
Answer { block: HashedBlock },
Stop,
}
#[derive(Debug, Clone)]
pub struct MinerCommunication {
message: Arc<Mutex<MinerMessage>>,
}
// Protocol
// --------
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum Message<A: ProtoAddr> {
NoticeTheseBlocks {
magic: u32,
gossip: bool,
blocks: Vec<Block>,
peers: Vec<Peer<A>>,
},
GiveMeThatBlock {
magic: u32,
bhash: Hash,
},
PleaseMineThisTransaction {
magic: u32,
tx: Transaction,
},
}
// Constants
// =========
// Size of a hash, in bytes
pub const _HASH_SIZE: usize = 32;
// Size of a block's body, in bytes
pub const MAX_BODY_SIZE: usize = 1280;
/// Size, in bytes, of needed space for transaction length store
pub const TRANSACTION_LENGTH_ENCODE_SIZE: usize = 2;
/// Size of the largest possible transaction, in bytes
pub const MAX_TRANSACTION_SIZE: usize =
MAX_BODY_SIZE - TRANSACTION_LENGTH_ENCODE_SIZE - 1;
// Max size of a big UDP packet, in bytes
pub const MAX_UDP_SIZE_SLOW: usize = 8000;
// Max size of a fast UDP packet, in bytes
pub const _MAX_UDP_SIZE_FAST: usize = 1500;
// TODO: enforce maximum block size on debug mode
// Size of a block, in bytes
//pub const BLOCK_SIZE : usize = HASH_SIZE + (U128_SIZE * 4) + BODY_SIZE;
// Size of an IPv4 address, in bytes
pub const _IPV4_SIZE: usize = 4;
// Size of an IPv6 address, in bytes
pub const _IPV6_SIZE: usize = 16;
// Size of an IP port, in bytes
pub const _PORT_SIZE: usize = 2;
// How many nodes we gossip an information to?
pub const _GOSSIP_FACTOR: u128 = 16;
// How many times the mining thread attempts before unblocking?
pub const MINE_ATTEMPTS: u128 = 1024;
// Desired average time between mined blocks, in milliseconds
pub const TIME_PER_BLOCK: u128 = 1000;
// Don't accept blocks from N milliseconds in the future
pub const DELAY_TOLERANCE: u128 = 60 * 60 * 1000;
// Readjust difficulty every N blocks
pub const BLOCKS_PER_PERIOD: u128 = 20;
// Readjusts difficulty every N seconds
pub const TIME_PER_PERIOD: u128 = TIME_PER_BLOCK * BLOCKS_PER_PERIOD;
// Initial difficulty, in expected hashes per block
pub const INITIAL_DIFFICULTY: u128 = 256;
// How many milliseconds without notice until we forget a peer?
pub const PEER_TIMEOUT: u128 = 10 * 1000;
// How many peers we need to keep minimum?
pub const _PEER_COUNT_MINIMUM: u128 = 256;
// How many peers we send when asked?
pub const _SHARE_PEER_COUNT: u128 = 3;
// How many peers we keep on the last_seen object?
pub const _LAST_SEEN_SIZE: u128 = 2;
// Delay between handling of network messages, in ms
pub const HANDLE_MESSAGE_DELAY: u128 = 20;
// Delay between handling of API requests, in ms
pub const HANDLE_REQUEST_DELAY: u128 = 20;
// This limits how many messages we accept at once
pub const _HANDLE_MESSAGE_LIMIT: u128 = 5;
// FIXME:
// With a handle_message_delay of 20ms, and the message limit of 5, we can handle up to 250
// messages per second. This number is made up. I do not know how many messages we're able to
// handle. We must stress test and benchmark the performance of Node::handle_message, in order to
// come up with a constant that is aligned. Furthermore, we can also greatly optimize the
// performance of Node::handle_message with some key changes, which would allow us to increase that
// limit considerably.
// 1. Use a faster hash function:
// We can replace every usage of Keccak by K12 on Kindelia, with the only exception being the
// hash of a public address to end up with an account's name, since Keccak is required to achieve
// Ethereum account compatibility.
// 2. Receive the hash from the peer:
// We can *perhaps*, receive a block's hash from the peer that sends it. Obviously, this would
// open door for several vulnerabilities, but it might be used as a heuristic to avoid slow
// branches. Of course, when the hash is needed for critical purposes, we must compute it.
// Algorithms
// ==========
// Target is a U256 number. A hash larger than or equal to that number hits the target.
// Difficulty is an estimation of how many hashes it takes to hit a given target.
/// Converts a target to a difficulty.
pub fn target_to_difficulty(target: U256) -> U256 {
let p256 =
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
let p256 = U256::from(p256);
return p256 / (p256 - target);
}
/// Converts a difficulty to a target.
pub fn difficulty_to_target(difficulty: U256) -> U256 {
let p256 =
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
let p256 = U256::from(p256);
return p256 - p256 / difficulty;
}
// Computes next target by scaling the current difficulty by a `scale` factor.
// Since the factor is an integer, it is divided by 2^32 to allow integer division.
// - compute_next_target(t, 2n**32n / 2n): difficulty halves
// - compute_next_target(t, 2n**32n * 1n): nothing changes
// - compute_next_target(t, 2n**32n * 2n): difficulty doubles
pub fn compute_next_target(last_target: U256, scale: U256) -> U256 {
let p32 = U256::from("0x100000000");
let last_difficulty = target_to_difficulty(last_target);
let next_difficulty = u256(1) + (last_difficulty * scale - u256(1)) / p32;
return difficulty_to_target(next_difficulty);
}
// Estimates how many hashes were necessary to get this one.
pub fn get_hash_work(hash: U256) -> U256 {
if hash == u256(0) {
return u256(0);
} else {
return target_to_difficulty(hash);
}
}
// Hashes a U256 value.
pub fn hash_u256(value: U256) -> U256 {
return hash_bytes(u256_to_bytes(value).as_slice());
}
// Hashes a byte array.
pub fn hash_bytes(bytes: &[u8]) -> U256 {
let mut hasher = sha3::Keccak256::new();
hasher.update(&bytes);
let hash = hasher.finalize();
return U256::from_little_endian(&hash);
}
/// Puts transaction inside `body_vec` if space is suficient
pub fn add_transaction_to_body_vec(
body_vec: &mut Vec<u8>,
transaction: &Transaction,
) -> Result<(), String> {
let tx_len = transaction.data.len() + TRANSACTION_LENGTH_ENCODE_SIZE;
let len_info = transaction.encode_length();
if body_vec.len() + tx_len > MAX_BODY_SIZE {
return Err("No enough space in block".to_string());
}
body_vec.push(len_info.0);
body_vec.push(len_info.1);
body_vec.extend_from_slice(&transaction.data);
Ok(())
}
/// Converts a block body to a vector of transactions.
pub fn extract_transactions(body: &Body) -> Vec<Transaction> {
let mut transactions = Vec::new();
let mut index = 1;
let tx_count = body.data[0].reverse_bits();
for _ in 0..tx_count {
if index >= body.data.len() {
break;
}
let tx_len =
Transaction::decode_length((body.data[index], body.data[index + 1]));
index += 2;
if index + tx_len > body.data.len() {
break;
}
let transaction_body = body.data[index..index + tx_len].to_vec();
match Transaction::new(transaction_body) {
Ok(transaction) => transactions.push(transaction),
Err(err) => eprintln!(
"A transaction bigger than block was created from a block{}",
err
), // in theory this can never happen
};
index += tx_len;
}
transactions
}
/// Initial target of 256 hashes per block.
pub fn initial_target() -> U256 {
difficulty_to_target(u256(INITIAL_DIFFICULTY))
}
/// The hash of the genesis block's parent.
pub fn zero_hash() -> U256 {
u256(0)
}
/// Builds the Genesis Block.
pub fn build_genesis_block(
stmts: &[Statement],
) -> Result<Block, BlockBodyError> {
let body = Body::from_transactions_iter(stmts)?;
Ok(Block::new(zero_hash(), 0, 0, body))
}
// Mining
// ------
// Given a target, attempts to mine a block by changing its nonce up to `max_attempts` times
pub fn try_mine(
prev: U256,
body: Body,
targ: U256,
max_attempts: u128,
) -> Option<HashedBlock> {
let rand = rand::random::<u128>();
let time = get_time();
let mut block = Block::new(prev, time, rand, body);
for _i in 0..max_attempts {
block = {
let hashed = block.hashed();
let hash_n = U256::from(hashed.get_hash());
if hash_n >= targ {
return Some(hashed);
}
let mut block = hashed.take();
block.meta = block.meta.wrapping_add(1);
block
}
}
None
}
impl MinerCommunication {
// Creates a shared MinerCommunication object
pub fn new() -> Self {
MinerCommunication { message: Arc::new(Mutex::new(MinerMessage::Stop)) }
}
// Writes the shared MinerCommunication object
pub fn write(&mut self, new_message: MinerMessage) {
// todo: investigate how best to handle lock poisoning error.
// see https://doc.rust-lang.org/std/sync/struct.Mutex.html#poisoning
// and https://blog.rust-lang.org/2020/12/11/lock-poisoning-survey.html
let mut value = self
.message
.lock()
.expect("Miner message mutex lock should be acquired for write acceess");
*value = new_message;
}
pub fn read(&self) -> MinerMessage {
// todo: investigate how best to handle lock poisoning error.
// see https://doc.rust-lang.org/std/sync/struct.Mutex.html#poisoning
// and https://blog.rust-lang.org/2020/12/11/lock-poisoning-survey.html
// note: parking_lot crate provides non poisoning mutexes.
return (*self
.message
.lock()
.expect("Miner message mutex lock should be acquired for read acceess"))
.clone();
}
}
// Main miner loop: if asked, attempts to mine a block
pub fn miner_loop(
mut miner_comm: MinerCommunication,
slow_mining: Option<u64>,
#[cfg(feature = "events")] event_emitter: Option<
mpsc::Sender<NodeEventEmittedInfo>,
>,
) {
loop {
if let MinerMessage::Request { prev, body, targ } = miner_comm.read() {
let before = std::time::Instant::now();
let mined = try_mine(prev, body, targ, MINE_ATTEMPTS);
// Slow down mining, for debugging pourposes, if enabled
if let Some(slow_ratio) = slow_mining {
let elapsed = before.elapsed();
let sleep_time = elapsed.saturating_mul(slow_ratio as u32);
std::thread::sleep(sleep_time);
}
if let Some(block) = mined {
emit_event!(
event_emitter,
NodeEventType::mined(block.get_hash().into(), targ),
tags = mining,
mined
);
miner_comm.write(MinerMessage::Answer { block });
} else {
emit_event!(
event_emitter,
NodeEventType::failed_mined(targ),
tags = mining,
failed_mined
);
}
}
}
}
// Node
// ----
/// Errors associated with Node.
#[derive(Error, Debug)]
pub enum NodeError {
#[error(transparent)]
BlockStorage(#[from] BlockStorageError),
}
/// Errors associated with Blocks
#[derive(Error, Debug)]
pub enum BlockLookupError {
#[error("Invalid Block: {0}")]
InvalidBlock(U256),
#[error("Invalid Height: {0}")]
InvalidHeight(U256),
#[error("Invalid Target: {0}")]
InvalidTarget(U256),
#[error("Invalid Work: {0}")]
InvalidWork(U256),
}
impl<C: ProtoComm, S: BlockStorage> Node<C, S> {
#[allow(clippy::too_many_arguments)]
pub fn new(
data_path: PathBuf,
network_id: u32,
addr: C::Address, // todo: review? https://github.com/Kindelia/Kindelia-Chain/pull/252#discussion_r1037732536
initial_peers: Vec<C::Address>,
comm: C,
miner_comm: Option<MinerCommunication>,
storage: S,
#[cfg(feature = "events")] event_emitter: Option<
mpsc::Sender<NodeEventEmittedInfo>,
>,
) -> (mpsc::SyncSender<NodeRequest<C>>, Self) {
let (query_sender, query_receiver) = mpsc::sync_channel(1);
let genesis_stmts =
parser::parse_code(constants::GENESIS_CODE).expect("Genesis code parses");
let genesis_block =
build_genesis_block(&genesis_stmts).expect("Genesis block builds");
let genesis_block = genesis_block.hashed();
let genesis_hash = genesis_block.get_hash().into();
let runtime = init_runtime(data_path.join("heaps"), &genesis_stmts);
#[rustfmt::skip]
let mut node = Node {
network_id,
addr,
comm,
runtime,
pool : PriorityQueue:: new(),
peers : PeersStore:: new(),
genesis_hash,
tip : genesis_hash,
block : u256map_from([(genesis_hash, genesis_block)]),
pending : u256map_new(),
ancestor : u256map_new(),
wait_list: u256map_new(),
children : u256map_from([(genesis_hash, vec![] )]),
work : u256map_from([(genesis_hash, u256(0) )]),
height : u256map_from([(genesis_hash, 0 )]),
target : u256map_from([(genesis_hash, initial_target())]),
results : u256map_from([(genesis_hash, vec![] )]),
#[cfg(feature = "events")]
event_emitter: event_emitter.clone(),
storage,
query_recv : query_receiver,
miner_comm,
};
let now = get_time();
initial_peers.iter().for_each(|address| {
return node.peers.see_peer(
Peer { address: *address, seen_at: now },
#[cfg(feature = "events")] // TODO: remove (implement on Node)
event_emitter.clone(),
);
});
// TODO: For testing purposes. Remove later.
// for &peer_port in try_ports.iter() {
// if peer_port != port {
// let address = Address::IPv4 { val0: 127, val1: 0, val2: 0, val3: 1, port: peer_port };
// node.peers.see_peer(Peer { address: address, seen_at: now })
// }
// }
(query_sender, node)
}
pub fn add_transaction(
&mut self,
transaction: Transaction,
) -> Result<(), PoolError> {
let hash = transaction.hash;
let t_score = hash.low_u64();
if self.pool.get(&transaction).is_none() {
self.pool.push(transaction, t_score);
Ok(())
} else {
Err(PoolError::AlreadyIncluded { hash: hash.into() })
}
}
// Registers a block on the node's database. This performs several actions:
// - If this block is too far into the future, ignore it.
// - If this block's parent isn't available:
// - Add this block to the parent's wait_list
// - When the parent is available, register this block again
// - If this block's parent is available:
// - Compute the block accumulated work, target, etc.
// - If this block is the new tip:
// - In case of a reorg, rollback to the block before it
// - Run that block's code, updating the HVM state
// - Updates the longest chain saved on disk
pub fn add_block(&mut self, block: &HashedBlock) {
// Adding a block might trigger the addition of other blocks
// that were waiting for it. Because of that, we loop here.
// Blocks to be added
let mut must_include = vec![block.clone()];
// While there is a block to add...
while let Some(block) = must_include.pop() {
let btime = block.time;
// If block is too far into the future, ignore it
if btime >= get_time() + DELAY_TOLERANCE {
emit_event!(
self.event_emitter,
NodeEventType::too_late(&block),
tags = add_block,
too_late
);
continue;
}
let bhash = block.get_hash().into();
// If we already registered this block, ignore it
if let Some(block) = self.block.get(&bhash) {
let height = self.height[&bhash];
emit_event!(
self.event_emitter,
NodeEventType::already_included(block, height),
tags = add_block,
already_included
);
continue;
}
let phash = block.prev;
// If previous block is available, add the block to the chain
if self.block.get(&phash).is_some() {
let work = get_hash_work(bhash); // block work score
self.block.insert(bhash, block.clone()); // inserts the block
self.work.insert(bhash, u256(0)); // inits the work attr
self.height.insert(bhash, 0); // inits the height attr
self.target.insert(bhash, u256(0)); // inits the target attr
self.children.insert(bhash, vec![]); // inits the children attrs
self.ancestor.remove(&bhash); // remove it from the ancestor jump table
// Checks if this block PoW hits the target
let has_enough_work = bhash >= self.target[&phash];
// Checks if this block's timestamp is larger than its parent's timestamp
// Note: Bitcoin checks if it is larger than the median of the last 11 blocks; should we?
let advances_time = btime > self.block[&phash].time;
// If the PoW hits the target and the block's timestamp is valid...
if has_enough_work && advances_time {
self.work.insert(bhash, self.work[&phash] + work); // sets this block accumulated work
self.height.insert(bhash, self.height[&phash] + 1); // sets this block accumulated height
// If this block starts a new period, computes the new target
if self.height[&bhash] > 0
&& self.height[&bhash] > BLOCKS_PER_PERIOD
&& self.height[&bhash] % BLOCKS_PER_PERIOD == 1
{
// Finds the checkpoint hash (hash of the first block of the last period)
let mut checkpoint_hash = phash;
for _ in 0..BLOCKS_PER_PERIOD - 1 {
checkpoint_hash = self.block[&checkpoint_hash].prev;
}
// Computes how much time the last period took to complete
let period_time = btime - self.block[&checkpoint_hash].time;
// Computes the target of this period
let last_target = self.target[&phash];
let next_scaler = 2u128.pow(32) * TIME_PER_PERIOD / period_time;
let next_target =
compute_next_target(last_target, u256(next_scaler));
// Sets the new target
self.target.insert(bhash, next_target);
// Otherwise, keep the old target
} else {
self.target.insert(bhash, self.target[&phash]);
}
// Updates the tip work and block hash
let cur_tip = self.tip;
let new_tip = bhash;
// Reorgs happens
if self.work[&new_tip] > self.work[&cur_tip] {
// When the tip updates, stop mining the last built block, which is
// based on the outdated tip
self.send_to_miner(MinerMessage::Stop);
emit_event!(