Skip to content

Commit

Permalink
feat: add mock signature pubkey (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr authored Oct 25, 2024
1 parent b3a156f commit c414d79
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ default-members = ["components/chainhook-cli", "components/chainhook-sdk"]
resolver = "2"

[patch.crates-io]
stacks-codec = { git = "https://github.com/hirosystems/clarinet.git", rev = "fcebfb5a986ded32d5a450c34f8e5e5f2da97de4" }
stacks-codec = { git = "https://github.com/hirosystems/clarinet.git", rev = "3ec638a64171b7557613531f118f302992c99b89" }
42 changes: 22 additions & 20 deletions components/chainhook-cli/src/storage/signers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ pub fn initialize_signers_db(base_dir: &PathBuf, ctx: &Context) -> Result<Connec
message_id INTEGER,
mock_block_id INTEGER,
server_version TEXT NOT NULL,
signature TEXT NOT NULL,
pubkey TEXT NOT NULL,
UNIQUE(message_id),
FOREIGN KEY (mock_proposal_id) REFERENCES mock_proposals(id) ON DELETE CASCADE,
FOREIGN KEY (mock_block_id) REFERENCES mock_blocks(id) ON DELETE CASCADE,
Expand All @@ -168,7 +170,7 @@ fn store_mock_proposal_peer_info(
"INSERT INTO mock_proposals
(message_id, burn_block_height, stacks_tip_consensus_hash, stacks_tip, stacks_tip_height, pox_consensus,
server_version, network_id, index_block_hash)
VALUES (?,?,?,?,?,?,?,?)
VALUES (?,?,?,?,?,?,?,?,?)
RETURNING id",
)
.map_err(|e| format!("unable to prepare statement: {e}"))?;
Expand All @@ -195,25 +197,27 @@ fn store_mock_proposal_peer_info(

fn store_mock_signature(
db_tx: &Transaction<'_>,
peer_info: &PeerInfoData,
metadata: &SignerMessageMetadata,
mock_signature: &MockSignatureData,
message_id: Option<u64>,
mock_block_id: Option<u64>,
) -> Result<(), String> {
let mock_proposal_id = store_mock_proposal_peer_info(&db_tx, &peer_info, None)?;
let mock_proposal_id =
store_mock_proposal_peer_info(&db_tx, &mock_signature.mock_proposal.peer_info, None)?;
let mut signature_stmt = db_tx
.prepare(
"INSERT INTO mock_signatures
(message_id, mock_proposal_id, mock_block_id, server_version)
VALUES (?,?,?,?)",
(message_id, mock_proposal_id, mock_block_id, server_version, signature, pubkey)
VALUES (?,?,?,?,?,?)",
)
.map_err(|e| format!("unable to prepare statement: {e}"))?;
signature_stmt
.execute(rusqlite::params![
&message_id,
&mock_proposal_id,
&mock_block_id,
&metadata.server_version,
&mock_signature.metadata.server_version,
&mock_signature.signature,
&mock_signature.pubkey,
])
.map_err(|e| format!("unable to write mock signature: {e}"))?;
Ok(())
Expand Down Expand Up @@ -435,13 +439,7 @@ pub fn store_signer_db_messages(
"Storing stacks MockSignature by signer {}",
chunk.pubkey
);
store_mock_signature(
&db_tx,
&data.mock_proposal.peer_info,
&data.metadata,
Some(message_id),
None,
)?;
store_mock_signature(&db_tx, &data, Some(message_id), None)?;
}
StacksSignerMessage::MockProposal(data) => {
try_info!(
Expand Down Expand Up @@ -477,8 +475,7 @@ pub fn store_signer_db_messages(
for signature in data.mock_signatures.iter() {
store_mock_signature(
&db_tx,
&signature.mock_proposal.peer_info,
&signature.metadata,
&signature,
None,
Some(mock_block_id),
)?;
Expand Down Expand Up @@ -654,7 +651,8 @@ pub fn get_signer_db_messages_received_at_block(
"mock_signature" => db_tx
.query_row(
"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.pox_consensus, p.server_version AS peer_version, p.network_id, s.server_version, s.signature,
s.pubkey
FROM mock_signatures AS s
INNER JOIN mock_proposals AS p ON p.id = s.mock_proposal_id
WHERE s.message_id = ?",
Expand All @@ -675,7 +673,9 @@ pub fn get_signer_db_messages_received_at_block(
},
metadata: SignerMessageMetadata {
server_version: signature_row.get(8).unwrap()
}
},
signature: signature_row.get(9).unwrap(),
pubkey: signature_row.get(10).unwrap()
}))
},
)
Expand Down Expand Up @@ -715,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, p.index_block_hash, s.server_version
p.network_id, p.index_block_hash, s.server_version, s.signature, s.pubkey
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 @@ -737,7 +737,9 @@ pub fn get_signer_db_messages_received_at_block(
},
metadata: SignerMessageMetadata {
server_version: signature_row.get(8).unwrap()
}
},
signature: signature_row.get(9).unwrap(),
pubkey: signature_row.get(10).unwrap()
});
}
Ok(StacksSignerMessage::MockBlock(MockBlockData {
Expand Down
27 changes: 20 additions & 7 deletions components/chainhook-sdk/src/indexer/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,13 +802,16 @@ pub fn standardize_stacks_stackerdb_chunks(
.mock_signatures
.iter()
.map(|signature| standardize_stacks_signer_mock_signature(signature))
.try_fold(Vec::new(), |mut acc, item| -> Result<Vec<MockSignatureData>, String> {
item.and_then(|val| {
acc.push(val);
Ok(())
})?;
Ok(acc)
})?,
.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 @@ -829,13 +832,23 @@ pub fn standardize_stacks_stackerdb_chunks(
pub fn standardize_stacks_signer_mock_signature(
signature: &stacks_codec::codec::MockSignature,
) -> Result<MockSignatureData, String> {
let pubkey = get_signer_pubkey_from_message_hash(
&signature
.mock_proposal
.signer_signature_hash()
.as_bytes()
.to_vec(),
&signature.signature,
)?;
Ok(MockSignatureData {
mock_proposal: MockProposalData {
peer_info: standardize_stacks_signer_peer_info(&signature.mock_proposal.peer_info)?,
},
metadata: SignerMessageMetadata {
server_version: signature.metadata.server_version.clone(),
},
signature: format!("0x{}", signature.signature.to_hex()),
pubkey: format!("0x{}", hex::encode(pubkey)),
})
}

Expand Down
4 changes: 3 additions & 1 deletion components/chainhook-types-rs/src/signers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ pub struct MockProposalData {
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct MockSignatureData {
pub mock_proposal: MockProposalData,
pub metadata: SignerMessageMetadata
pub metadata: SignerMessageMetadata,
pub signature: String,
pub pubkey: String,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
Expand Down
4 changes: 2 additions & 2 deletions components/client/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/client/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hirosystems/chainhook-client",
"version": "2.2.0",
"version": "2.3.0",
"description": "Chainhook TypeScript client",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions components/client/typescript/src/schemas/stacks/signers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export type StacksSignerMessageMockProposalData = Static<
export const StacksSignerMessageMockSignatureDataSchema = Type.Object({
mock_proposal: StacksSignerMessageMockProposalDataSchema,
metadata: StacksSignerMessageMetadataSchema,
signature: Type.String(),
pubkey: Type.String(),
});
export type StacksSignerMessageMockSignatureData = Static<
typeof StacksSignerMessageMockSignatureDataSchema
Expand Down

0 comments on commit c414d79

Please sign in to comment.