-
Notifications
You must be signed in to change notification settings - Fork 6
/
mod.rs
1445 lines (1287 loc) · 45.2 KB
/
mod.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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mod serialization;
mod store;
pub mod types;
mod util;
use prost::Message;
use crate::contract::{
serialization::{from_base64, from_base64_json_slice, from_base64_rlp},
store::{
get_consensus_state, get_processed_height, get_processed_time, get_self_height,
set_consensus_meta, set_consensus_state, EMPTY_PREFIX, SUBJECT_PREFIX, SUBSTITUTE_PREFIX,
},
types::ibc::{
apply_prefix, verify_membership, Channel, ChannelId, ClientId, ClientUpgradePath,
ConnectionEnd, ConnectionId, Height, MerklePath, MerklePrefix, MerkleProof, MerkleRoot,
Path as IcsPath, PortId, Sequence,
},
types::msg::{
CheckHeaderAndUpdateStateResult, CheckMisbehaviourAndUpdateStateResult,
CheckSubstituteAndUpdateStateResult, ClientStateCallResponseResult, HandleMsg,
InitializeStateResult, ProcessedTimeResponse, QueryMsg, StatusResult,
VerifyChannelStateResult, VerifyClientConsensusStateResult, VerifyClientStateResult,
VerifyConnectionStateResult, VerifyPacketAcknowledgementResult,
VerifyPacketCommitmentResult, VerifyPacketReceiptAbsenceResult,
VerifyUpgradeAndUpdateStateResult, ZeroCustomFieldsResult
},
types::state::{LightClientState, LightConsensusState},
types::wasm::{
ClientState, ConsensusState, CosmosClientState, CosmosConsensusState, Misbehaviour,
PartialConsensusState, Status, WasmHeader,
},
util::{to_generic_err, u64_to_big_endian, wrap_response, to_binary},
};
use crate::{state::State, traits::FromRlp, traits::ToRlp, types::header::Header};
use cosmwasm_std::{attr, to_vec, Binary};
use cosmwasm_std::{Deps, DepsMut, Env, MessageInfo};
use cosmwasm_std::{HandleResponse, InitResponse, StdError, StdResult};
use std::str::FromStr;
// # A few notes on certain design decisions
// ## Serialization
// RLP is being used in a few methods, why can't we use JSON everywhere?
//
// CosmWasm doesn't accept floating point operations (see: `cosmwasm/packages/vm/src/middleware/deterministic.rs`)
// and that's for a good reason. Even if you're not using floating point arithmetic explicilty,
// some other library might do it behind the scenes. That's exactly what happens with serde json.
//
// For example to deserialize Celo `Header` type, a set of fields needs to be translated from
// String to Int/BigInt (serialized message comes from celo-geth daemon). The following line would
// implicitly use floating point arithmetic:
// ```
// Source: src/serialization/bytes.rs
// let s: &str = Deserialize::deserialize(deserializer)?;
// ```
//
// How can I check if my wasm binary uses floating points?
// * gaia will fail to upload wasm code (validation will fail)
// * run: `wasm2wat target/wasm32-unknown-unknown/release/celo_light_client.wasm | grep f64`
//
// Taken all the possible options I think the easiest way is to use RLP for the structs that fail
// to serialize/deserialize via JSON (ie. Header, LightConsensusState)
//
// ## IBC
// ### Proof
// ICS-23 specifies the generic proof structure (ie. ExistenceProof). Without the other side of the
// bridge (CosmosLC on CeloBlockchain) we can't say for sure what the proof structure is going to
// be (TendermintProof, MerkleProof etc.) for sure.
//
// I've used MerkleProof + MerklePrefix as a placeholder to be revisited once we have the other side of the bridge
// implemented
//
// ### Counterparty Consensus State
// Essentially this is Cosmos/Tendermint consensus state coming from the other side of the bridge. For now it's almost empty datastructure,
// use as a placeholder.
//
// ### Serialization
// I assumed that proof and counterparty_consensus_state are encoded with JsonMarshaller.
// It's likely that amino / protobuf binary encoding will be used...
//
// ### Vocabulary (hint for the reader)
// CeloLC on CosmosNetwork:
// * proof - proof that CosmosConsensusState is stored on the TendermintLC in CeloBlockchain
// * counterparty_consensus_state - CosmosConsensusState
//
// Tendermint LC on Celo Blockchain:
// * proof - proof that CeloConsensusState is stored on CeloLC in CosmosNetwork
// * counterparty_consensus_state - CeloConsensusState
pub(crate) fn init(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: HandleMsg,
) -> Result<InitResponse, StdError> {
// The 10-wasm Init method is split into two calls, where the second (via handle())
// call expects ClientState included in the return.
//
// Therefore it's better to execute whole logic in the second call.
Ok(InitResponse::default())
}
pub(crate) fn handle(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: HandleMsg,
) -> Result<HandleResponse, StdError> {
match msg {
HandleMsg::InitializeState {
consensus_state,
me,
} => init_contract(deps, env, info, consensus_state, me),
HandleMsg::CheckHeaderAndUpdateState {
header,
consensus_state,
me,
} => check_header_and_update_state(deps, env, me, consensus_state, header),
HandleMsg::CheckMisbehaviourAndUpdateState {
me,
misbehaviour,
consensus_state_1,
consensus_state_2,
} => check_misbehaviour(
deps,
env,
me,
misbehaviour,
consensus_state_1,
consensus_state_2,
),
HandleMsg::VerifyUpgradeAndUpdateState {
me,
new_client_state,
new_consensus_state,
client_upgrade_proof,
consensus_state_upgrade_proof,
last_height_consensus_state,
} => verify_upgrade_and_update_state(
deps,
env,
me,
new_client_state,
new_consensus_state,
client_upgrade_proof,
consensus_state_upgrade_proof,
last_height_consensus_state,
),
HandleMsg::CheckSubstituteAndUpdateState {
me,
substitute_client_state,
subject_consensus_state,
initial_height,
} => check_substitute_client_state(
deps,
env,
me,
substitute_client_state,
subject_consensus_state,
initial_height,
),
HandleMsg::ZeroCustomFields {
me,
} => zero_custom_fields(
deps,
env,
me,
),
}
}
pub(crate) fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::VerifyClientState {
me,
height,
commitment_prefix,
counterparty_client_identifier,
proof,
counterparty_client_state,
consensus_state,
} => verify_client_state(
deps,
env,
me,
height,
commitment_prefix,
counterparty_client_identifier,
proof,
counterparty_client_state,
consensus_state,
).map(to_binary),
QueryMsg::VerifyClientConsensusState {
me,
height,
consensus_height,
commitment_prefix,
counterparty_client_identifier,
proof,
counterparty_consensus_state,
consensus_state,
} => verify_client_consensus_state(
deps,
env,
me,
height,
consensus_height,
commitment_prefix,
counterparty_client_identifier,
proof,
counterparty_consensus_state,
consensus_state,
).map(to_binary),
QueryMsg::VerifyConnectionState {
me,
height,
commitment_prefix,
proof,
connection_id,
connection_end,
consensus_state,
} => verify_connection_state(
deps,
env,
me,
height,
commitment_prefix,
proof,
connection_id,
connection_end,
consensus_state,
).map(to_binary),
QueryMsg::VerifyChannelState {
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
channel,
consensus_state,
} => verify_channel_state(
deps,
env,
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
channel,
consensus_state,
).map(to_binary),
QueryMsg::VerifyPacketCommitment {
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
sequence,
commitment_bytes,
consensus_state,
} => verify_packet_commitment(
deps,
env,
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
sequence,
commitment_bytes,
consensus_state,
).map(to_binary),
QueryMsg::VerifyPacketAcknowledgement {
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
sequence,
acknowledgement,
consensus_state,
} => verify_packet_acknowledgment(
deps,
env,
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
sequence,
acknowledgement,
consensus_state,
).map(to_binary),
QueryMsg::VerifyPacketReceiptAbsence {
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
sequence,
consensus_state,
} => verify_packet_receipt_absence(
deps,
env,
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
sequence,
consensus_state,
).map(to_binary),
QueryMsg::VerifyNextSequenceRecv {
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
next_sequence_recv,
consensus_state,
} => verify_next_sequence_recv(
deps,
env,
me,
height,
commitment_prefix,
proof,
port_id,
channel_id,
delay_time_period,
delay_block_period,
next_sequence_recv,
consensus_state,
).map(to_binary),
QueryMsg::ProcessedTime { height } => {
let processed_time = get_processed_time(deps.storage, EMPTY_PREFIX, &height)?;
Ok(cosmwasm_std::to_binary(&ProcessedTimeResponse {
time: processed_time,
})?)
},
QueryMsg::Status {
me,
consensus_state,
} => status(deps, env, me, consensus_state).map(to_binary),
}
}
fn init_contract(
deps: DepsMut,
env: Env,
_info: MessageInfo,
consensus_state: ConsensusState,
me: ClientState,
) -> Result<HandleResponse, StdError> {
// Unmarshal initial state entry (ie. validator set, epoch_size etc.)
let light_consensus_state: LightConsensusState =
from_base64_rlp(&consensus_state.data, "msg.initial_state_entry")?;
// Verify initial state
match light_consensus_state.verify() {
Err(e) => {
return Err(StdError::generic_err(format!(
"Initial state verification failed. Error: {}",
e
)))
}
_ => {}
}
// Set metadata for initial consensus state
set_consensus_meta(&env, deps.storage, EMPTY_PREFIX, &me.latest_height.unwrap())?;
// Update the state
let response_data = Binary(to_vec(&InitializeStateResult {
me,
result: ClientStateCallResponseResult::success(),
})?);
Ok(HandleResponse {
messages: vec![],
attributes: vec![
attr("action", "init_block"),
attr("last_consensus_state_height", light_consensus_state.number),
],
data: Some(response_data),
})
}
fn check_header_and_update_state(
deps: DepsMut,
env: Env,
me: ClientState,
consensus_state: ConsensusState,
wasm_header: WasmHeader,
) -> Result<HandleResponse, StdError> {
let current_timestamp: u64 = env.block.time;
// Unmarshal header
let header: Header = from_base64_rlp(&wasm_header.data, "msg.header")?;
// Unmarshal state entry
let light_consensus_state: LightConsensusState =
from_base64_rlp(&consensus_state.data, "msg.light_consensus_state")?;
// Unmarshal state config
let light_client_state: LightClientState = from_base64_rlp(&me.data, "msg.light_client_state")?;
// Ingest new header
let mut state: State = State::new(light_consensus_state, &light_client_state);
match state.insert_header(&header, current_timestamp) {
Err(e) => {
return Err(StdError::generic_err(format!(
"Unable to ingest header. Error: {}",
e
)))
}
_ => {}
}
// Update the state
let new_client_state = me.clone();
let new_consensus_state = ConsensusState {
code_id: consensus_state.code_id,
data: base64::encode(state.snapshot().to_rlp().as_slice()),
timestamp: header.time,
root: MerkleRoot {
hash: base64::encode(header.root.to_vec().as_slice()),
},
};
// set metadata for this consensus state
set_consensus_meta(&env, deps.storage, EMPTY_PREFIX, &wasm_header.height)?;
let response_data = Binary(to_vec(&CheckHeaderAndUpdateStateResult {
new_client_state,
new_consensus_state,
result: ClientStateCallResponseResult::success(),
})?);
Ok(HandleResponse {
messages: vec![],
attributes: vec![
attr("action", "update_block"),
attr("last_consensus_state_height", state.snapshot().number),
],
data: Some(response_data),
})
}
pub fn verify_upgrade_and_update_state(
deps: DepsMut,
env: Env,
me: ClientState,
new_client_state: ClientState,
new_consensus_state: ConsensusState,
client_upgrade_proof: String,
consensus_state_upgrade_proof: String,
last_height_consensus_state: ConsensusState,
) -> Result<HandleResponse, StdError> {
let specs = vec![ics23::iavl_spec(), ics23::tendermint_spec()];
// Sanity check
if !(new_client_state.latest_height > me.latest_height) {
return Err(StdError::generic_err(format!(
"upgraded client height {:?} must be at greater than current client height {:?}",
new_client_state.latest_height, me.latest_height
)));
}
// Unmarshal proofs
let proof_client: MerkleProof =
from_base64_json_slice(&client_upgrade_proof, "msg.client_proof")?;
let proof_consensus: MerkleProof =
from_base64_json_slice(&consensus_state_upgrade_proof, "msg.consensus_proof")?;
// Unmarshal root
let root: Vec<u8> = from_base64(
&last_height_consensus_state.root.hash,
"msg.last_height_consensus_state.root",
)?;
// Check consensus state expiration
let current_timestamp: u64 = env.block.time;
let light_client_state: LightClientState = from_base64_rlp(&me.data, "msg.light_client_state")?;
if is_expired(
current_timestamp,
last_height_consensus_state.timestamp,
&light_client_state,
) {
return Err(StdError::generic_err("cannot upgrade an expired client"));
}
// Verify client proof
let value: Vec<u8> = to_vec(&new_client_state)?;
let upgrade_client_path = construct_upgrade_merkle_path(
&light_client_state.upgrade_path,
ClientUpgradePath::UpgradedClientState(me.latest_height.unwrap().revision_number),
);
if !verify_membership(
&proof_consensus,
&specs,
&root,
&upgrade_client_path,
value,
0,
)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));
}
// Verify consensus proof
let value: Vec<u8> = to_vec(&new_consensus_state)?;
let upgrade_consensus_state_path = construct_upgrade_merkle_path(
&light_client_state.upgrade_path,
ClientUpgradePath::UpgradedClientConsensusState(me.latest_height.unwrap().revision_number),
);
if !verify_membership(
&proof_client,
&specs,
&root,
&upgrade_consensus_state_path,
value,
0,
)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));
}
// set metadata for this consensus state
set_consensus_meta(
&env,
deps.storage,
EMPTY_PREFIX,
&new_client_state.latest_height.unwrap(),
)?;
// Build up the response
wrap_response(
&VerifyUpgradeAndUpdateStateResult {
result: ClientStateCallResponseResult::success(),
// NOTE: The contents of client or consensus state
// are subject to change (once we have end-to-end test flow)
new_client_state,
new_consensus_state,
},
"verify_client_state",
)
}
pub fn check_misbehaviour(
_deps: DepsMut,
_env: Env,
me: ClientState,
misbehaviour: Misbehaviour,
consensus_state1: ConsensusState,
consensus_state2: ConsensusState,
) -> Result<HandleResponse, StdError> {
// The header heights are expected to be the same
if misbehaviour.header_1.height != misbehaviour.header_2.height {
return Err(StdError::generic_err(format!(
"Misbehaviour header heights differ, {} != {}",
misbehaviour.header_1.height, misbehaviour.header_2.height
)));
}
// If client is already frozen at earlier height than misbehaviour, return with error
if me.frozen
&& me.frozen_height.is_some()
&& me.frozen_height.unwrap() <= misbehaviour.header_1.height
{
return Err(StdError::generic_err(format!(
"Client is already frozen at earlier height {} than misbehaviour height {}",
me.frozen_height.unwrap(),
misbehaviour.header_1.height
)));
}
// Unmarshal header
let header_1: Header = from_base64_rlp(&misbehaviour.header_1.data, "msg.header")?;
let header_2: Header = from_base64_rlp(&misbehaviour.header_2.data, "msg.header")?;
// The header state root should differ
if header_1.root == header_2.root {
return Err(StdError::generic_err(
"Header's state roots should differ, but are the same",
));
}
// Check the validity of the two conflicting headers against their respective
// trusted consensus states
check_misbehaviour_header(1, &me, &consensus_state1, &header_1)?;
check_misbehaviour_header(2, &me, &consensus_state2, &header_2)?;
// Store the new state
let mut new_client_state = me.clone();
new_client_state.frozen = true;
new_client_state.frozen_height = Some(misbehaviour.header_1.height);
let response_data = Binary(to_vec(&CheckMisbehaviourAndUpdateStateResult {
new_client_state,
result: ClientStateCallResponseResult::success(),
})?);
Ok(HandleResponse {
messages: vec![],
attributes: vec![
attr("action", "verify_misbehaviour"),
attr("height", misbehaviour.header_1.height),
],
data: Some(response_data),
})
}
// zero_custom_fields returns a ClientState that is a copy of the current ClientState
// with all client customizable fields zeroed out
pub fn zero_custom_fields(
_deps: DepsMut,
_env: Env,
me: ClientState,
) -> Result<HandleResponse, StdError> {
let new_client_state = ClientState {
code_id: me.code_id,
frozen: false,
frozen_height: None,
latest_height: None,
// No custom fields in light client state
data: me.data.clone(),
};
// Build up the response
wrap_response(
&ZeroCustomFieldsResult {
me: new_client_state,
},
"zero_custom_fields",
)
}
pub fn check_misbehaviour_header(
num: u16,
me: &ClientState,
consensus_state: &ConsensusState,
header: &Header,
) -> Result<(), StdError> {
// Unmarshal state entry
let light_consensus_state: LightConsensusState =
from_base64_rlp(&consensus_state.data, "msg.light_consensus_state")?;
// Unmarshal state config
let light_client_state: LightClientState = from_base64_rlp(&me.data, "msg.light_client_state")?;
// Verify header
let state: State = State::new(light_consensus_state, &light_client_state);
match state.verify_header_seal(&header) {
Err(e) => {
return Err(StdError::generic_err(format!(
"Failed to verify header num: {} against it's consensus state. Error: {}",
num, e
)))
}
_ => return Ok(()),
}
}
pub fn verify_client_state(
_deps: Deps,
_env: Env,
_me: ClientState,
_height: Height,
commitment_prefix: MerklePrefix,
counterparty_client_identifier: String,
proof: String,
counterparty_client_state: CosmosClientState,
proving_consensus_state: ConsensusState,
) -> Result<HandleResponse, StdError> {
// Unmarshal proof
let proof: MerkleProof = from_base64_json_slice(&proof, "msg.proof")?;
let specs = vec![ics23::iavl_spec(), ics23::tendermint_spec()];
// Get root from proving (celo) consensus state
let root: Vec<u8> = from_base64(
&proving_consensus_state.root.hash,
"msg.proving_consensus_state.root",
)?;
// Build path (proof is used to validate the existance of value under that path)
let client_prefixed_path = IcsPath::ClientState(
ClientId::from_str(&counterparty_client_identifier).map_err(to_generic_err)?,
)
.to_string();
// Verify proof against key-value pair
let path = apply_prefix(&commitment_prefix, vec![client_prefixed_path])?;
let value: Vec<u8> = to_vec(&counterparty_client_state)?;
if !verify_membership(&proof, &specs, &root, &path, value, 0)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));
}
// Build up the response
wrap_response(
&VerifyClientStateResult {
result: ClientStateCallResponseResult::success(),
},
"verify_client_state",
)
}
pub fn verify_client_consensus_state(
_deps: Deps,
_env: Env,
_me: ClientState,
_height: Height,
consensus_height: Height,
commitment_prefix: MerklePrefix,
counterparty_client_identifier: String,
proof: String,
counterparty_consensus_state: CosmosConsensusState,
proving_consensus_state: ConsensusState,
) -> Result<HandleResponse, StdError> {
// Unmarshal proof
let proof: MerkleProof = from_base64_json_slice(&proof, "msg.proof")?;
let specs = vec![ics23::iavl_spec(), ics23::tendermint_spec()];
// Get root from proving (celo) consensus state
let root: Vec<u8> = from_base64(
&proving_consensus_state.root.hash,
"msg.proving_consensus_state.root",
)?;
// Build path (proof is used to validate the existance of value under that path)
let client_prefixed_path = IcsPath::ClientConsensusState {
client_id: ClientId::from_str(&counterparty_client_identifier).map_err(to_generic_err)?,
epoch: consensus_height.revision_number,
height: consensus_height.revision_height,
}
.to_string();
// Verify proof against key-value pair
let path = apply_prefix(&commitment_prefix, vec![client_prefixed_path])?;
let value: Vec<u8> = to_vec(&counterparty_consensus_state)?;
if !verify_membership(&proof, &specs, &root, &path, value, 0)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));
}
// Build up the response
wrap_response(
&VerifyClientConsensusStateResult {
result: ClientStateCallResponseResult::success(),
},
"verify_client_state",
)
}
pub fn verify_connection_state(
_deps: Deps,
_env: Env,
_me: ClientState,
_height: Height,
commitment_prefix: MerklePrefix,
proof: String,
connection_id: String,
connection_end: ConnectionEnd,
consensus_state: ConsensusState,
) -> Result<HandleResponse, StdError> {
// Unmarshal proof
let proof: MerkleProof = from_base64_json_slice(&proof, "msg.proof")?;
let specs = vec![ics23::iavl_spec(), ics23::tendermint_spec()];
// Get root from proving (celo) consensus state
let root: Vec<u8> = from_base64(
&consensus_state.root.hash,
"msg.proving_consensus_state.root",
)?;
// Build path (proof is used to validate the existance of value under that path)
let connection_path =
IcsPath::Connections(ConnectionId::from_str(&connection_id).map_err(to_generic_err)?)
.to_string();
// Verify proof against key-value pair
let path = apply_prefix(&commitment_prefix, vec![connection_path])?;
let value: Vec<u8> = to_vec(&connection_end)?;
if !verify_membership(&proof, &specs, &root, &path, value, 0)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));
}
// Build up the response
wrap_response(
&VerifyConnectionStateResult {
result: ClientStateCallResponseResult::success(),
},
"verify_connection_state",
)
}
pub fn verify_channel_state(
_deps: Deps,
_env: Env,
_me: ClientState,
_height: Height,
commitment_prefix: MerklePrefix,
proof: String,
port_id: String,
channel_id: String,
channel: Channel,
consensus_state: ConsensusState,
) -> Result<HandleResponse, StdError> {
// Unmarshal proof
let proof: MerkleProof = from_base64_json_slice(&proof, "msg.proof")?;
let specs = vec![ics23::iavl_spec(), ics23::tendermint_spec()];
// Get root from proving (celo) consensus state
let root: Vec<u8> = from_base64(
&consensus_state.root.hash,
"msg.proving_consensus_state.root",
)?;
// Build path (proof is used to validate the existance of value under that path)
let channel_path = IcsPath::ChannelEnds(
PortId::from_str(&port_id).map_err(to_generic_err)?,
ChannelId::from_str(&channel_id).map_err(to_generic_err)?,
)
.to_string();
// Verify proof against key-value pair
let path = apply_prefix(&commitment_prefix, vec![channel_path])?;
let value: Vec<u8> = to_vec(&channel)?;
if !verify_membership(&proof, &specs, &root, &path, value, 0)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));
}
// Build up the response
wrap_response(
&VerifyChannelStateResult {
result: ClientStateCallResponseResult::success(),
},
"verify_channel_state",
)
}
pub fn verify_packet_commitment(
deps: Deps,
env: Env,
_me: ClientState,
height: Height,
commitment_prefix: MerklePrefix,
proof: String,
port_id: String,
channel_id: String,
delay_time_period: u64,
delay_block_period: u64,
sequence: u64,
commitment_bytes: String,
consensus_state: ConsensusState,
) -> Result<HandleResponse, StdError> {
// Unmarshal proof
let proof: MerkleProof = from_base64_json_slice(&proof, "msg.proof")?;
let specs = vec![ics23::iavl_spec(), ics23::tendermint_spec()];
// Get root from proving (celo) consensus state
let root: Vec<u8> = from_base64(&consensus_state.root.hash, "msg.consensus_state.root")?;
// Check delay period has passed
verify_delay_period_passed(
deps,
height,
env.block.height,
env.block.time,
delay_time_period,
delay_block_period,
)?;
// Build path (proof is used to validate the existance of value under that path)
let commitment_path = IcsPath::Commitments {
port_id: PortId::from_str(&port_id).map_err(to_generic_err)?,
channel_id: ChannelId::from_str(&channel_id).map_err(to_generic_err)?,
sequence: Sequence::from(sequence),
}
.to_string();
// Verify proof against key-value pair
let path = apply_prefix(&commitment_prefix, vec![commitment_path])?;
let value: Vec<u8> = from_base64(&commitment_bytes, "msg.commitment_bytes")?;
if !verify_membership(&proof, &specs, &root, &path, value, 0)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));
}
// Build up the response
wrap_response(
&VerifyPacketCommitmentResult {
result: ClientStateCallResponseResult::success(),
},
"verify_packet_commitment",
)
}
pub fn verify_packet_acknowledgment(
deps: Deps,
env: Env,
_me: ClientState,
height: Height,
commitment_prefix: MerklePrefix,
proof: String,
port_id: String,
channel_id: String,
delay_time_period: u64,
delay_block_period: u64,
sequence: u64,
acknowledgement: String,
consensus_state: ConsensusState,
) -> Result<HandleResponse, StdError> {
// Unmarshal proof
let proof: MerkleProof = from_base64_json_slice(&proof, "msg.proof")?;
let specs = vec![ics23::iavl_spec(), ics23::tendermint_spec()];
// Get root from proving (celo) consensus state
let root: Vec<u8> = from_base64(&consensus_state.root.hash, "msg.consensus_state.root")?;
// Check delay period has passed
verify_delay_period_passed(
deps,
height,
env.block.height,
env.block.time,
delay_time_period,
delay_block_period,
)?;
// Build path (proof is used to validate the existance of value under that path)
let ack_path = IcsPath::Acks {
port_id: PortId::from_str(&port_id).map_err(to_generic_err)?,
channel_id: ChannelId::from_str(&channel_id).map_err(to_generic_err)?,
sequence: Sequence::from(sequence),
}
.to_string();
// Verify proof against key-value pair
let path = apply_prefix(&commitment_prefix, vec![ack_path])?;
let value: Vec<u8> = from_base64(&acknowledgement, "msg.acknowledgement")?;
if !verify_membership(&proof, &specs, &root, &path, value, 0)? {
return Err(StdError::generic_err(
"proof membership verification failed (invalid proof)",
));