Skip to content

Commit

Permalink
add index block hash
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Oct 25, 2024
1 parent 0111edf commit 0b35f3a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
26 changes: 16 additions & 10 deletions components/chainhook-cli/src/storage/signers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub fn initialize_signers_db(base_dir: &PathBuf, ctx: &Context) -> Result<Connec
pox_consensus TEXT NOT NULL,
server_version TEXT NOT NULL,
network_id INTEGER NOT NULL,
index_block_hash TEXT NOT NULL,
UNIQUE(message_id),
FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
)",
Expand Down Expand Up @@ -166,7 +167,7 @@ fn store_mock_proposal_peer_info(
.prepare(
"INSERT INTO mock_proposals
(message_id, burn_block_height, stacks_tip_consensus_hash, stacks_tip, stacks_tip_height, pox_consensus,
server_version, network_id)
server_version, network_id, index_block_hash)
VALUES (?,?,?,?,?,?,?,?)
RETURNING id",
)
Expand All @@ -181,6 +182,7 @@ fn store_mock_proposal_peer_info(
&peer_info.pox_consensus,
&peer_info.server_version,
&peer_info.network_id,
&peer_info.index_block_hash,
])
.map_err(|e| format!("unable to write mock proposal: {e}"))?
.next()
Expand Down Expand Up @@ -667,11 +669,12 @@ pub fn get_signer_db_messages_received_at_block(
stacks_tip_height: signature_row.get(3).unwrap(),
pox_consensus: signature_row.get(4).unwrap(),
server_version: signature_row.get(5).unwrap(),
network_id: signature_row.get(6).unwrap()
network_id: signature_row.get(6).unwrap(),
index_block_hash: signature_row.get(7).unwrap(),
}
},
metadata: SignerMessageMetadata {
server_version: signature_row.get(7).unwrap()
server_version: signature_row.get(8).unwrap()
}
}))
},
Expand All @@ -680,7 +683,7 @@ pub fn get_signer_db_messages_received_at_block(
"mock_proposal" => db_tx
.query_row(
"SELECT burn_block_height, stacks_tip_consensus_hash, stacks_tip, stacks_tip_height,
pox_consensus, server_version, network_id
pox_consensus, server_version, network_id, index_block_hash
FROM mock_proposals
WHERE message_id = ?",
rusqlite::params![&message_id],
Expand All @@ -692,15 +695,16 @@ pub fn get_signer_db_messages_received_at_block(
stacks_tip_height: proposal_row.get(3).unwrap(),
pox_consensus: proposal_row.get(4).unwrap(),
server_version: proposal_row.get(5).unwrap(),
network_id: proposal_row.get(6).unwrap()
network_id: proposal_row.get(6).unwrap(),
index_block_hash: proposal_row.get(7).unwrap(),
}))
},
)
.map_err(|e| format!("unable to query mock proposal: {e}"))?,
"mock_block" => db_tx
.query_row(
"SELECT b.id, p.burn_block_height, p.stacks_tip_consensus_hash, p.stacks_tip, p.stacks_tip_height,
p.pox_consensus, p.server_version, p.network_id
p.pox_consensus, p.server_version, p.network_id, p.index_block_hash
FROM mock_blocks AS b
INNER JOIN mock_proposals AS p ON p.id = b.mock_proposal_id
WHERE b.message_id = ?",
Expand All @@ -711,7 +715,7 @@ pub fn get_signer_db_messages_received_at_block(
.prepare(
"SELECT p.burn_block_height, p.stacks_tip_consensus_hash, p.stacks_tip,
p.stacks_tip_height, p.pox_consensus, p.server_version AS peer_version,
p.network_id, s.server_version
p.network_id, p.index_block_hash, s.server_version
FROM mock_signatures AS s
INNER JOIN mock_proposals AS p ON p.id = s.mock_proposal_id
WHERE s.mock_block_id = ?")?;
Expand All @@ -727,11 +731,12 @@ pub fn get_signer_db_messages_received_at_block(
stacks_tip_height: signature_row.get(3).unwrap(),
pox_consensus: signature_row.get(4).unwrap(),
server_version: signature_row.get(5).unwrap(),
network_id: signature_row.get(6).unwrap()
network_id: signature_row.get(6).unwrap(),
index_block_hash: signature_row.get(7).unwrap(),
}
},
metadata: SignerMessageMetadata {
server_version: signature_row.get(7).unwrap()
server_version: signature_row.get(8).unwrap()
}
});
}
Expand All @@ -744,7 +749,8 @@ pub fn get_signer_db_messages_received_at_block(
stacks_tip_height: block_row.get(4).unwrap(),
pox_consensus: block_row.get(5).unwrap(),
server_version: block_row.get(6).unwrap(),
network_id: block_row.get(7).unwrap()
network_id: block_row.get(7).unwrap(),
index_block_hash: block_row.get(8).unwrap(),
}
},
mock_signatures
Expand Down
41 changes: 25 additions & 16 deletions components/chainhook-sdk/src/indexer/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,21 +788,27 @@ pub fn standardize_stacks_stackerdb_chunks(
block: standardize_stacks_nakamoto_block(&nakamoto_block)?,
})
}
SignerMessage::MockSignature(signature) => {
StacksSignerMessage::MockSignature(standardize_stacks_signer_mock_signature(&signature))
}
SignerMessage::MockSignature(signature) => StacksSignerMessage::MockSignature(
standardize_stacks_signer_mock_signature(&signature)?,
),
SignerMessage::MockProposal(data) => StacksSignerMessage::MockProposal(
standardize_stacks_signer_peer_info(&data.peer_info),
standardize_stacks_signer_peer_info(&data.peer_info)?,
),
SignerMessage::MockBlock(data) => StacksSignerMessage::MockBlock(MockBlockData {
mock_proposal: MockProposalData {
peer_info: standardize_stacks_signer_peer_info(&data.mock_proposal.peer_info),
peer_info: standardize_stacks_signer_peer_info(&data.mock_proposal.peer_info)?,
},
mock_signatures: data
.mock_signatures
.iter()
.map(|signature| standardize_stacks_signer_mock_signature(signature))
.collect(),
.try_fold(Vec::new(), |mut acc, item| -> Result<Vec<MockSignatureData>, String> {
item.and_then(|val| {
acc.push(val);
Ok(())
})?;
Ok(acc)
})?,
}),
};
parsed_chunks.push(StacksStackerDbChunk {
Expand All @@ -822,32 +828,35 @@ pub fn standardize_stacks_stackerdb_chunks(
#[cfg(feature = "stacks-signers")]
pub fn standardize_stacks_signer_mock_signature(
signature: &stacks_codec::codec::MockSignature,
) -> MockSignatureData {
MockSignatureData {
) -> Result<MockSignatureData, String> {
Ok(MockSignatureData {
mock_proposal: MockProposalData {
peer_info: standardize_stacks_signer_peer_info(
&signature.mock_proposal.peer_info,
),
peer_info: standardize_stacks_signer_peer_info(&signature.mock_proposal.peer_info)?,
},
metadata: SignerMessageMetadata {
server_version: signature.metadata.server_version.clone(),
},
}
})
}

#[cfg(feature = "stacks-signers")]
pub fn standardize_stacks_signer_peer_info(
peer_info: &stacks_codec::codec::PeerInfo,
) -> PeerInfoData {
PeerInfoData {
) -> Result<PeerInfoData, String> {
let block_hash = format!("0x{}", peer_info.stacks_tip.to_hex());
Ok(PeerInfoData {
burn_block_height: peer_info.burn_block_height,
stacks_tip_consensus_hash: format!("0x{}", peer_info.stacks_tip_consensus_hash.to_hex()),
stacks_tip: format!("0x{}", peer_info.stacks_tip.to_hex()),
stacks_tip: block_hash.clone(),
stacks_tip_height: peer_info.stacks_tip_height,
pox_consensus: format!("0x{}", peer_info.pox_consensus.to_hex()),
server_version: peer_info.server_version.clone(),
network_id: peer_info.network_id,
}
index_block_hash: get_nakamoto_index_block_hash(
&block_hash,
&peer_info.stacks_tip_consensus_hash,
)?,
})
}

#[cfg(feature = "stacks-signers")]
Expand Down
1 change: 1 addition & 0 deletions components/chainhook-types-rs/src/signers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct PeerInfoData {
pub pox_consensus: String,
pub server_version: String,
pub network_id: u32,
pub index_block_hash: String,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions components/client/typescript/src/schemas/stacks/signers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const StacksSignerMessagePeerInfoSchema = Type.Object({
pox_consensus: Type.String(),
server_version: Type.String(),
network_id: Type.Integer(),
index_block_hash: Type.String(),
});
export type StacksSignerMessagePeerInfo = Static<typeof StacksSignerMessagePeerInfoSchema>;

Expand Down

0 comments on commit 0b35f3a

Please sign in to comment.