Skip to content

Commit

Permalink
processing: cache the secp context
Browse files Browse the repository at this point in the history
  • Loading branch information
darosior committed Oct 28, 2021
1 parent 0477e31 commit b572cea
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions fuzz/targets/process_sign_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn main() {
&builder.config,
msg,
&builder.bitcoin_privkey,
&builder.secp,
) {
Ok(res) => res,
Err(cosignerd::processing::SignProcessingError::Database(e)) => panic!("{}", e),
Expand Down
3 changes: 2 additions & 1 deletion src/bin/cosignerd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn daemon_main(
});
let managers_noise_pubkeys: Vec<NoisePubkey> =
config.managers.iter().map(|m| m.noise_key).collect();
let secp_ctx = secp256k1::Secp256k1::new();

// We expect a single connection once in a while, there is *no need* for complexity here so
// just treat incoming connections sequentially.
Expand All @@ -86,7 +87,7 @@ fn daemon_main(
RequestParams::Sign(sign_req) => {
log::trace!("Decoded request: {:#?}", sign_req);

let res = match process_sign_message(&config, sign_req, bitcoin_privkey) {
let res = match process_sign_message(&config, sign_req, bitcoin_privkey, &secp_ctx) {
Ok(res) => res,
Err(e) => {
log::error!("Error when processing 'sign' message: '{}'", e);
Expand Down
9 changes: 6 additions & 3 deletions src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

use revault_net::message::cosigner::{SignRequest, SignResult};
use revault_tx::{
bitcoin::{secp256k1, PublicKey as BitcoinPubkey, SigHashType, util::bip143::SigHashCache},
bitcoin::{secp256k1, util::bip143::SigHashCache, PublicKey as BitcoinPubkey, SigHashType},
error::InputSatisfactionError,
transactions::RevaultTransaction,
};
Expand Down Expand Up @@ -42,10 +42,9 @@ pub fn process_sign_message(
config: &Config,
sign_msg: SignRequest,
bitcoin_privkey: &secp256k1::SecretKey,
secp: &secp256k1::Secp256k1<secp256k1::All>,
) -> Result<SignResult, SignProcessingError> {
let db_path = config.db_file();
// TODO: Cache it in the caller
let secp = secp256k1::Secp256k1::new();
let our_pubkey = BitcoinPubkey {
compressed: true,
key: secp256k1::PublicKey::from_secret_key(&secp, bitcoin_privkey),
Expand Down Expand Up @@ -173,6 +172,7 @@ mod test {
&test_framework.config,
sign_a.clone(),
&test_framework.bitcoin_privkey,
&test_framework.secp,
)
.unwrap();
let tx = tx.unwrap();
Expand All @@ -190,6 +190,7 @@ mod test {
&test_framework.config,
sign_a,
&test_framework.bitcoin_privkey,
&test_framework.secp,
)
.unwrap();
assert_eq!(tx, second_psbt.unwrap());
Expand All @@ -211,6 +212,7 @@ mod test {
&test_framework.config,
sign_a,
&test_framework.bitcoin_privkey,
&test_framework.secp,
)
.unwrap();
assert!(tx.is_none(), "It contains a duplicated outpoint");
Expand All @@ -226,6 +228,7 @@ mod test {
&test_framework.config,
sign_msg,
&test_framework.bitcoin_privkey,
&test_framework.secp,
)
.unwrap_err();
}
Expand Down
2 changes: 2 additions & 0 deletions src/tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct CosignerTestBuilder {
pub noise_privkey: NoisePrivkey,
pub bitcoin_privkey: secp256k1::SecretKey,
pub managers_keys: Vec<DescriptorPublicKey>,
pub secp: secp256k1::Secp256k1<secp256k1::All>,
}

impl CosignerTestBuilder {
Expand Down Expand Up @@ -95,6 +96,7 @@ impl CosignerTestBuilder {
noise_privkey,
bitcoin_privkey,
managers_keys,
secp,
}
}

Expand Down

0 comments on commit b572cea

Please sign in to comment.