@@ -61,8 +61,7 @@ pub mod execute {
61
61
use crate :: msg;
62
62
use crate :: state:: BucketLimits ;
63
63
use crate :: ContractError :: ObjectPinned ;
64
- use cosmwasm_std:: { Order , StdError , Uint128 } ;
65
- use std:: any:: type_name;
64
+ use cosmwasm_std:: { Order , Uint128 } ;
66
65
67
66
pub fn store_object (
68
67
deps : DepsMut < ' _ > ,
@@ -118,6 +117,7 @@ pub mod execute {
118
117
// store object data
119
118
let id = crypto:: hash ( & bucket. config . hash_algorithm . into ( ) , & data. 0 ) ;
120
119
let data_path = DATA . key ( id. clone ( ) ) ;
120
+
121
121
if data_path. has ( deps. storage ) {
122
122
return Err ( ContractError :: Bucket ( BucketError :: ObjectAlreadyStored ) ) ;
123
123
}
@@ -178,22 +178,25 @@ pub mod execute {
178
178
return Ok ( res) ;
179
179
}
180
180
181
- let o = objects ( ) . update ( deps. storage , id. clone ( ) , |o| -> Result < Object , StdError > {
182
- o. map ( |mut e : Object | -> Object {
183
- e. pin_count += Uint128 :: one ( ) ;
184
- e
185
- } )
186
- . ok_or_else ( || StdError :: not_found ( type_name :: < Object > ( ) ) )
187
- } ) ?;
181
+ let object = objects ( ) . load ( deps. storage , id. clone ( ) ) ?;
182
+ let mut updated_object = object. clone ( ) ;
183
+ updated_object. pin_count += Uint128 :: one ( ) ;
184
+
185
+ objects ( ) . replace (
186
+ deps. storage ,
187
+ id. clone ( ) ,
188
+ Some ( & updated_object) ,
189
+ Some ( & object) ,
190
+ ) ?;
188
191
189
192
let bucket = BUCKET . load ( deps. storage ) ?;
190
193
191
194
match bucket. limits {
192
195
BucketLimits {
193
196
max_object_pins : Some ( max) ,
194
197
..
195
- } if max < o . pin_count => {
196
- Err ( BucketError :: MaxObjectPinsLimitExceeded ( o . pin_count , max) . into ( ) )
198
+ } if max < updated_object . pin_count => {
199
+ Err ( BucketError :: MaxObjectPinsLimitExceeded ( updated_object . pin_count , max) . into ( ) )
197
200
}
198
201
_ => {
199
202
pins ( ) . save (
@@ -413,7 +416,6 @@ impl From<state::HashAlgorithm> for crypto::HashAlgorithm {
413
416
#[ cfg( test) ]
414
417
mod tests {
415
418
use super :: * ;
416
- use crate :: crypto:: Hash ;
417
419
use crate :: error:: BucketError ;
418
420
use crate :: msg:: {
419
421
BucketConfig , BucketLimitsBuilder , BucketResponse , CompressionAlgorithm , HashAlgorithm ,
@@ -425,6 +427,24 @@ mod tests {
425
427
use cosmwasm_std:: { from_binary, Attribute , Order , StdError , Uint128 } ;
426
428
use std:: any:: type_name;
427
429
430
+ fn decode_hex ( hex : & str ) -> Vec < u8 > {
431
+ base16ct:: lower:: decode_vec ( hex) . unwrap ( )
432
+ }
433
+
434
+ fn with_namespace ( key : & [ u8 ] ) -> Vec < u8 > {
435
+ let namespace = decode_hex ( "00064f424a454354" ) ;
436
+ let mut v = Vec :: with_capacity ( namespace. len ( ) + key. len ( ) ) ;
437
+ v. extend ( namespace) ;
438
+ v. extend_from_slice ( key) ;
439
+ v
440
+ }
441
+
442
+ fn not_found_object_info < T > ( hex : & str ) -> String {
443
+ let type_name = type_name :: < T > ( ) ;
444
+ let key = with_namespace ( & decode_hex ( hex) ) ;
445
+ format ! ( "type: {type_name}; key: {:02X?}" , key)
446
+ }
447
+
428
448
#[ test]
429
449
fn proper_initialization ( ) {
430
450
let mut deps = mock_dependencies ( ) ;
@@ -617,10 +637,6 @@ mod tests {
617
637
assert_eq ! ( "foobar" , value. name) ;
618
638
}
619
639
620
- fn decode_hash ( hash : String ) -> Hash {
621
- base16ct:: lower:: decode_vec ( hash) . unwrap ( ) . into ( )
622
- }
623
-
624
640
#[ test]
625
641
fn store_object_without_limits ( ) {
626
642
let obj1_content = & general_purpose:: STANDARD . encode ( "hello" ) ;
@@ -762,15 +778,15 @@ mod tests {
762
778
assert_eq ! (
763
779
Binary :: from_base64( content) . unwrap( ) ,
764
780
Binary :: from(
765
- DATA . load( & deps. storage, decode_hash ( expected_hash. clone ( ) ) )
781
+ DATA . load( & deps. storage, decode_hex ( & expected_hash) . into ( ) )
766
782
. unwrap( )
767
783
) ,
768
784
) ;
769
785
770
786
let created = objects ( )
771
- . load ( & deps. storage , decode_hash ( expected_hash. clone ( ) ) )
787
+ . load ( & deps. storage , decode_hex ( & expected_hash) . into ( ) )
772
788
. unwrap ( ) ;
773
- assert_eq ! ( created. id, decode_hash ( expected_hash. to_string ( ) ) ) ;
789
+ assert_eq ! ( created. id, decode_hex ( & expected_hash) . into ( ) ) ;
774
790
assert_eq ! ( created. owner, info. sender. clone( ) ) ;
775
791
assert_eq ! ( created. size. u128 ( ) , * expected_size) ;
776
792
assert_eq ! (
@@ -785,7 +801,7 @@ mod tests {
785
801
assert_eq ! (
786
802
pins( ) . has(
787
803
& deps. storage,
788
- ( decode_hash ( expected_hash. to_string ( ) ) , info. clone( ) . sender) ,
804
+ ( decode_hex ( & expected_hash) . into ( ) , info. clone( ) . sender) ,
789
805
) ,
790
806
* pin,
791
807
) ;
@@ -1422,7 +1438,9 @@ mod tests {
1422
1438
senders: vec![ mock_info( "bob" , & [ ] ) ] ,
1423
1439
expected_count: 0 ,
1424
1440
expected_error: Some ( ContractError :: Std ( StdError :: not_found(
1425
- type_name:: <Object >( ) ,
1441
+ not_found_object_info:: <Object >(
1442
+ "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56" ,
1443
+ ) ,
1426
1444
) ) ) ,
1427
1445
expected_object_pin_count: vec![ (
1428
1446
ObjectId :: from(
@@ -1520,7 +1538,7 @@ mod tests {
1520
1538
for ( object_id, count) in case. expected_object_pin_count {
1521
1539
assert_eq ! (
1522
1540
objects( )
1523
- . load( & deps. storage, decode_hash ( object_id) )
1541
+ . load( & deps. storage, decode_hex ( & object_id) . into ( ) )
1524
1542
. unwrap( )
1525
1543
. pin_count,
1526
1544
count
@@ -1689,7 +1707,9 @@ mod tests {
1689
1707
unpin_senders: vec![ mock_info( "martin" , & [ ] ) ] ,
1690
1708
expected_count: 1 ,
1691
1709
expected_error: Some ( ContractError :: Std ( StdError :: not_found(
1692
- type_name:: <Object >( ) ,
1710
+ not_found_object_info:: <Object >(
1711
+ "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56" ,
1712
+ ) ,
1693
1713
) ) ) ,
1694
1714
expected_object_pin_count: vec![ (
1695
1715
ObjectId :: from(
@@ -1801,7 +1821,7 @@ mod tests {
1801
1821
for ( object_id, count) in case. expected_object_pin_count {
1802
1822
assert_eq ! (
1803
1823
objects( )
1804
- . load( & deps. storage, decode_hash ( object_id) )
1824
+ . load( & deps. storage, decode_hex ( & object_id) . into ( ) )
1805
1825
. unwrap( )
1806
1826
. pin_count,
1807
1827
count
@@ -2073,7 +2093,9 @@ mod tests {
2073
2093
after: None ,
2074
2094
first: None ,
2075
2095
} ,
2076
- ContractError :: Std ( StdError :: not_found( type_name:: <Object >( ) ) ) ,
2096
+ ContractError :: Std ( StdError :: not_found( not_found_object_info:: <Object >(
2097
+ "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56" ,
2098
+ ) ) ) ,
2077
2099
) ,
2078
2100
(
2079
2101
QueryMsg :: ObjectPins {
@@ -2195,7 +2217,9 @@ mod tests {
2195
2217
expected_count: 3 ,
2196
2218
expected_total_size: Uint128 :: new( 13 ) ,
2197
2219
expected_error: Some ( ContractError :: Std ( StdError :: not_found(
2198
- type_name:: <Object >( ) ,
2220
+ not_found_object_info:: <Object >(
2221
+ "abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56" ,
2222
+ ) ,
2199
2223
) ) ) ,
2200
2224
} ,
2201
2225
TC {
@@ -2296,9 +2320,9 @@ mod tests {
2296
2320
for object_id in case. forget_objects {
2297
2321
assert_eq ! (
2298
2322
objects( )
2299
- . load( & deps. storage, decode_hash ( object_id) )
2323
+ . load( & deps. storage, decode_hex ( object_id. as_str ( ) ) . into ( ) )
2300
2324
. unwrap_err( ) ,
2301
- StdError :: not_found( type_name :: <Object >( ) )
2325
+ StdError :: not_found( not_found_object_info :: <Object >( & object_id ) )
2302
2326
) ;
2303
2327
}
2304
2328
}
0 commit comments