@@ -76,6 +76,68 @@ pub enum StoreType {
76
76
PoxUnlockHeight = 0x15 ,
77
77
}
78
78
79
+ impl TryFrom < & str > for StoreType {
80
+ type Error = String ;
81
+
82
+ fn try_from ( value : & str ) -> core:: result:: Result < Self , Self :: Error > {
83
+ use self :: StoreType :: * ;
84
+
85
+ let hex_value = u8:: from_str_radix ( value, 10 ) . map_err ( |e| e. to_string ( ) ) ?;
86
+ match hex_value {
87
+ 0x00 => Ok ( DataMap ) ,
88
+ 0x01 => Ok ( Variable ) ,
89
+ 0x02 => Ok ( FungibleToken ) ,
90
+ 0x03 => Ok ( CirculatingSupply ) ,
91
+ 0x04 => Ok ( NonFungibleToken ) ,
92
+ 0x05 => Ok ( DataMapMeta ) ,
93
+ 0x06 => Ok ( VariableMeta ) ,
94
+ 0x07 => Ok ( FungibleTokenMeta ) ,
95
+ 0x08 => Ok ( NonFungibleTokenMeta ) ,
96
+ 0x09 => Ok ( Contract ) ,
97
+ 0x10 => Ok ( SimmedBlock ) ,
98
+ 0x11 => Ok ( SimmedBlockHeight ) ,
99
+ 0x12 => Ok ( Nonce ) ,
100
+ 0x13 => Ok ( STXBalance ) ,
101
+ 0x14 => Ok ( PoxSTXLockup ) ,
102
+ 0x15 => Ok ( PoxUnlockHeight ) ,
103
+ _ => Err ( "Invalid StoreType" . into ( ) ) ,
104
+ }
105
+ }
106
+ }
107
+
108
+ pub enum ContractDataVarName {
109
+ Contract ,
110
+ ContractSize ,
111
+ ContractSrc ,
112
+ ContractDataSize ,
113
+ }
114
+
115
+ impl ContractDataVarName {
116
+ pub fn as_str ( & self ) -> & str {
117
+ match self {
118
+ Self :: Contract => "contract" ,
119
+ Self :: ContractSize => "contract-size" ,
120
+ Self :: ContractSrc => "contract-src" ,
121
+ Self :: ContractDataSize => "contract-data-size" ,
122
+ }
123
+ }
124
+ }
125
+
126
+ impl TryFrom < & str > for ContractDataVarName {
127
+ type Error = String ;
128
+
129
+ fn try_from ( value : & str ) -> core:: result:: Result < Self , Self :: Error > {
130
+ use self :: ContractDataVarName :: * ;
131
+ match value {
132
+ "contract" => Ok ( Contract ) ,
133
+ "contract-size" => Ok ( ContractSize ) ,
134
+ "contract-src" => Ok ( ContractSrc ) ,
135
+ "contract-data-size" => Ok ( ContractDataSize ) ,
136
+ _ => Err ( "Invalid ContractDataVarName" . into ( ) ) ,
137
+ }
138
+ }
139
+ }
140
+
79
141
pub struct ClarityDatabase < ' a > {
80
142
pub store : RollbackWrapper < ' a > ,
81
143
headers_db : & ' a dyn HeadersDB ,
@@ -576,12 +638,18 @@ impl<'a> ClarityDatabase<'a> {
576
638
self . store
577
639
. prepare_for_contract_metadata ( contract_identifier, hash) ?;
578
640
// insert contract-size
579
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract-size" ) ;
641
+ let key = ClarityDatabase :: make_metadata_key (
642
+ StoreType :: Contract ,
643
+ ContractDataVarName :: ContractSize . as_str ( ) ,
644
+ ) ;
580
645
self . insert_metadata ( contract_identifier, & key, & ( contract_content. len ( ) as u64 ) ) ?;
581
646
582
647
// insert contract-src
583
648
if STORE_CONTRACT_SRC_INTERFACE {
584
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract-src" ) ;
649
+ let key = ClarityDatabase :: make_metadata_key (
650
+ StoreType :: Contract ,
651
+ ContractDataVarName :: ContractSrc . as_str ( ) ,
652
+ ) ;
585
653
self . insert_metadata ( contract_identifier, & key, & contract_content. to_string ( ) ) ?;
586
654
}
587
655
Ok ( ( ) )
@@ -591,7 +659,10 @@ impl<'a> ClarityDatabase<'a> {
591
659
& mut self ,
592
660
contract_identifier : & QualifiedContractIdentifier ,
593
661
) -> Option < String > {
594
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract-src" ) ;
662
+ let key = ClarityDatabase :: make_metadata_key (
663
+ StoreType :: Contract ,
664
+ ContractDataVarName :: ContractSrc . as_str ( ) ,
665
+ ) ;
595
666
self . fetch_metadata ( contract_identifier, & key)
596
667
. ok ( )
597
668
. flatten ( )
@@ -700,15 +771,21 @@ impl<'a> ClarityDatabase<'a> {
700
771
& mut self ,
701
772
contract_identifier : & QualifiedContractIdentifier ,
702
773
) -> Result < u64 > {
703
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract-size" ) ;
774
+ let key = ClarityDatabase :: make_metadata_key (
775
+ StoreType :: Contract ,
776
+ ContractDataVarName :: ContractSize . as_str ( ) ,
777
+ ) ;
704
778
let contract_size: u64 =
705
779
self . fetch_metadata ( contract_identifier, & key) ?
706
780
. ok_or_else ( || {
707
781
InterpreterError :: Expect (
708
782
"Failed to read non-consensus contract metadata, even though contract exists in MARF."
709
783
. into ( ) )
710
784
} ) ?;
711
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract-data-size" ) ;
785
+ let key = ClarityDatabase :: make_metadata_key (
786
+ StoreType :: Contract ,
787
+ ContractDataVarName :: ContractDataSize . as_str ( ) ,
788
+ ) ;
712
789
let data_size: u64 = self
713
790
. fetch_metadata ( contract_identifier, & key) ?
714
791
. ok_or_else ( || {
@@ -727,7 +804,10 @@ impl<'a> ClarityDatabase<'a> {
727
804
contract_identifier : & QualifiedContractIdentifier ,
728
805
data_size : u64 ,
729
806
) -> Result < ( ) > {
730
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract-size" ) ;
807
+ let key = ClarityDatabase :: make_metadata_key (
808
+ StoreType :: Contract ,
809
+ ContractDataVarName :: ContractSize . as_str ( ) ,
810
+ ) ;
731
811
let contract_size: u64 =
732
812
self . fetch_metadata ( contract_identifier, & key) ?
733
813
. ok_or_else ( || {
@@ -737,7 +817,10 @@ impl<'a> ClarityDatabase<'a> {
737
817
} ) ?;
738
818
contract_size. cost_overflow_add ( data_size) ?;
739
819
740
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract-data-size" ) ;
820
+ let key = ClarityDatabase :: make_metadata_key (
821
+ StoreType :: Contract ,
822
+ ContractDataVarName :: ContractDataSize . as_str ( ) ,
823
+ ) ;
741
824
self . insert_metadata ( contract_identifier, & key, & data_size) ?;
742
825
Ok ( ( ) )
743
826
}
@@ -747,21 +830,30 @@ impl<'a> ClarityDatabase<'a> {
747
830
contract_identifier : & QualifiedContractIdentifier ,
748
831
contract : Contract ,
749
832
) -> Result < ( ) > {
750
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract" ) ;
833
+ let key = ClarityDatabase :: make_metadata_key (
834
+ StoreType :: Contract ,
835
+ ContractDataVarName :: Contract . as_str ( ) ,
836
+ ) ;
751
837
self . insert_metadata ( contract_identifier, & key, & contract) ?;
752
838
Ok ( ( ) )
753
839
}
754
840
755
841
pub fn has_contract ( & mut self , contract_identifier : & QualifiedContractIdentifier ) -> bool {
756
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract" ) ;
842
+ let key = ClarityDatabase :: make_metadata_key (
843
+ StoreType :: Contract ,
844
+ ContractDataVarName :: Contract . as_str ( ) ,
845
+ ) ;
757
846
self . store . has_metadata_entry ( contract_identifier, & key)
758
847
}
759
848
760
849
pub fn get_contract (
761
850
& mut self ,
762
851
contract_identifier : & QualifiedContractIdentifier ,
763
852
) -> Result < Contract > {
764
- let key = ClarityDatabase :: make_metadata_key ( StoreType :: Contract , "contract" ) ;
853
+ let key = ClarityDatabase :: make_metadata_key (
854
+ StoreType :: Contract ,
855
+ ContractDataVarName :: Contract . as_str ( ) ,
856
+ ) ;
765
857
let mut data: Contract = self . fetch_metadata ( contract_identifier, & key) ?
766
858
. ok_or_else ( || InterpreterError :: Expect (
767
859
"Failed to read non-consensus contract metadata, even though contract exists in MARF."
0 commit comments