@@ -20,7 +20,7 @@ use stacks::vm::{
20
20
mem_type_check,
21
21
} ,
22
22
database:: ClaritySerializable ,
23
- types:: { QualifiedContractIdentifier , TupleData } ,
23
+ types:: { QualifiedContractIdentifier , ResponseData , TupleData } ,
24
24
Value ,
25
25
} ;
26
26
@@ -33,6 +33,21 @@ use super::{
33
33
SK_3 ,
34
34
} ;
35
35
36
+ const OTHER_CONTRACT : & ' static str = "
37
+ (define-data-var x uint u0)
38
+ (define-public (f1)
39
+ (ok (var-get x)))
40
+ (define-public (f2 (val uint))
41
+ (ok (var-set x val)))
42
+ " ;
43
+
44
+ const CALL_READ_CONTRACT : & ' static str = "
45
+ (define-public (public-no-write)
46
+ (ok (contract-call? .other f1)))
47
+ (define-public (public-write)
48
+ (ok (contract-call? .other f2 u5)))
49
+ " ;
50
+
36
51
const GET_INFO_CONTRACT : & ' static str = "
37
52
(define-map block-data
38
53
{ height: uint }
@@ -105,14 +120,14 @@ const GET_INFO_CONTRACT: &'static str = "
105
120
(begin
106
121
(unwrap-panic (inner-update-info (- block-height u2)))
107
122
(inner-update-info (- block-height u1))))
108
-
123
+
109
124
(define-trait trait-1 (
110
125
(foo-exec (int) (response int int))))
111
126
112
127
(define-trait trait-2 (
113
128
(get-1 (uint) (response uint uint))
114
129
(get-2 (uint) (response uint uint))))
115
-
130
+
116
131
(define-trait trait-3 (
117
132
(fn-1 (uint) (response uint uint))
118
133
(fn-2 (uint) (response uint uint))))
@@ -123,7 +138,7 @@ const IMPL_TRAIT_CONTRACT: &'static str = "
123
138
(impl-trait .get-info.trait-1)
124
139
(define-private (test-height) burn-block-height)
125
140
(define-public (foo-exec (a int)) (ok 1))
126
-
141
+
127
142
;; implicit trait compliance for trait-2
128
143
(define-public (get-1 (x uint)) (ok u1))
129
144
(define-public (get-2 (x uint)) (ok u1))
@@ -172,9 +187,30 @@ fn integration_test_get_info() {
172
187
173
188
if round == 1 {
174
189
// block-height = 2
190
+ eprintln ! ( "Tenure in 1 started!" ) ;
175
191
let publish_tx =
176
192
make_contract_publish ( & contract_sk, 0 , 0 , "get-info" , GET_INFO_CONTRACT ) ;
177
- eprintln ! ( "Tenure in 1 started!" ) ;
193
+ tenure
194
+ . mem_pool
195
+ . submit_raw (
196
+ & mut chainstate_copy,
197
+ & consensus_hash,
198
+ & header_hash,
199
+ publish_tx,
200
+ )
201
+ . unwrap ( ) ;
202
+ let publish_tx = make_contract_publish ( & contract_sk, 1 , 0 , "other" , OTHER_CONTRACT ) ;
203
+ tenure
204
+ . mem_pool
205
+ . submit_raw (
206
+ & mut chainstate_copy,
207
+ & consensus_hash,
208
+ & header_hash,
209
+ publish_tx,
210
+ )
211
+ . unwrap ( ) ;
212
+ let publish_tx =
213
+ make_contract_publish ( & contract_sk, 2 , 0 , "main" , CALL_READ_CONTRACT ) ;
178
214
tenure
179
215
. mem_pool
180
216
. submit_raw (
@@ -188,7 +224,7 @@ fn integration_test_get_info() {
188
224
// block-height = 3
189
225
let publish_tx = make_contract_publish (
190
226
& contract_sk,
191
- 1 ,
227
+ 3 ,
192
228
0 ,
193
229
"impl-trait-contract" ,
194
230
IMPL_TRAIT_CONTRACT ,
@@ -255,8 +291,8 @@ fn integration_test_get_info() {
255
291
let blocks = StacksChainState :: list_blocks ( & chain_state. db ( ) ) . unwrap ( ) ;
256
292
assert ! ( chain_tip. metadata. block_height == 2 ) ;
257
293
258
- // Block #1 should have 3 txs
259
- assert ! ( chain_tip. block. txs. len( ) == 3 ) ;
294
+ // Block #1 should have 5 txs
295
+ assert ! ( chain_tip. block. txs. len( ) == 5 ) ;
260
296
261
297
let parent = chain_tip. block . header . parent_block ;
262
298
let bhh = & chain_tip. metadata . index_block_hash ( ) ;
@@ -467,7 +503,7 @@ fn integration_test_get_info() {
467
503
eprintln ! ( "Test: GET {}" , path) ;
468
504
let res = client. get ( & path) . send ( ) . unwrap ( ) . json :: < AccountEntryResponse > ( ) . unwrap ( ) ;
469
505
assert_eq ! ( u128 :: from_str_radix( & res. balance[ 2 ..] , 16 ) . unwrap( ) , 0 ) ;
470
- assert_eq ! ( res. nonce, 2 ) ;
506
+ assert_eq ! ( res. nonce, 4 ) ;
471
507
assert ! ( res. nonce_proof. is_some( ) ) ;
472
508
assert ! ( res. balance_proof. is_some( ) ) ;
473
509
@@ -581,6 +617,49 @@ fn integration_test_get_info() {
581
617
"(get-exotic-data-info u3)" ) ;
582
618
assert_eq ! ( result_data, expected_data) ;
583
619
620
+ // how about a non read-only function call which does not modify anything
621
+ let path = format ! ( "{}/v2/contracts/call-read/{}/{}/{}" , & http_origin, & contract_addr, "main" , "public-no-write" ) ;
622
+ eprintln ! ( "Test: POST {}" , path) ;
623
+
624
+ let body = CallReadOnlyRequestBody {
625
+ sender : "'SP139Q3N9RXCJCD1XVA4N5RYWQ5K9XQ0T9PKQ8EE5" . into ( ) ,
626
+ arguments : vec ! [ ]
627
+ } ;
628
+
629
+ let res = client. post ( & path)
630
+ . json ( & body)
631
+ . send ( )
632
+ . unwrap ( ) . json :: < serde_json:: Value > ( ) . unwrap ( ) ;
633
+ assert ! ( res. get( "cause" ) . is_none( ) ) ;
634
+ assert ! ( res[ "okay" ] . as_bool( ) . unwrap( ) ) ;
635
+
636
+ let result_data = Value :: try_deserialize_hex_untyped ( & res[ "result" ] . as_str ( ) . unwrap ( ) [ 2 ..] ) . unwrap ( ) ;
637
+ let expected_data = Value :: Response ( ResponseData {
638
+ committed : true ,
639
+ data : Box :: new ( Value :: Response ( ResponseData {
640
+ committed : true ,
641
+ data : Box :: new ( Value :: UInt ( 0 ) )
642
+ } ) )
643
+ } ) ;
644
+ assert_eq ! ( result_data, expected_data) ;
645
+
646
+ // how about a non read-only function call which does modify something and should fail
647
+ let path = format ! ( "{}/v2/contracts/call-read/{}/{}/{}" , & http_origin, & contract_addr, "main" , "public-write" ) ;
648
+ eprintln ! ( "Test: POST {}" , path) ;
649
+
650
+ let body = CallReadOnlyRequestBody {
651
+ sender : "'SP139Q3N9RXCJCD1XVA4N5RYWQ5K9XQ0T9PKQ8EE5" . into ( ) ,
652
+ arguments : vec ! [ ]
653
+ } ;
654
+
655
+ let res = client. post ( & path)
656
+ . json ( & body)
657
+ . send ( )
658
+ . unwrap ( ) . json :: < serde_json:: Value > ( ) . unwrap ( ) ;
659
+ assert ! ( res. get( "cause" ) . is_some( ) ) ;
660
+ assert ! ( !res[ "okay" ] . as_bool( ) . unwrap( ) ) ;
661
+ assert ! ( res[ "cause" ] . as_str( ) . unwrap( ) . contains( "NotReadOnly" ) ) ;
662
+
584
663
// let's try a call with a url-encoded string.
585
664
let path = format ! ( "{}/v2/contracts/call-read/{}/{}/{}" , & http_origin, & contract_addr, "get-info" ,
586
665
"get-exotic-data-info%3F" ) ;
@@ -636,7 +715,7 @@ fn integration_test_get_info() {
636
715
. send ( )
637
716
. unwrap ( ) . json :: < serde_json:: Value > ( ) . unwrap ( ) ;
638
717
639
- eprintln ! ( "{}" , res[ "cause" ] . as_str( ) . unwrap( ) ) ;
718
+ eprintln ! ( "{:#? }" , res[ "cause" ] . as_str( ) . unwrap( ) ) ;
640
719
assert ! ( res. get( "result" ) . is_none( ) ) ;
641
720
assert ! ( !res[ "okay" ] . as_bool( ) . unwrap( ) ) ;
642
721
assert ! ( res[ "cause" ] . as_str( ) . unwrap( ) . contains( "NotReadOnly" ) ) ;
0 commit comments