@@ -30,7 +30,7 @@ pub trait ClaritySerializable {
30
30
}
31
31
32
32
pub trait ClarityDeserializable < T > {
33
- fn deserialize ( json : & str ) -> Result < T > ;
33
+ fn deserialize ( json : & str ) -> Result < T , Error > ;
34
34
}
35
35
36
36
impl ClaritySerializable for String {
@@ -40,7 +40,7 @@ impl ClaritySerializable for String {
40
40
}
41
41
42
42
impl ClarityDeserializable < String > for String {
43
- fn deserialize ( serialized : & str ) -> Result < String > {
43
+ fn deserialize ( serialized : & str ) -> Result < String , Error > {
44
44
Ok ( serialized. into ( ) )
45
45
}
46
46
}
@@ -54,7 +54,7 @@ macro_rules! clarity_serializable {
54
54
}
55
55
impl ClarityDeserializable <$Name> for $Name {
56
56
#[ cfg( not( target_family = "wasm" ) ) ]
57
- fn deserialize( json: & str ) -> Result <Self > {
57
+ fn deserialize( json: & str ) -> Result <Self , Error > {
58
58
let mut deserializer = serde_json:: Deserializer :: from_str( & json) ;
59
59
// serde's default 128 depth limit can be exhausted
60
60
// by a 64-stack-depth AST, so disable the recursion limit
@@ -67,7 +67,7 @@ macro_rules! clarity_serializable {
67
67
} )
68
68
}
69
69
#[ cfg( target_family = "wasm" ) ]
70
- fn deserialize( json: & str ) -> Result <Self > {
70
+ fn deserialize( json: & str ) -> Result <Self , Error > {
71
71
serde_json:: from_str( json) . map_err( |_| {
72
72
InterpreterError :: Expect ( "Failed to deserialize vm.Value" . into( ) ) . into( )
73
73
} )
@@ -166,8 +166,6 @@ pub struct STXBalanceSnapshot<'db, 'conn> {
166
166
db_ref : & ' conn mut ClarityDatabase < ' db > ,
167
167
}
168
168
169
- type Result < T > = std:: result:: Result < T , Error > ;
170
-
171
169
impl ClaritySerializable for STXBalance {
172
170
#[ allow( clippy:: expect_used) ]
173
171
fn serialize ( & self ) -> String {
@@ -259,7 +257,7 @@ impl ClaritySerializable for STXBalance {
259
257
}
260
258
261
259
impl ClarityDeserializable < STXBalance > for STXBalance {
262
- fn deserialize ( input : & str ) -> Result < Self > {
260
+ fn deserialize ( input : & str ) -> Result < Self , Error > {
263
261
let bytes = hex_bytes ( input) . map_err ( |_| {
264
262
InterpreterError :: Expect ( "STXBalance deserialization: failed decoding bytes." . into ( ) )
265
263
} ) ?;
@@ -376,12 +374,12 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
376
374
& self . balance
377
375
}
378
376
379
- pub fn save ( self ) -> Result < ( ) > {
377
+ pub fn save ( self ) -> Result < ( ) , Error > {
380
378
let key = ClarityDatabase :: make_key_for_account_balance ( & self . principal ) ;
381
379
self . db_ref . put_data ( & key, & self . balance )
382
380
}
383
381
384
- pub fn transfer_to ( mut self , recipient : & PrincipalData , amount : u128 ) -> Result < ( ) > {
382
+ pub fn transfer_to ( mut self , recipient : & PrincipalData , amount : u128 ) -> Result < ( ) , Error > {
385
383
if !self . can_transfer ( amount) ? {
386
384
return Err ( InterpreterError :: InsufficientBalance . into ( ) ) ;
387
385
}
@@ -402,7 +400,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
402
400
Ok ( ( ) )
403
401
}
404
402
405
- pub fn get_available_balance ( & mut self ) -> Result < u128 > {
403
+ pub fn get_available_balance ( & mut self ) -> Result < u128 , Error > {
406
404
let v1_unlock_height = self . db_ref . get_v1_unlock_height ( ) ;
407
405
let v2_unlock_height = self . db_ref . get_v2_unlock_height ( ) ?;
408
406
let v3_unlock_height = self . db_ref . get_v3_unlock_height ( ) ?;
@@ -414,7 +412,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
414
412
)
415
413
}
416
414
417
- pub fn canonical_balance_repr ( & mut self ) -> Result < STXBalance > {
415
+ pub fn canonical_balance_repr ( & mut self ) -> Result < STXBalance , Error > {
418
416
let v1_unlock_height = self . db_ref . get_v1_unlock_height ( ) ;
419
417
let v2_unlock_height = self . db_ref . get_v2_unlock_height ( ) ?;
420
418
let v3_unlock_height = self . db_ref . get_v3_unlock_height ( ) ?;
@@ -429,7 +427,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
429
427
. 0 )
430
428
}
431
429
432
- pub fn has_locked_tokens ( & mut self ) -> Result < bool > {
430
+ pub fn has_locked_tokens ( & mut self ) -> Result < bool , Error > {
433
431
let v1_unlock_height = self . db_ref . get_v1_unlock_height ( ) ;
434
432
let v2_unlock_height = self . db_ref . get_v2_unlock_height ( ) ?;
435
433
let v3_unlock_height = self . db_ref . get_v3_unlock_height ( ) ?;
@@ -441,7 +439,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
441
439
) )
442
440
}
443
441
444
- pub fn has_unlockable_tokens ( & mut self ) -> Result < bool > {
442
+ pub fn has_unlockable_tokens ( & mut self ) -> Result < bool , Error > {
445
443
let v1_unlock_height = self . db_ref . get_v1_unlock_height ( ) ;
446
444
let v2_unlock_height = self . db_ref . get_v2_unlock_height ( ) ?;
447
445
let v3_unlock_height = self . db_ref . get_v3_unlock_height ( ) ?;
@@ -453,11 +451,11 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
453
451
) )
454
452
}
455
453
456
- pub fn can_transfer ( & mut self , amount : u128 ) -> Result < bool > {
454
+ pub fn can_transfer ( & mut self , amount : u128 ) -> Result < bool , Error > {
457
455
Ok ( self . get_available_balance ( ) ? >= amount)
458
456
}
459
457
460
- pub fn debit ( & mut self , amount : u128 ) -> Result < ( ) > {
458
+ pub fn debit ( & mut self , amount : u128 ) -> Result < ( ) , Error > {
461
459
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
462
460
if unlocked > 0 {
463
461
debug ! ( "Consolidated after account-debit" ) ;
@@ -466,7 +464,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
466
464
self . balance . debit_unlocked_amount ( amount)
467
465
}
468
466
469
- pub fn credit ( & mut self , amount : u128 ) -> Result < ( ) > {
467
+ pub fn credit ( & mut self , amount : u128 ) -> Result < ( ) , Error > {
470
468
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
471
469
if unlocked > 0 {
472
470
debug ! ( "Consolidated after account-credit" ) ;
@@ -482,7 +480,11 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
482
480
self . balance = balance;
483
481
}
484
482
485
- pub fn lock_tokens_v1 ( & mut self , amount_to_lock : u128 , unlock_burn_height : u64 ) -> Result < ( ) > {
483
+ pub fn lock_tokens_v1 (
484
+ & mut self ,
485
+ amount_to_lock : u128 ,
486
+ unlock_burn_height : u64 ,
487
+ ) -> Result < ( ) , Error > {
486
488
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
487
489
if unlocked > 0 {
488
490
debug ! ( "Consolidated after account-token-lock" ) ;
@@ -528,7 +530,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
528
530
529
531
/// Return true iff `self` represents a snapshot that has a lock
530
532
/// created by PoX v2.
531
- pub fn is_v2_locked ( & mut self ) -> Result < bool > {
533
+ pub fn is_v2_locked ( & mut self ) -> Result < bool , Error > {
532
534
match self . canonical_balance_repr ( ) ? {
533
535
STXBalance :: LockedPoxTwo { .. } => Ok ( true ) ,
534
536
_ => Ok ( false ) ,
@@ -537,7 +539,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
537
539
538
540
/// Increase the account's current lock to `new_total_locked`.
539
541
/// Panics if `self` was not locked by V2 PoX.
540
- pub fn increase_lock_v2 ( & mut self , new_total_locked : u128 ) -> Result < ( ) > {
542
+ pub fn increase_lock_v2 ( & mut self , new_total_locked : u128 ) -> Result < ( ) , Error > {
541
543
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
542
544
if unlocked > 0 {
543
545
debug ! ( "Consolidated after extend-token-lock" ) ;
@@ -586,7 +588,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
586
588
/// Extend this account's current lock to `unlock_burn_height`.
587
589
/// After calling, this method will set the balance to a "LockedPoxTwo" balance,
588
590
/// because this method is only invoked as a result of PoX2 interactions
589
- pub fn extend_lock_v2 ( & mut self , unlock_burn_height : u64 ) -> Result < ( ) > {
591
+ pub fn extend_lock_v2 ( & mut self , unlock_burn_height : u64 ) -> Result < ( ) , Error > {
590
592
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
591
593
if unlocked > 0 {
592
594
debug ! ( "Consolidated after extend-token-lock" ) ;
@@ -619,7 +621,11 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
619
621
/// Lock `amount_to_lock` tokens on this account until `unlock_burn_height`.
620
622
/// After calling, this method will set the balance to a "LockedPoxTwo" balance,
621
623
/// because this method is only invoked as a result of PoX2 interactions
622
- pub fn lock_tokens_v2 ( & mut self , amount_to_lock : u128 , unlock_burn_height : u64 ) -> Result < ( ) > {
624
+ pub fn lock_tokens_v2 (
625
+ & mut self ,
626
+ amount_to_lock : u128 ,
627
+ unlock_burn_height : u64 ,
628
+ ) -> Result < ( ) , Error > {
623
629
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
624
630
if unlocked > 0 {
625
631
debug ! ( "Consolidated after account-token-lock" ) ;
@@ -668,7 +674,11 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
668
674
/// Lock `amount_to_lock` tokens on this account until `unlock_burn_height`.
669
675
/// After calling, this method will set the balance to a "LockedPoxThree" balance,
670
676
/// because this method is only invoked as a result of PoX3 interactions
671
- pub fn lock_tokens_v3 ( & mut self , amount_to_lock : u128 , unlock_burn_height : u64 ) -> Result < ( ) > {
677
+ pub fn lock_tokens_v3 (
678
+ & mut self ,
679
+ amount_to_lock : u128 ,
680
+ unlock_burn_height : u64 ,
681
+ ) -> Result < ( ) , Error > {
672
682
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
673
683
if unlocked > 0 {
674
684
debug ! ( "Consolidated after account-token-lock" ) ;
@@ -718,7 +728,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
718
728
/// Extend this account's current lock to `unlock_burn_height`.
719
729
/// After calling, this method will set the balance to a "LockedPoxThree" balance,
720
730
/// because this method is only invoked as a result of PoX3 interactions
721
- pub fn extend_lock_v3 ( & mut self , unlock_burn_height : u64 ) -> Result < ( ) > {
731
+ pub fn extend_lock_v3 ( & mut self , unlock_burn_height : u64 ) -> Result < ( ) , Error > {
722
732
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
723
733
if unlocked > 0 {
724
734
debug ! ( "Consolidated after extend-token-lock" ) ;
@@ -750,7 +760,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
750
760
751
761
/// Increase the account's current lock to `new_total_locked`.
752
762
/// Panics if `self` was not locked by V3 PoX.
753
- pub fn increase_lock_v3 ( & mut self , new_total_locked : u128 ) -> Result < ( ) > {
763
+ pub fn increase_lock_v3 ( & mut self , new_total_locked : u128 ) -> Result < ( ) , Error > {
754
764
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
755
765
if unlocked > 0 {
756
766
debug ! ( "Consolidated after extend-token-lock" ) ;
@@ -795,7 +805,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
795
805
796
806
/// Return true iff `self` represents a snapshot that has a lock
797
807
/// created by PoX v3.
798
- pub fn is_v3_locked ( & mut self ) -> Result < bool > {
808
+ pub fn is_v3_locked ( & mut self ) -> Result < bool , Error > {
799
809
match self . canonical_balance_repr ( ) ? {
800
810
STXBalance :: LockedPoxThree { .. } => Ok ( true ) ,
801
811
_ => Ok ( false ) ,
@@ -807,7 +817,11 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
807
817
/// Lock `amount_to_lock` tokens on this account until `unlock_burn_height`.
808
818
/// After calling, this method will set the balance to a "LockedPoxFour" balance,
809
819
/// because this method is only invoked as a result of PoX4 interactions
810
- pub fn lock_tokens_v4 ( & mut self , amount_to_lock : u128 , unlock_burn_height : u64 ) -> Result < ( ) > {
820
+ pub fn lock_tokens_v4 (
821
+ & mut self ,
822
+ amount_to_lock : u128 ,
823
+ unlock_burn_height : u64 ,
824
+ ) -> Result < ( ) , Error > {
811
825
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
812
826
if unlocked > 0 {
813
827
debug ! ( "Consolidated after account-token-lock" ) ;
@@ -846,7 +860,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
846
860
/// Extend this account's current lock to `unlock_burn_height`.
847
861
/// After calling, this method will set the balance to a "LockedPoxFour" balance,
848
862
/// because this method is only invoked as a result of PoX3 interactions
849
- pub fn extend_lock_v4 ( & mut self , unlock_burn_height : u64 ) -> Result < ( ) > {
863
+ pub fn extend_lock_v4 ( & mut self , unlock_burn_height : u64 ) -> Result < ( ) , Error > {
850
864
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
851
865
if unlocked > 0 {
852
866
debug ! ( "Consolidated after extend-token-lock" ) ;
@@ -872,7 +886,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
872
886
873
887
/// Increase the account's current lock to `new_total_locked`.
874
888
/// Panics if `self` was not locked by V3 PoX.
875
- pub fn increase_lock_v4 ( & mut self , new_total_locked : u128 ) -> Result < ( ) > {
889
+ pub fn increase_lock_v4 ( & mut self , new_total_locked : u128 ) -> Result < ( ) , Error > {
876
890
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
877
891
if unlocked > 0 {
878
892
debug ! ( "Consolidated after extend-token-lock" ) ;
@@ -912,7 +926,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
912
926
913
927
/// Return true iff `self` represents a snapshot that has a lock
914
928
/// created by PoX v3.
915
- pub fn is_v4_locked ( & mut self ) -> Result < bool > {
929
+ pub fn is_v4_locked ( & mut self ) -> Result < bool , Error > {
916
930
match self . canonical_balance_repr ( ) ? {
917
931
STXBalance :: LockedPoxFour { .. } => Ok ( true ) ,
918
932
_ => Ok ( false ) ,
@@ -923,7 +937,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
923
937
924
938
/// If this snapshot is locked, then alter the lock height to be
925
939
/// the next burn block (i.e., `self.burn_block_height + 1`)
926
- pub fn accelerate_unlock ( & mut self ) -> Result < ( ) > {
940
+ pub fn accelerate_unlock ( & mut self ) -> Result < ( ) , Error > {
927
941
let unlocked = self . unlock_available_tokens_if_any ( ) ?;
928
942
if unlocked > 0 {
929
943
debug ! ( "Consolidated after account-token-lock" ) ;
@@ -971,7 +985,7 @@ impl<'db, 'conn> STXBalanceSnapshot<'db, 'conn> {
971
985
972
986
/// Unlock any tokens that are unlockable at the current
973
987
/// burn block height, and return the amount newly unlocked
974
- fn unlock_available_tokens_if_any ( & mut self ) -> Result < u128 > {
988
+ fn unlock_available_tokens_if_any ( & mut self ) -> Result < u128 , Error > {
975
989
let ( new_balance, unlocked) = self . balance . canonical_repr_at_block (
976
990
self . burn_block_height ,
977
991
self . db_ref . get_v1_unlock_height ( ) ,
@@ -1088,7 +1102,7 @@ impl STXBalance {
1088
1102
}
1089
1103
}
1090
1104
1091
- fn debit_unlocked_amount ( & mut self , delta : u128 ) -> Result < ( ) > {
1105
+ fn debit_unlocked_amount ( & mut self , delta : u128 ) -> Result < ( ) , Error > {
1092
1106
match self {
1093
1107
STXBalance :: Unlocked {
1094
1108
amount : amount_unlocked,
@@ -1150,7 +1164,7 @@ impl STXBalance {
1150
1164
v1_unlock_height : u32 ,
1151
1165
v2_unlock_height : u32 ,
1152
1166
v3_unlock_height : u32 ,
1153
- ) -> Result < ( STXBalance , u128 ) > {
1167
+ ) -> Result < ( STXBalance , u128 ) , Error > {
1154
1168
if self . has_unlockable_tokens_at_burn_block (
1155
1169
burn_block_height,
1156
1170
v1_unlock_height,
@@ -1174,7 +1188,7 @@ impl STXBalance {
1174
1188
v1_unlock_height : u32 ,
1175
1189
v2_unlock_height : u32 ,
1176
1190
v3_unlock_height : u32 ,
1177
- ) -> Result < u128 > {
1191
+ ) -> Result < u128 , Error > {
1178
1192
if self . has_unlockable_tokens_at_burn_block (
1179
1193
burn_block_height,
1180
1194
v1_unlock_height,
@@ -1243,7 +1257,7 @@ impl STXBalance {
1243
1257
}
1244
1258
}
1245
1259
1246
- pub fn get_total_balance ( & self ) -> Result < u128 > {
1260
+ pub fn get_total_balance ( & self ) -> Result < u128 , Error > {
1247
1261
let ( unlocked, locked) = match self {
1248
1262
STXBalance :: Unlocked { amount } => ( * amount, 0 ) ,
1249
1263
STXBalance :: LockedPoxOne {
@@ -1448,7 +1462,7 @@ impl STXBalance {
1448
1462
v1_unlock_height : u32 ,
1449
1463
v2_unlock_height : u32 ,
1450
1464
v3_unlock_height : u32 ,
1451
- ) -> Result < bool > {
1465
+ ) -> Result < bool , Error > {
1452
1466
Ok ( self . get_available_balance_at_burn_block (
1453
1467
burn_block_height,
1454
1468
v1_unlock_height,
0 commit comments