Skip to content

Commit

Permalink
feat(check-misbehavior): implement hash function for header comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
dale-smartosc committed May 9, 2024
1 parent b50cf5c commit f8ff6be
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
use aiken/bytearray
use aiken/builtin.{if_then_else as ite}
use aiken/bytearray.{concat, length, push}
use aiken/hash.{Hash, Sha3_256}
use ibc/client/ics_007_tendermint_client/cometbft/block/block_id.{BlockID} as block_id_mod
use ibc/client/ics_007_tendermint_client/cometbft/canonical.{
canonicalize_block_id,
}
use ibc/client/ics_007_tendermint_client/cometbft/constants.{
max_chain_id_len, tm_hash_size,
}
use ibc/client/ics_007_tendermint_client/cometbft/crypto/merkle/tree.{
hash_from_byte_slices_sha2_256,
}
use ibc/client/ics_007_tendermint_client/cometbft/protos/canonical_pb.{
marshal_for_block_id,
}
use ibc/client/ics_007_tendermint_client/cometbft/protos/timestamp_pb.{
marshal_for_timestamp, timestamp_proto,
}
use ibc/client/ics_007_tendermint_client/cometbft/protos/types_pb.{
Consensus, marshal_for_consensus,
}
use ibc/client/ics_007_tendermint_client/cometbft/validator_set.{ValidatorSet}
use ibc/client/ics_007_tendermint_client/cometbft/version/consensus.{
Consensus, block_protocol,
block_protocol,
}
use ibc/utils/bytes.{encode_varint}
use ibc/utils/int.{uint64}

pub type TmHeader {
// basic block info
Expand Down Expand Up @@ -84,9 +102,149 @@ pub fn null_tm_header() -> TmHeader {
}
}

pub fn hash(_h: TmHeader) -> ByteArray {
// TODO: Add hashing logic
""
pub fn hash(h: TmHeader) -> ByteArray {
ite(
length(h.validators_hash) == 0,
#[],
{
let TmHeader {
version,
chain_id,
height,
time,
last_block_id,
last_commit_hash,
data_hash,
validators_hash,
next_validators_hash,
consensus_hash,
app_hash,
last_results_hash,
evidence_hash,
proposer_address,
} = h
let hbz = version |> Some() |> marshal_for_consensus()
let pbt = time |> timestamp_proto() |> marshal_for_timestamp()
let bzbi =
last_block_id
|> canonicalize_block_id()
|> Some()
|> marshal_for_block_id()

hash_from_byte_slices_sha2_256(
[
hbz,
ite(
length(chain_id) == 0,
#[],
chain_id
|> length()
|> uint64()
|> encode_varint()
|> concat(chain_id)
|> push(10),
),
ite(
height == 0,
#[],
height |> uint64() |> encode_varint() |> push(8),
),
pbt,
bzbi,
ite(
length(last_commit_hash) == 0,
#[],
last_commit_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(last_commit_hash)
|> push(10),
),
ite(
length(data_hash) == 0,
#[],
data_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(data_hash)
|> push(10),
),
ite(
length(validators_hash) == 0,
#[],
validators_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(validators_hash)
|> push(10),
),
ite(
length(next_validators_hash) == 0,
#[],
next_validators_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(next_validators_hash)
|> push(10),
),
ite(
length(consensus_hash) == 0,
#[],
consensus_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(consensus_hash)
|> push(10),
),
ite(
length(app_hash) == 0,
#[],
app_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(app_hash)
|> push(10),
),
ite(
length(last_results_hash) == 0,
#[],
last_results_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(last_results_hash)
|> push(10),
),
ite(
length(evidence_hash) == 0,
#[],
evidence_hash
|> length()
|> uint64()
|> encode_varint()
|> concat(evidence_hash)
|> push(10),
),
ite(
length(proposer_address) == 0,
#[],
proposer_address
|> length()
|> uint64()
|> encode_varint()
|> concat(proposer_address)
|> push(10),
),
],
)
},
)
}

fn validate_hash(h: ByteArray) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use aiken/builtin.{if_then_else as ite}
use aiken/bytearray.{concat, push}
use aiken/option.{is_none}
use ibc/utils/bytes.{encode_varint}
use ibc/utils/int.{uint64}

pub type Consensus {
block: Int,
app: Int,
}

pub fn marshal_for_consensus(consensus_opt: Option<Consensus>) -> ByteArray {
ite(
is_none(consensus_opt),
#[],
{
expect Some(consensus) = consensus_opt
let Consensus { block, app } = consensus
#[]
|> concat(
ite(
block == 0,
#[],
block |> uint64() |> encode_varint() |> push(8),
),
)
|> concat(
ite(app == 0, #[], app |> uint64() |> encode_varint() |> push(0x10)),
)
},
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
pub const block_protocol = 11

pub type Consensus {
block: Int,
app: Int,
}
3 changes: 2 additions & 1 deletion cardano/validators/spending_client.ak
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ use ibc/client/ics_007_tendermint_client/cometbft/block/block_id.{
use ibc/client/ics_007_tendermint_client/cometbft/block/commit.{Commit}
use ibc/client/ics_007_tendermint_client/cometbft/block/commit_sig.{CommitSig}
use ibc/client/ics_007_tendermint_client/cometbft/block/header.{TmHeader} as tm_header_mod
use ibc/client/ics_007_tendermint_client/cometbft/protos/types_pb.{Consensus}
use ibc/client/ics_007_tendermint_client/cometbft/signed_header.{SignedHeader}
use ibc/client/ics_007_tendermint_client/cometbft/tm_validator.{Validator}
use ibc/client/ics_007_tendermint_client/cometbft/validator_set.{ValidatorSet}
use ibc/client/ics_007_tendermint_client/cometbft/version/consensus.{
Consensus, block_protocol,
block_protocol,
}
use ibc/client/ics_007_tendermint_client/consensus_state.{ConsensusState}
use ibc/client/ics_007_tendermint_client/header.{Header} as header_mod
Expand Down

0 comments on commit f8ff6be

Please sign in to comment.