use anyhow::{anyhow, Result};
use banyan_shared::{eth::EthClient, proofs, proofs::window, types::*};
use log::info;
use rocket::serde::{Deserialize, Serialize};
use serde_json::from_str;
use std::io::Cursor;
use std::sync::Arc;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChainlinkRequestData {
pub deal_id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ResponseData {
pub deal_id: DealID,
pub success_count: u64,
pub num_windows: u64,
pub status: u16,
pub result: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChainlinkResponse {
pub data: ResponseData,
}
pub struct WebserverState(pub Arc<EthClient>);
/* Function to construct an error response to return to Chainlink */
fn construct_error(deal_id: DealID, reason: String) -> ChainlinkResponse {
ChainlinkResponse {
data: ResponseData {
deal_id,
success_count: 0,
num_windows: 0,
status: 0,
result: reason,
},
}
}
/// this validates the deal based on a deal_id, returns a json response of either the success count and num_windows,
/// or an error message to be turned into Json<ChainlinkResponse> in the caller!
/// TODO fix logging... :|
pub(crate) async fn validate_deal_internal(
provider: Arc<EthClient>,
input_data: ChainlinkRequestData,
) -> Result<ChainlinkResponse> {
let deal_id = from_str(&input_data.deal_id)?;
let deal_info = provider
.get_offer(deal_id)
.await
.map_err(|e| anyhow!("Error in get_deal: {:?}", e))?;
// checking that deal is either finished or cancelled
let current_block_num = provider
.get_latest_block_num()
.await
.map_err(|e| anyhow!("Couldn't get most recent block number: {e}"))?;
// TODO: Why have any of these checks in the API. Shouldn't they all be in the Smart Contract Logic.
let deal_over = EthClient::deal_over(current_block_num, deal_info.clone());
let deal_cancelled = false; // TODO need to figure out how to get this
// this refuses to do the validation computations unless the deal is done with or cancelled
https://github.com/banyancomputer/chainlink-proof-validator/blob/f3c53bb665a0adf94f8a288c635603f08be816d5/src/validate.rs#L61