Skip to content

Commit

Permalink
Optionally pass in oracle announcements on Offer
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Nov 27, 2023
1 parent 5b5da81 commit dd0a9b4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
72 changes: 57 additions & 15 deletions dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,28 @@ where
&mut self,
contract_input: &ContractInput,
counter_party: PublicKey,
oracle_announcements: Option<Vec<Vec<OracleAnnouncement>>>,
) -> Result<OfferDlc, Error> {
contract_input.validate()?;

let oracle_announcements = contract_input
.contract_infos
.iter()
.map(|x| self.get_oracle_announcements(&x.oracles))
.collect::<Result<Vec<_>, Error>>()?;
let oracle_announcements = match oracle_announcements {
Some(announcements) => {
// validate that the number of announcements is correct
if announcements.len() != contract_input.contract_infos.len() {
return Err(Error::InvalidParameters(format!(
"Expected {} oracle announcement vectors, got {}",
contract_input.contract_infos.len(),
announcements.len()
)));
}
announcements
}
None => contract_input
.contract_infos
.iter()
.map(|x| self.get_oracle_announcements(&x.oracles))
.collect::<Result<Vec<_>, Error>>()?,
};

let (offered_contract, offer_msg) = crate::contract_updater::offer_contract(
&self.secp,
Expand Down Expand Up @@ -728,12 +742,26 @@ where
&mut self,
contract_input: &ContractInput,
counter_party: PublicKey,
oracle_announcements: Option<Vec<Vec<OracleAnnouncement>>>,
) -> Result<OfferChannel, Error> {
let oracle_announcements = contract_input
.contract_infos
.iter()
.map(|x| self.get_oracle_announcements(&x.oracles))
.collect::<Result<Vec<_>, Error>>()?;
let oracle_announcements = match oracle_announcements {
Some(announcements) => {
// validate that the number of announcements is correct
if announcements.len() != contract_input.contract_infos.len() {
return Err(Error::InvalidParameters(format!(
"Expected {} oracle announcement vectors, got {}",
contract_input.contract_infos.len(),
announcements.len()
)));
}
announcements
}
None => contract_input
.contract_infos
.iter()
.map(|x| self.get_oracle_announcements(&x.oracles))
.collect::<Result<Vec<_>, Error>>()?,
};

let (offered_channel, offered_contract) = crate::channel_updater::offer_channel(
&self.secp,
Expand Down Expand Up @@ -876,15 +904,29 @@ where
channel_id: &ChannelId,
counter_payout: u64,
contract_input: &ContractInput,
oracle_announcements: Option<Vec<Vec<OracleAnnouncement>>>,
) -> Result<(RenewOffer, PublicKey), Error> {
let mut signed_channel =
get_channel_in_state!(self, channel_id, Signed, None as Option<PublicKey>)?;

let oracle_announcements = contract_input
.contract_infos
.iter()
.map(|x| self.get_oracle_announcements(&x.oracles))
.collect::<Result<Vec<_>, Error>>()?;
let oracle_announcements = match oracle_announcements {
Some(announcements) => {
// validate that the number of announcements is correct
if announcements.len() != contract_input.contract_infos.len() {
return Err(Error::InvalidParameters(format!(
"Expected {} oracle announcement vectors, got {}",
contract_input.contract_infos.len(),
announcements.len()
)));
}
announcements
}
None => contract_input
.contract_infos
.iter()
.map(|x| self.get_oracle_announcements(&x.oracles))
.collect::<Result<Vec<_>, Error>>()?,
};

let (msg, offered_contract) = crate::channel_updater::renew_offer(
&self.secp,
Expand Down
11 changes: 6 additions & 5 deletions dlc-manager/tests/channel_execution_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ fn channel_execution_test(test_params: TestParams, path: TestPath) {
"0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166"
.parse()
.unwrap(),
None,
)
.expect("Send offer error");

Expand Down Expand Up @@ -967,7 +968,7 @@ fn renew_channel(
let (renew_offer, _) = first
.lock()
.unwrap()
.renew_offer(&channel_id, 100000000, contract_input)
.renew_offer(&channel_id, 100000000, contract_input, None)
.expect("to be able to renew channel contract");

first_send
Expand Down Expand Up @@ -1023,7 +1024,7 @@ fn renew_reject(
let (renew_offer, _) = first
.lock()
.unwrap()
.renew_offer(&channel_id, 100000000, contract_input)
.renew_offer(&channel_id, 100000000, contract_input, None)
.expect("to be able to renew channel contract");

first_send
Expand Down Expand Up @@ -1064,13 +1065,13 @@ fn renew_race(
let (renew_offer, _) = first
.lock()
.unwrap()
.renew_offer(&channel_id, 100000000, contract_input)
.renew_offer(&channel_id, 100000000, contract_input, None)
.expect("to be able to renew channel contract");

let (renew_offer_2, _) = second
.lock()
.unwrap()
.renew_offer(&channel_id, 100000000, contract_input)
.renew_offer(&channel_id, 100000000, contract_input, None)
.expect("to be able to renew channel contract");

first_send
Expand Down Expand Up @@ -1148,7 +1149,7 @@ fn renew_timeout(
let (renew_offer, _) = first
.lock()
.unwrap()
.renew_offer(&channel_id, 100000000, contract_input)
.renew_offer(&channel_id, 100000000, contract_input, None)
.expect("to be able to offer a settlement of the contract.");

first_send
Expand Down
1 change: 1 addition & 0 deletions dlc-manager/tests/manager_execution_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ fn manager_execution_test(test_params: TestParams, path: TestPath) {
"0218845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166"
.parse()
.unwrap(),
None,
)
.expect("Send offer error");

Expand Down
6 changes: 3 additions & 3 deletions sample/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ pub(crate) async fn poll_for_user_input(
manager_clone
.lock()
.unwrap()
.send_offer(&contract_input, pubkey)
.send_offer(&contract_input, pubkey, None)
.expect("Error sending offer"),
)
} else {
DlcMessage::OfferChannel(
manager_clone
.lock()
.unwrap()
.offer_channel(&contract_input, pubkey)
.offer_channel(&contract_input, pubkey, None)
.expect("Error sending offer channel"),
)
}
Expand Down Expand Up @@ -412,7 +412,7 @@ pub(crate) async fn poll_for_user_input(
manager_clone
.lock()
.unwrap()
.renew_offer(&channel_id, counter_payout, &contract_input)
.renew_offer(&channel_id, counter_payout, &contract_input, None)
.expect("Error sending offer")
})
.await
Expand Down

0 comments on commit dd0a9b4

Please sign in to comment.