Skip to content

Commit

Permalink
Merge pull request #243 from rorp/fix/signed_event_validation
Browse files Browse the repository at this point in the history
Fix OracleEvent.validate() for signed numeric events
  • Loading branch information
Tibo-lg authored Nov 6, 2024
2 parents a9c9379 + b684353 commit 39825dd
Showing 1 changed file with 28 additions and 3 deletions.
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 @@ -410,6 +416,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 @@ -424,6 +440,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 Down Expand Up @@ -451,7 +476,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 @@ -476,7 +501,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

0 comments on commit 39825dd

Please sign in to comment.