Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OracleEvent.validate() for signed numeric events #243

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions dlc-messages/src/oracle_msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ impl OracleEvent {
pub fn validate(&self) -> Result<(), Error> {
let expected_nb_nonces = match &self.event_descriptor {
EventDescriptor::EnumEvent(_) => 1,
EventDescriptor::DigitDecompositionEvent(d) => d.nb_digits as usize,
EventDescriptor::DigitDecompositionEvent(d) => {
if d.is_signed {
d.nb_digits as usize + 1
} else {
d.nb_digits as usize
}
}
};

if expected_nb_nonces == self.oracle_nonces.len() {
Expand Down Expand Up @@ -366,6 +372,16 @@ mod tests {
}
}

fn signed_digit_descriptor() -> DigitDecompositionEventDescriptor {
DigitDecompositionEventDescriptor {
base: 2,
is_signed: true,
unit: "kg/sats".to_string(),
precision: 1,
nb_digits: 10,
}
}

fn some_schnorr_pubkey() -> XOnlyPublicKey {
let key_pair = Keypair::new(SECP256K1, &mut thread_rng());
XOnlyPublicKey::from_keypair(&key_pair).0
Expand All @@ -380,6 +396,15 @@ mod tests {
}
}

fn signed_digit_event(nb_nonces: usize) -> OracleEvent {
OracleEvent {
oracle_nonces: (0..nb_nonces).map(|_| some_schnorr_pubkey()).collect(),
event_maturity_epoch: 10,
event_descriptor: EventDescriptor::DigitDecompositionEvent(signed_digit_descriptor()),
event_id: "test-signed".to_string(),
}
}

fn enum_event(nb_nonces: usize) -> OracleEvent {
OracleEvent {
oracle_nonces: (0..nb_nonces).map(|_| some_schnorr_pubkey()).collect(),
Expand All @@ -393,7 +418,7 @@ mod tests {
fn valid_oracle_announcement_passes_validation_test() {
let key_pair = Keypair::new(SECP256K1, &mut thread_rng());
let oracle_pubkey = XOnlyPublicKey::from_keypair(&key_pair).0;
let events = [digit_event(10), enum_event(1)];
let events = [digit_event(10), signed_digit_event(11), enum_event(1)];
for event in events {
let mut event_hex = Vec::new();
event
Expand All @@ -418,7 +443,7 @@ mod tests {
fn invalid_oracle_announcement_fails_validation_test() {
let key_pair = Keypair::new(SECP256K1, &mut thread_rng());
let oracle_pubkey = XOnlyPublicKey::from_keypair(&key_pair).0;
let events = [digit_event(9), enum_event(2)];
let events = [digit_event(9), signed_digit_event(10), enum_event(2)];
for event in events {
let mut event_hex = Vec::new();
event
Expand Down
Loading