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

Grpc: Support list offers endpoint #778

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions src/grpcd/proto/farcaster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ service Farcaster {
rpc Progress(ProgressRequest) returns (ProgressResponse){}
rpc NeedsFunding(NeedsFundingRequest) returns (NeedsFundingResponse){}
rpc SweepAddress(SweepAddressRequest) returns (SweepAddressResponse){}
rpc ListOffers(ListOffersRequest) returns (ListOffersResponse){}
}

message InfoRequest {
Expand Down Expand Up @@ -74,6 +75,23 @@ message PeersResponse {
repeated string peers = 2;
}

message ListOffersRequest {
uint32 id = 1;
OfferSelector selector = 2;
}

enum OfferSelector {
OPEN = 0;
IN_PROGRESS = 1;
ENDED = 2;
ALL = 3;
}

message ListOffersResponse {
uint32 id = 1;
repeated string public_offers = 2;
}

message CheckpointsRequest {
uint32 id = 1;
}
Expand Down
53 changes: 53 additions & 0 deletions src/grpcd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::bus::bridge::BridgeMsg;
use crate::bus::ctl::ProtoPublicOffer;
use crate::bus::ctl::PubOffer;
use crate::bus::info::Address;
use crate::bus::info::OfferStatusSelector;
use crate::bus::AddressSecretKey;
use crate::bus::Failure;
use crate::service::Endpoints;
Expand Down Expand Up @@ -49,6 +50,8 @@ use self::farcaster::AbortSwapRequest;
use self::farcaster::AbortSwapResponse;
use self::farcaster::CheckpointsRequest;
use self::farcaster::CheckpointsResponse;
use self::farcaster::ListOffersRequest;
use self::farcaster::ListOffersResponse;
use self::farcaster::MakeRequest;
use self::farcaster::MakeResponse;
use self::farcaster::NeedsFundingRequest;
Expand Down Expand Up @@ -139,6 +142,17 @@ impl From<Blockchain> for farcaster::Blockchain {
}
}

impl From<farcaster::OfferSelector> for OfferStatusSelector {
fn from(t: farcaster::OfferSelector) -> OfferStatusSelector {
match t {
farcaster::OfferSelector::All => OfferStatusSelector::All,
farcaster::OfferSelector::Open => OfferStatusSelector::Open,
farcaster::OfferSelector::InProgress => OfferStatusSelector::InProgress,
farcaster::OfferSelector::Ended => OfferStatusSelector::Ended,
}
}
}

#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Hash, Display)]
#[display(Debug)]
pub struct IdCounter(u64);
Expand Down Expand Up @@ -308,6 +322,45 @@ impl Farcaster for FarcasterService {
}
}

async fn list_offers(
&self,
request: GrpcRequest<ListOffersRequest>,
) -> Result<GrpcResponse<ListOffersResponse>, Status> {
debug!("Received a grpc request: {:?}", request);
let ListOffersRequest {
id,
selector: grpc_offer_selector,
} = request.into_inner();
let selector = farcaster::OfferSelector::from_i32(grpc_offer_selector)
.ok_or(Status::invalid_argument("selector"))?
.into();
let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Info {
request: InfoMsg::ListOffers(selector),
service_id: ServiceId::Farcasterd,
}))
.await?;
match oneshot_rx.await {
Ok(BusMsg::Info(InfoMsg::OfferList(mut offers))) => {
let reply = ListOffersResponse {
id,
public_offers: offers.drain(..).map(|o| o.offer).collect(),
};
Ok(GrpcResponse::new(reply))
}
Ok(BusMsg::Info(InfoMsg::OfferStatusList(mut offers))) => {
let reply = ListOffersResponse {
id,
public_offers: offers.drain(..).map(|o| o.offer.to_string()).collect(),
};
Ok(GrpcResponse::new(reply))
}
Err(error) => Err(Status::internal(format!("{}", error))),
Ok(BusMsg::Ctl(CtlMsg::Failure(Failure { info, .. }))) => Err(Status::internal(info)),
_ => Err(Status::invalid_argument("received invalid response")),
}
}

async fn offer_info(
&self,
request: GrpcRequest<OfferInfoRequest>,
Expand Down
14 changes: 11 additions & 3 deletions tests/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ extern crate log;

use crate::farcaster::{
farcaster_client::FarcasterClient, AbortSwapRequest, CheckpointsRequest, InfoResponse,
MakeRequest, NeedsFundingRequest, OfferInfoRequest, PeersRequest, ProgressRequest,
RestoreCheckpointRequest, RevokeOfferRequest, SwapInfoRequest, SweepAddressRequest,
TakeRequest,
ListOffersRequest, MakeRequest, NeedsFundingRequest, OfferInfoRequest, OfferSelector,
PeersRequest, ProgressRequest, RestoreCheckpointRequest, RevokeOfferRequest, SwapInfoRequest,
SweepAddressRequest, TakeRequest,
};
use bitcoincore_rpc::RpcApi;
use farcaster::{InfoRequest, MakeResponse, NeedsFundingResponse};
Expand Down Expand Up @@ -111,6 +111,14 @@ async fn grpc_server_functional_test() {
let response = farcaster_client_2.offer_info(offer_info_request).await;
assert_eq!(response.unwrap().into_inner().id, 21);

// Test List offers
let list_offers_request = tonic::Request::new(ListOffersRequest {
id: 22,
selector: OfferSelector::All.into(),
});
let response = farcaster_client_1.list_offers(list_offers_request).await;
assert_eq!(response.unwrap().into_inner().id, 22);

// Test take offer
let take_request = TakeRequest {
id: 5,
Expand Down