-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathversioning.rs
864 lines (762 loc) · 27.8 KB
/
versioning.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
use std::{
cmp::Ordering,
collections::{BTreeMap, HashMap},
};
use failure::{Error, Fail};
use protobuf::Message as _;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumIter, EnumString};
use crate::{
chain::{ConsensusConstants, Epoch, Hash},
get_protocol_version,
proto::{
schema::witnet::{
Block, Block_BlockHeader, Block_BlockHeader_BlockMerkleRoots, Block_BlockTransactions,
LegacyBlock, LegacyBlock_LegacyBlockHeader,
LegacyBlock_LegacyBlockHeader_LegacyBlockMerkleRoots,
LegacyBlock_LegacyBlockTransactions, LegacyMessage, LegacyMessage_LegacyCommand,
LegacyMessage_LegacyCommand_oneof_kind, Message_Command, Message_Command_oneof_kind,
},
ProtobufConvert,
},
types::{Message, ProtocolVersionName},
};
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct ProtocolInfo {
pub current_version: ProtocolVersion,
pub all_versions: VersionsMap,
pub all_checkpoints_periods: HashMap<ProtocolVersion, u16>,
}
impl ProtocolInfo {
pub fn register(&mut self, epoch: Epoch, version: ProtocolVersion, checkpoint_period: u16) {
self.all_versions.register(epoch, version);
self.all_checkpoints_periods
.insert(version, checkpoint_period);
}
pub fn clear_versions(&mut self) {
self.current_version = ProtocolVersion::default();
self.all_versions.clear();
self.all_checkpoints_periods.clear();
}
/// Convenience method for deriving the activation timestamp for any given protocol version.
pub fn derive_activation_timestamp(
&self,
version: ProtocolVersion,
consensus_constants: &ConsensusConstants,
) -> Option<i64> {
if version == ProtocolVersion::MIN {
Some(consensus_constants.checkpoint_zero_timestamp)
} else {
let previous_version = version.prev();
let previous_version_activation_timestamp =
self.derive_activation_timestamp(previous_version, consensus_constants);
let previous_version_checkpoint_period =
u32::from(if previous_version == ProtocolVersion::MIN {
consensus_constants.checkpoints_period
} else {
*self.all_checkpoints_periods.get(&previous_version)?
});
let previous_version_span_epochs = self
.all_versions
.try_get_activation_epoch(version)?
.saturating_sub(
self.all_versions
.try_get_activation_epoch(previous_version)
.unwrap_or(0),
);
let previous_version_span_seconds =
i64::from(previous_version_span_epochs * previous_version_checkpoint_period);
Some(previous_version_activation_timestamp? + previous_version_span_seconds)
}
}
}
#[test]
fn test_activation_timestamp_derivation() {
let mut protocols = ProtocolInfo::default();
protocols.register(100, ProtocolVersion::V1_8, 30);
protocols.register(200, ProtocolVersion::V2_0, 15);
let consensus_constants = ConsensusConstants {
checkpoint_zero_timestamp: 10_000,
checkpoints_period: 45,
..Default::default()
};
assert_eq!(
protocols.derive_activation_timestamp(ProtocolVersion::V1_7, &consensus_constants),
Some(10_000)
);
assert_eq!(
protocols.derive_activation_timestamp(ProtocolVersion::V1_8, &consensus_constants),
Some(14_500)
);
assert_eq!(
protocols.derive_activation_timestamp(ProtocolVersion::V2_0, &consensus_constants),
Some(17_500)
);
// Try again but pretending that a certain protocol version still has an unknown activation date
let mut protocols = ProtocolInfo::default();
protocols.register(100, ProtocolVersion::V1_8, 30);
assert_eq!(
protocols.derive_activation_timestamp(ProtocolVersion::V2_0, &consensus_constants),
None
);
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct VersionsMap {
efv: HashMap<ProtocolVersion, Epoch>,
vfe: BTreeMap<Epoch, ProtocolVersion>,
}
impl VersionsMap {
pub fn register(&mut self, epoch: Epoch, version: ProtocolVersion) {
self.efv.insert(version, epoch);
self.vfe.insert(epoch, version);
}
pub fn version_for_epoch(&self, queried_epoch: Epoch) -> ProtocolVersion {
self.vfe
.iter()
.rev()
.find(|(epoch, _)| **epoch <= queried_epoch)
.map(|(_, version)| version)
.copied()
.unwrap_or_default()
}
#[inline]
pub fn try_get_activation_epoch(&self, version: ProtocolVersion) -> Option<Epoch> {
self.efv.get(&version).cloned()
}
pub fn get_activation_epoch(&self, version: ProtocolVersion) -> Epoch {
match self.try_get_activation_epoch(version) {
Some(epoch) => epoch,
None => Epoch::MAX,
}
}
pub fn clear(&mut self) {
self.efv.clear();
self.vfe.clear();
}
}
impl IntoIterator for VersionsMap {
type Item = (ProtocolVersion, Epoch);
type IntoIter = std::collections::hash_map::IntoIter<ProtocolVersion, Epoch>;
fn into_iter(self) -> Self::IntoIter {
self.efv.into_iter()
}
}
/// An enumeration of different protocol versions.
///
/// IMPORTANT NOTE: when adding new versions here in the future, make sure to also add them in
/// `impl PartialOrd for ProtocolVersion`.
#[derive(
Clone,
Copy,
Debug,
Default,
Deserialize,
Display,
EnumString,
Eq,
Hash,
PartialEq,
Serialize,
EnumIter,
)]
pub enum ProtocolVersion {
/// The original Witnet protocol.
// TODO: update this default once 2.0 is completely active
#[default]
V1_7,
/// The transitional protocol based on 1.x but with staking enabled.
V1_8,
/// The final Witnet 2.0 protocol.
V2_0,
}
impl ProtocolVersion {
pub const MIN: Self = ProtocolVersion::V1_7;
pub const MAX: Self = ProtocolVersion::V2_0;
pub fn guess() -> Self {
Self::from_epoch_opt(None)
}
#[inline]
pub fn from_epoch(epoch: Epoch) -> Self {
Self::from_epoch_opt(Some(epoch))
}
#[inline]
pub fn from_epoch_opt(epoch: Option<Epoch>) -> Self {
get_protocol_version(epoch)
}
pub fn next(&self) -> Self {
match self {
ProtocolVersion::V1_7 => ProtocolVersion::V1_8,
_ => ProtocolVersion::V2_0,
}
}
pub fn prev(&self) -> Self {
match self {
ProtocolVersion::V2_0 => ProtocolVersion::V1_8,
_ => ProtocolVersion::V1_7,
}
}
}
impl PartialOrd for ProtocolVersion {
/// Implement comparisons for protocol versions so that expressions like `< V2_0` can be used.
///
/// IMPORTANT NOTE: all future versions need to be added here.
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
use ProtocolVersion::*;
Some(match (self, other) {
// Control equality first
(x, y) if x == y => Ordering::Equal,
// V1_7 is the lowest version
(V1_7, _) => Ordering::Less,
// V2_0 is the highest version
(V2_0, _) => Ordering::Greater,
// Versions that are not the lowest or the highest need explicit comparisons
(V1_8, V1_7) => Ordering::Greater,
(V1_8, V2_0) => Ordering::Less,
// the compiler doesn't know, but this is actually unreachable if you think about it
_ => {
unreachable!()
}
})
}
}
impl From<ProtocolVersionName> for ProtocolVersion {
fn from(version: ProtocolVersionName) -> Self {
match version {
ProtocolVersionName::V1_7(_) => ProtocolVersion::V1_7,
ProtocolVersionName::V1_8(_) => ProtocolVersion::V1_8,
ProtocolVersionName::V2_0(_) => ProtocolVersion::V2_0,
}
}
}
pub trait Versioned: ProtobufConvert {
type LegacyType: protobuf::Message;
/// Turn a protobuf-compatible data structure into a versioned form of itself.
///
/// For truly versionable data structures, this method should be implemented manually. For other
/// data structures, the trait's own blanket implementation should be fine.
fn to_versioned_pb(
&self,
_version: ProtocolVersion,
) -> Result<Box<dyn protobuf::Message>, Error>
where
<Self as ProtobufConvert>::ProtoStruct: protobuf::Message,
{
Ok(Box::new(self.to_pb()))
}
/// Turn a protobuf-compaitble data structures into its serialized protobuf bytes.
/// This blanket implementation should normally not be overriden.
fn to_versioned_pb_bytes(&self, version: ProtocolVersion) -> Result<Vec<u8>, Error>
where
<Self as ProtobufConvert>::ProtoStruct: protobuf::Message,
{
Ok(self.to_versioned_pb(version)?.write_to_bytes()?)
}
/// Constructs an instance of this data structure based on a protobuf instance of its legacy
/// schema.
fn from_versioned_pb(legacy: Self::LegacyType) -> Result<Self, Error>
where
Self: From<Self::LegacyType>,
{
Ok(Self::from(legacy))
}
/// Tries to deserialize a data structure from its regular protobuf schema, and if it fails, it
/// retries with its legacy schema.
fn from_versioned_pb_bytes(bytes: &[u8]) -> Result<Self, Error>
where
<Self as ProtobufConvert>::ProtoStruct: protobuf::Message,
Self: From<Self::LegacyType>,
{
let mut current = Self::ProtoStruct::new();
let direct_attempt = current
.merge_from_bytes(bytes)
.map_err(|e| Error::from_boxed_compat(Box::new(e.compat())))
.and_then(|_| Self::from_pb(current));
if direct_attempt.is_ok() {
direct_attempt
} else {
let mut legacy = Self::LegacyType::new();
legacy.merge_from_bytes(bytes)?;
Ok(Self::from(legacy))
}
}
}
impl Versioned for crate::chain::BlockMerkleRoots {
type LegacyType = LegacyBlock_LegacyBlockHeader_LegacyBlockMerkleRoots;
fn to_versioned_pb(
&self,
version: ProtocolVersion,
) -> Result<Box<dyn protobuf::Message>, Error> {
use ProtocolVersion::*;
let mut pb = self.to_pb();
let versioned: Box<dyn protobuf::Message> = match version {
// Legacy merkle roots need to get rearranged
V1_7 => Box::new(Self::LegacyType::from(pb)),
// Transition merkle roots need no transformation
V1_8 => Box::new(pb),
// Final merkle roots need to drop the mint hash
V2_0 => {
pb.set_mint_hash(Default::default());
Box::new(pb)
}
};
Ok(versioned)
}
}
impl Versioned for crate::chain::BlockHeader {
type LegacyType = LegacyBlock_LegacyBlockHeader;
fn to_versioned_pb(
&self,
version: ProtocolVersion,
) -> Result<Box<dyn protobuf::Message>, Error> {
use ProtocolVersion::*;
let pb = self.to_pb();
let versioned: Box<dyn protobuf::Message> = match version {
// Legacy block headers need to be rearranged
V1_7 => Box::new(Self::LegacyType::from(pb)),
// All other block headers need no transformation
V1_8 | V2_0 => Box::new(pb),
};
Ok(versioned)
}
}
impl Versioned for crate::chain::SuperBlock {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::chain::Block {
type LegacyType = LegacyBlock;
fn to_versioned_pb(
&self,
_version: ProtocolVersion,
) -> Result<Box<dyn protobuf::Message>, Error>
where
<Self as ProtobufConvert>::ProtoStruct: protobuf::Message,
{
Ok(Box::new(Self::LegacyType::from(self.to_pb())))
}
}
impl Versioned for Message {
type LegacyType = LegacyMessage;
fn to_versioned_pb(&self, version: ProtocolVersion) -> Result<Box<dyn protobuf::Message>, Error>
where
<Self as ProtobufConvert>::ProtoStruct: protobuf::Message,
{
use ProtocolVersion::*;
let pb = self.to_pb();
let versioned: Box<dyn protobuf::Message> = match version {
V1_7 => Box::new(Self::LegacyType::from(pb)),
V1_8 | V2_0 => Box::new(pb),
};
Ok(versioned)
}
}
impl Versioned for crate::transaction::Transaction {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::VTTransaction {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::VTTransactionBody {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::DRTransactionBody {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::CommitTransactionBody {
// FIXME: implement proper versioning here for commit transactions
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::RevealTransaction {
// FIXME: implement proper versioning here for reveal transactions
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::RevealTransactionBody {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::TallyTransaction {
// FIXME: implement proper versioning here for tally transactions
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::MintTransaction {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::StakeTransactionBody {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::transaction::UnstakeTransactionBody {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
impl Versioned for crate::chain::DataRequestOutput {
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
}
pub trait AutoVersioned: ProtobufConvert {}
impl AutoVersioned for crate::chain::BlockHeader {}
impl AutoVersioned for crate::chain::SuperBlock {}
impl AutoVersioned for crate::transaction::VTTransactionBody {}
impl AutoVersioned for crate::transaction::CommitTransactionBody {}
impl AutoVersioned for crate::transaction::RevealTransactionBody {}
impl AutoVersioned for crate::transaction::MintTransaction {}
impl AutoVersioned for crate::transaction::StakeTransactionBody {}
impl AutoVersioned for crate::transaction::UnstakeTransactionBody {}
impl AutoVersioned for crate::chain::DataRequestOutput {}
pub trait VersionedHashable {
fn versioned_hash(&self, version: ProtocolVersion) -> Hash;
}
impl<T> VersionedHashable for T
where
T: AutoVersioned + Versioned,
<Self as ProtobufConvert>::ProtoStruct: protobuf::Message,
{
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
// This unwrap is kept in here just because we want `VersionedHashable` to have the same interface as
// `Hashable`.
witnet_crypto::hash::calculate_sha256(&self.to_versioned_pb_bytes(version).unwrap()).into()
}
}
impl VersionedHashable for crate::transaction::Transaction {
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
use crate::transaction::Transaction::*;
match self {
ValueTransfer(tx) => tx.versioned_hash(version),
DataRequest(tx) => tx.versioned_hash(version),
Commit(tx) => tx.versioned_hash(version),
Reveal(tx) => tx.versioned_hash(version),
Tally(tx) => tx.versioned_hash(version),
Mint(tx) => tx.versioned_hash(version),
Stake(tx) => tx.versioned_hash(version),
Unstake(tx) => tx.versioned_hash(version),
}
}
}
impl VersionedHashable for crate::transaction::VTTransaction {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
self.body.versioned_hash(version)
}
}
impl VersionedHashable for crate::transaction::DRTransactionBody {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
let Hash::SHA256(data_bytes) = self.data_poi_hash(version);
let Hash::SHA256(rest_bytes) = self.rest_poi_hash(version);
witnet_crypto::hash::calculate_sha256(&[data_bytes, rest_bytes].concat()).into()
}
}
impl VersionedHashable for crate::transaction::DRTransaction {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
self.body.versioned_hash(version)
}
}
impl VersionedHashable for crate::transaction::CommitTransaction {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
self.body.versioned_hash(version)
}
}
impl VersionedHashable for crate::transaction::RevealTransaction {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
self.body.versioned_hash(version)
}
}
impl VersionedHashable for crate::transaction::TallyTransaction {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
let Hash::SHA256(data_bytes) = self.data_poi_hash();
let Hash::SHA256(rest_bytes) = self.rest_poi_hash(version);
witnet_crypto::hash::calculate_sha256(&[data_bytes, rest_bytes].concat()).into()
}
}
impl VersionedHashable for crate::transaction::StakeTransaction {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
self.body.versioned_hash(version)
}
}
impl VersionedHashable for crate::transaction::UnstakeTransaction {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
self.body.versioned_hash(version)
}
}
impl VersionedHashable for crate::chain::Block {
#[inline]
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
self.block_header.versioned_hash(version)
}
}
impl From<Block_BlockHeader_BlockMerkleRoots>
for LegacyBlock_LegacyBlockHeader_LegacyBlockMerkleRoots
{
fn from(header: Block_BlockHeader_BlockMerkleRoots) -> Self {
let mut legacy = LegacyBlock_LegacyBlockHeader_LegacyBlockMerkleRoots::new();
legacy.set_mint_hash(header.get_mint_hash().clone());
legacy.vt_hash_merkle_root = header.vt_hash_merkle_root;
legacy.dr_hash_merkle_root = header.dr_hash_merkle_root;
legacy.commit_hash_merkle_root = header.commit_hash_merkle_root;
legacy.reveal_hash_merkle_root = header.reveal_hash_merkle_root;
legacy.tally_hash_merkle_root = header.tally_hash_merkle_root;
legacy
}
}
impl From<LegacyBlock_LegacyBlockHeader_LegacyBlockMerkleRoots>
for Block_BlockHeader_BlockMerkleRoots
{
fn from(
LegacyBlock_LegacyBlockHeader_LegacyBlockMerkleRoots {
mint_hash,
vt_hash_merkle_root,
dr_hash_merkle_root,
commit_hash_merkle_root,
reveal_hash_merkle_root,
tally_hash_merkle_root,
..
}: LegacyBlock_LegacyBlockHeader_LegacyBlockMerkleRoots,
) -> Self {
let mut header = Block_BlockHeader_BlockMerkleRoots::new();
header.mint_hash = mint_hash;
header.vt_hash_merkle_root = vt_hash_merkle_root;
header.dr_hash_merkle_root = dr_hash_merkle_root;
header.commit_hash_merkle_root = commit_hash_merkle_root;
header.reveal_hash_merkle_root = reveal_hash_merkle_root;
header.tally_hash_merkle_root = tally_hash_merkle_root;
header.set_stake_hash_merkle_root(Hash::default().to_pb());
header.set_unstake_hash_merkle_root(Hash::default().to_pb());
header
}
}
impl From<Block_BlockHeader> for LegacyBlock_LegacyBlockHeader {
fn from(
Block_BlockHeader {
signals,
beacon,
merkle_roots,
proof,
bn256_public_key,
..
}: Block_BlockHeader,
) -> Self {
let mut legacy = LegacyBlock_LegacyBlockHeader::new();
legacy.signals = signals;
legacy.beacon = beacon;
legacy.merkle_roots = merkle_roots.map(Into::into);
legacy.proof = proof;
legacy.bn256_public_key = bn256_public_key;
legacy
}
}
impl From<LegacyBlock_LegacyBlockHeader> for Block_BlockHeader {
fn from(
LegacyBlock_LegacyBlockHeader {
signals,
beacon,
merkle_roots,
proof,
bn256_public_key,
..
}: LegacyBlock_LegacyBlockHeader,
) -> Self {
let mut header = Block_BlockHeader::new();
header.signals = signals;
header.beacon = beacon;
header.merkle_roots = merkle_roots.map(Into::into);
header.proof = proof;
header.bn256_public_key = bn256_public_key;
header
}
}
impl From<Block_BlockTransactions> for LegacyBlock_LegacyBlockTransactions {
fn from(
Block_BlockTransactions {
mint,
value_transfer_txns,
data_request_txns,
commit_txns,
reveal_txns,
tally_txns,
..
}: Block_BlockTransactions,
) -> Self {
let mut legacy = LegacyBlock_LegacyBlockTransactions::new();
legacy.mint = mint;
legacy.value_transfer_txns = value_transfer_txns;
legacy.data_request_txns = data_request_txns;
legacy.commit_txns = commit_txns;
legacy.reveal_txns = reveal_txns;
legacy.tally_txns = tally_txns;
legacy
}
}
impl From<LegacyBlock_LegacyBlockTransactions> for Block_BlockTransactions {
fn from(
LegacyBlock_LegacyBlockTransactions {
mint,
value_transfer_txns,
data_request_txns,
commit_txns,
reveal_txns,
tally_txns,
..
}: LegacyBlock_LegacyBlockTransactions,
) -> Self {
let mut txns = Block_BlockTransactions::new();
txns.mint = mint;
txns.value_transfer_txns = value_transfer_txns;
txns.data_request_txns = data_request_txns;
txns.commit_txns = commit_txns;
txns.reveal_txns = reveal_txns;
txns.tally_txns = tally_txns;
txns.stake_txns = vec![].into();
txns.unstake_txns = vec![].into();
txns
}
}
impl From<Block> for LegacyBlock {
fn from(
Block {
block_header,
block_sig,
txns,
..
}: Block,
) -> Self {
let mut legacy = LegacyBlock::new();
legacy.block_header = block_header.map(Into::into);
legacy.block_sig = block_sig;
legacy.txns = txns.map(Into::into);
legacy
}
}
impl From<LegacyBlock> for Block {
fn from(
LegacyBlock {
block_header,
block_sig,
txns,
..
}: LegacyBlock,
) -> Self {
let mut block = Block::new();
block.block_header = block_header.map(Into::into);
block.block_sig = block_sig;
block.txns = txns.map(Into::into);
block
}
}
impl From<Message_Command_oneof_kind> for LegacyMessage_LegacyCommand_oneof_kind {
fn from(value: Message_Command_oneof_kind) -> Self {
match value {
Message_Command_oneof_kind::Version(x) => {
LegacyMessage_LegacyCommand_oneof_kind::Version(x)
}
Message_Command_oneof_kind::Verack(x) => {
LegacyMessage_LegacyCommand_oneof_kind::Verack(x)
}
Message_Command_oneof_kind::GetPeers(x) => {
LegacyMessage_LegacyCommand_oneof_kind::GetPeers(x)
}
Message_Command_oneof_kind::Peers(x) => {
LegacyMessage_LegacyCommand_oneof_kind::Peers(x)
}
Message_Command_oneof_kind::Block(x) => {
LegacyMessage_LegacyCommand_oneof_kind::Block(x.into())
}
Message_Command_oneof_kind::InventoryAnnouncement(x) => {
LegacyMessage_LegacyCommand_oneof_kind::InventoryAnnouncement(x)
}
Message_Command_oneof_kind::InventoryRequest(x) => {
LegacyMessage_LegacyCommand_oneof_kind::InventoryRequest(x)
}
Message_Command_oneof_kind::LastBeacon(x) => {
LegacyMessage_LegacyCommand_oneof_kind::LastBeacon(x)
}
Message_Command_oneof_kind::Transaction(x) => {
LegacyMessage_LegacyCommand_oneof_kind::Transaction(x)
}
Message_Command_oneof_kind::SuperBlockVote(x) => {
LegacyMessage_LegacyCommand_oneof_kind::SuperBlockVote(x)
}
Message_Command_oneof_kind::SuperBlock(x) => {
LegacyMessage_LegacyCommand_oneof_kind::SuperBlock(x)
}
}
}
}
impl From<LegacyMessage_LegacyCommand_oneof_kind> for Message_Command_oneof_kind {
fn from(legacy: LegacyMessage_LegacyCommand_oneof_kind) -> Self {
match legacy {
LegacyMessage_LegacyCommand_oneof_kind::Version(x) => {
Message_Command_oneof_kind::Version(x)
}
LegacyMessage_LegacyCommand_oneof_kind::Verack(x) => {
Message_Command_oneof_kind::Verack(x)
}
LegacyMessage_LegacyCommand_oneof_kind::GetPeers(x) => {
Message_Command_oneof_kind::GetPeers(x)
}
LegacyMessage_LegacyCommand_oneof_kind::Peers(x) => {
Message_Command_oneof_kind::Peers(x)
}
LegacyMessage_LegacyCommand_oneof_kind::Block(x) => {
Message_Command_oneof_kind::Block(x.into())
}
LegacyMessage_LegacyCommand_oneof_kind::InventoryAnnouncement(x) => {
Message_Command_oneof_kind::InventoryAnnouncement(x)
}
LegacyMessage_LegacyCommand_oneof_kind::InventoryRequest(x) => {
Message_Command_oneof_kind::InventoryRequest(x)
}
LegacyMessage_LegacyCommand_oneof_kind::LastBeacon(x) => {
Message_Command_oneof_kind::LastBeacon(x)
}
LegacyMessage_LegacyCommand_oneof_kind::Transaction(x) => {
Message_Command_oneof_kind::Transaction(x)
}
LegacyMessage_LegacyCommand_oneof_kind::SuperBlockVote(x) => {
Message_Command_oneof_kind::SuperBlockVote(x)
}
LegacyMessage_LegacyCommand_oneof_kind::SuperBlock(x) => {
Message_Command_oneof_kind::SuperBlock(x)
}
}
}
}
impl From<Message_Command> for LegacyMessage_LegacyCommand {
fn from(Message_Command { kind, .. }: Message_Command) -> Self {
let mut legacy = LegacyMessage_LegacyCommand::new();
legacy.kind = kind.map(Into::into);
legacy
}
}
impl From<LegacyMessage_LegacyCommand> for Message_Command {
fn from(LegacyMessage_LegacyCommand { kind, .. }: LegacyMessage_LegacyCommand) -> Self {
let mut command = Message_Command::new();
command.kind = kind.map(Into::into);
command
}
}
impl From<crate::proto::schema::witnet::Message> for LegacyMessage {
fn from(
crate::proto::schema::witnet::Message { magic, kind, .. }: crate::proto::schema::witnet::Message,
) -> Self {
let mut legacy = LegacyMessage::new();
legacy.magic = magic;
legacy.kind = kind.map(Into::into);
legacy
}
}
impl From<LegacyMessage> for crate::proto::schema::witnet::Message {
fn from(LegacyMessage { magic, kind, .. }: LegacyMessage) -> Self {
let mut message = crate::proto::schema::witnet::Message::new();
message.magic = magic;
message.kind = kind.map(Into::into);
message
}
}
impl From<LegacyMessage> for Message {
fn from(legacy: LegacyMessage) -> Self {
let pb = crate::proto::schema::witnet::Message::from(legacy);
Message::from_pb(pb).unwrap()
}
}