Skip to content

Commit

Permalink
Header::hash works now (tested against JSON fixture only and yields:
Browse files Browse the repository at this point in the history
 2DC46AD76277039F1B65FE3C7F2064788B1C12FE701CFE7EC93F751586A48781)

will add a test after #42 gets merged
  • Loading branch information
liamsi committed Sep 25, 2019
1 parent 360a37a commit 3217eae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
70 changes: 51 additions & 19 deletions tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ impl lite::Header for Header {
.encode(&mut time_enc)
.unwrap();
let chain_id_bytes = self.chain_id.as_bytes();
let mut chain_id_enc = vec![];
prost_amino::encode_length_delimiter(chain_id_bytes.len(), &mut chain_id_enc).unwrap();
chain_id_enc.append(&mut chain_id_bytes.to_vec());
let chain_id_enc = encode_bytes(&chain_id_bytes);
let mut num_tx_enc = vec![];
prost_amino::encoding::encode_varint(self.num_txs, &mut num_tx_enc);
let mut total_tx_enc = vec![];
Expand All @@ -116,6 +114,40 @@ impl lite::Header for Header {
amino_types::BlockId::from(&self.last_block_id)
.encode(&mut last_block_id_enc)
.unwrap();
let mut last_commit_hash_enc = vec![];
if let Some(last_commit_hash_bytes) = self.last_commit_hash.as_bytes() {
last_commit_hash_enc = encode_bytes(last_commit_hash_bytes);
}
let mut data_hash_enc = vec![];
if let Some(data_hash_bytes) = self.data_hash.as_bytes() {
data_hash_enc = encode_bytes(data_hash_bytes);
}
let mut validator_hash_enc = vec![];
if let Some(validator_hash_bytes) = self.validators_hash.as_bytes() {
validator_hash_enc = encode_bytes(validator_hash_bytes);
}
let mut next_validator_hash_enc = vec![];
if let Some(next_validator_hash_bytes) = self.next_validators_hash.as_bytes() {
next_validator_hash_enc = encode_bytes(next_validator_hash_bytes);
}
let mut consensus_hash_enc = vec![];
if let Some(consensus_hash_bytes) = self.consensus_hash.as_bytes() {
consensus_hash_enc = encode_bytes(consensus_hash_bytes);
}
let mut app_hash_enc = vec![];
if let Some(app_hash_bytes) = self.app_hash.as_bytes() {
app_hash_enc = encode_bytes(app_hash_bytes);
}
let mut last_result_hash_enc = vec![];
if let Some(last_result_hash_bytes) = self.last_results_hash.as_bytes() {
last_result_hash_enc = encode_bytes(last_result_hash_bytes);
}
let mut evidence_hash_enc = vec![];
if let Some(evidence_hash_bytes) = self.evidence_hash.as_bytes() {
evidence_hash_enc = encode_bytes(evidence_hash_bytes);
}
let proposer_address_bytes = self.proposer_address.as_bytes();
let proposer_address_enc = encode_bytes(&proposer_address_bytes);

let mut byteslices: Vec<&[u8]> = vec![];
byteslices.push(version_enc.as_slice());
Expand All @@ -125,23 +157,16 @@ impl lite::Header for Header {
byteslices.push(num_tx_enc.as_slice());
byteslices.push(total_tx_enc.as_slice());
byteslices.push(last_block_id_enc.as_slice());
byteslices.push(last_commit_hash_enc.as_slice());
byteslices.push(data_hash_enc.as_slice());
byteslices.push(validator_hash_enc.as_slice());
byteslices.push(next_validator_hash_enc.as_slice());
byteslices.push(consensus_hash_enc.as_slice());
byteslices.push(app_hash_enc.as_slice());
byteslices.push(last_result_hash_enc.as_slice());
byteslices.push(evidence_hash_enc.as_slice());
byteslices.push(proposer_address_enc.as_slice());

// cdcEncode(h.Version),
// cdcEncode(h.ChainID),
// cdcEncode(h.Height),
// cdcEncode(h.Time),
// cdcEncode(h.NumTxs),
// cdcEncode(h.TotalTxs),
// cdcEncode(h.LastBlockID),
// cdcEncode(h.LastCommitHash),
// cdcEncode(h.DataHash),
// cdcEncode(h.ValidatorsHash),
// cdcEncode(h.NextValidatorsHash),
// cdcEncode(h.ConsensusHash),
// cdcEncode(h.AppHash),
// cdcEncode(h.LastResultsHash),
// cdcEncode(h.EvidenceHash),
// cdcEncode(h.ProposerAddress),
Hash::Sha256(simple_hash_from_byte_slices(byteslices.as_slice()))
}
}
Expand Down Expand Up @@ -169,3 +194,10 @@ pub struct Version {
#[prost(uint64, tag = "2")]
pub app: u64,
}

fn encode_bytes(bytes: &[u8]) -> Vec<u8> {
let mut chain_id_enc = vec![];
prost_amino::encode_length_delimiter(bytes.len(), &mut chain_id_enc).unwrap();
chain_id_enc.append(&mut bytes.to_vec());
chain_id_enc
}
2 changes: 1 addition & 1 deletion tendermint/src/chain/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Id {

/// Get the chain ID as a raw bytes.
pub fn as_bytes(&self) -> &[u8] {
&self.0
&self.as_str().as_bytes()
}
}

Expand Down

0 comments on commit 3217eae

Please sign in to comment.