Skip to content

Commit

Permalink
fix: add validating tendermint header hash
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhbx-smartosc committed May 8, 2024
1 parent 9ed88b9 commit 4565565
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 149 deletions.
151 changes: 76 additions & 75 deletions cardano/lib/ibc/client/ics-007-tendermint-client/client_datum_test.ak
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use ibc/client/ics_007_tendermint_client/client_datum.{
ClientDatum, ClientDatumState,
}
use ibc/client/ics_007_tendermint_client/client_state.{ClientState}
use ibc/client/ics_007_tendermint_client/cometbft/block/commit.{
BlockID, Commit, PartSetHeader,
use ibc/client/ics_007_tendermint_client/cometbft/block/block_id.{
BlockID, PartSetHeader,
}
use ibc/client/ics_007_tendermint_client/cometbft/block/commit.{Commit}
use ibc/client/ics_007_tendermint_client/cometbft/block/header as tm_header
use ibc/client/ics_007_tendermint_client/cometbft/signed_header.{SignedHeader}
use ibc/client/ics_007_tendermint_client/cometbft/tm_validator.{Validator}
Expand Down Expand Up @@ -53,7 +54,7 @@ fn get_mock_header(latest_height: Height) -> Header {
Header {
signed_header: SignedHeader {
header: tm_header.TmHeader {
//TODO: must check chain Id for getting Height from header
..tm_header.null_tm_header(),
chain_id: "ogmosis",
height: latest_height.revision_height,
time: expected_time,
Expand Down Expand Up @@ -109,14 +110,14 @@ fn get_mock_client_datum(latest_height: Height) -> ClientDatum {
let mock_consensus_states =
dict.new()
|> dict.insert(
latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
let mock_auth_token =
AuthToken {
policy_id: mock_auth_token_policy_id,
Expand Down Expand Up @@ -148,14 +149,14 @@ test test_is_initialized_valid_success() {
let mock_consensus_states =
dict.new()
|> dict.insert(
mock_latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
mock_latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
let mock_auth_token =
AuthToken {
policy_id: mock_auth_token_policy_id,
Expand Down Expand Up @@ -193,14 +194,14 @@ test test_is_initialized_valid_fail_with_invalid_client_state() fail {
let mock_consensus_states =
dict.new()
|> dict.insert(
mock_latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
mock_latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
let mock_auth_token =
AuthToken {
policy_id: mock_auth_token_policy_id,
Expand Down Expand Up @@ -265,14 +266,14 @@ test test_is_initialized_valid_fail_with_invalid_datum_token() fail {
let mock_consensus_states =
dict.new()
|> dict.insert(
mock_latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
mock_latest_height,
ConsensusState {
timestamp: mock_timestamp,
next_validators_hash: mock_next_validators_hash,
root: merkle.new_merkle_root(mock_app_hash),
},
height.compare,
)
let mock_auth_token =
AuthToken {
policy_id: mock_auth_token_policy_id,
Expand Down Expand Up @@ -317,10 +318,10 @@ test test_update_state_succeed_01() {
height.compare,
)
|> dict.insert(
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
|> dict.to_list(),
client_state: ClientState {
..mock_client_datum.state.client_state,
Expand Down Expand Up @@ -358,10 +359,10 @@ test test_update_state_succeed_02() {
height.compare,
)
|> dict.insert(
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
|> dict.to_list(),
},
}
Expand Down Expand Up @@ -398,10 +399,10 @@ test test_update_state_succeed_03() {
height.compare,
)
|> dict.insert(
Height { revision_number: 0, revision_height: 88 },
pruned_cons_state,
height.compare,
)
Height { revision_number: 0, revision_height: 88 },
pruned_cons_state,
height.compare,
)
|> dict.to_list(),
},
}
Expand All @@ -413,19 +414,19 @@ test test_update_state_succeed_03() {
let expected_cons_state =
dict.from_list(mock_client_datum.state.consensus_states, height.compare)
|> dict.filter(
fn(_key, val) {
!client_state.is_expired(
mock_client_datum.state.client_state,
val.timestamp,
tx_valid_from * 1_000_000,
)
},
)
fn(_key, val) {
!client_state.is_expired(
mock_client_datum.state.client_state,
val.timestamp,
tx_valid_from * 1_000_000,
)
},
)
|> dict.insert(
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)

let input_client_datum = mock_client_datum
let output_client_datum =
Expand Down Expand Up @@ -463,10 +464,10 @@ test test_update_state_return_false_01() {
height.compare,
)
|> dict.insert(
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
|> dict.to_list(),
client_state: ClientState {
..mock_client_datum.state.client_state,
Expand Down Expand Up @@ -502,10 +503,10 @@ test test_update_state_return_false_02() {
height.compare,
)
|> dict.insert(
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
|> dict.to_list(),
},
}
Expand Down Expand Up @@ -535,10 +536,10 @@ test test_update_state_fail_when_input_consensus_state_contain_header_height_key
height.compare,
)
|> dict.insert(
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
|> dict.to_list(),
},
}
Expand All @@ -552,10 +553,10 @@ test test_update_state_fail_when_input_consensus_state_contain_header_height_key
height.compare,
)
|> dict.insert(
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
header.get_height(mock_header),
header.consensus_state(mock_header),
height.compare,
)
|> dict.to_list(),
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use aiken/bytearray
use ibc/client/ics_007_tendermint_client/cometbft/constants.{tm_hash_size}

pub type BlockID {
hash: ByteArray,
part_set_header: PartSetHeader,
}

pub type PartSetHeader {
total: Int,
hash: ByteArray,
}

pub fn null_block_id() -> BlockID {
BlockID { hash: "", part_set_header: PartSetHeader { total: 0, hash: "" } }
}

pub fn validate_basic(block_id: BlockID) -> Bool {
expect validate_hash(block_id.hash)

expect validate_hash(block_id.part_set_header.hash)

expect block_id.part_set_header.total >= 0

True
}

fn validate_hash(h: ByteArray) -> Bool {
let len = h |> bytearray.length()
len == 0 || len == tm_hash_size
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use aiken/list
use ibc/client/ics_007_tendermint_client/cometbft/block/block_id.{
BlockID, PartSetHeader,
} as block_id_mod
use ibc/client/ics_007_tendermint_client/cometbft/block/commit_sig.{CommitSig}

pub type Commit {
Expand All @@ -8,16 +11,6 @@ pub type Commit {
signatures: List<CommitSig>,
}

pub type BlockID {
hash: ByteArray,
part_set_header: PartSetHeader,
}

pub type PartSetHeader {
total: Int,
hash: ByteArray,
}

pub fn validate_basic(commit: Commit) -> Bool {
expect commit.height >= 0 && commit.round >= 0
if commit.height >= 1 {
Expand All @@ -34,10 +27,7 @@ pub fn null_commit() -> Commit {
Commit {
height: 0,
round: 0,
block_id: BlockID {
hash: #[],
part_set_header: PartSetHeader { total: 0, hash: #[] },
},
block_id: block_id_mod.null_block_id(),
signatures: [],
}
}
Expand Down

0 comments on commit 4565565

Please sign in to comment.