Add endpoint to get round via id & round stage (#202)

This commit is contained in:
Pietralberto Mazza
2024-07-12 15:42:21 +02:00
committed by GitHub
parent 4ff1c7c87a
commit 498394a191
8 changed files with 853 additions and 374 deletions

View File

@@ -259,6 +259,36 @@
]
}
},
"/v1/round/id/{id}": {
"get": {
"operationId": "ArkService_GetRoundById",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/v1GetRoundByIdResponse"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}
],
"tags": [
"ArkService"
]
}
},
"/v1/round/{txid}": {
"get": {
"summary": "TODO BTC: signTree rpc",
@@ -427,6 +457,14 @@
}
}
},
"v1GetRoundByIdResponse": {
"type": "object",
"properties": {
"round": {
"$ref": "#/definitions/v1Round"
}
}
},
"v1GetRoundResponse": {
"type": "object",
"properties": {
@@ -577,6 +615,9 @@
"items": {
"type": "string"
}
},
"stage": {
"$ref": "#/definitions/v1RoundStage"
}
}
},
@@ -628,6 +669,17 @@
}
}
},
"v1RoundStage": {
"type": "string",
"enum": [
"ROUND_STAGE_UNSPECIFIED",
"ROUND_STAGE_REGISTRATION",
"ROUND_STAGE_FINALIZATION",
"ROUND_STAGE_FINALIZED",
"ROUND_STAGE_FAILED"
],
"default": "ROUND_STAGE_UNSPECIFIED"
},
"v1Tree": {
"type": "object",
"properties": {

View File

@@ -29,6 +29,11 @@ service ArkService {
get: "/v1/round/{txid}"
};
};
rpc GetRoundById(GetRoundByIdRequest) returns (GetRoundByIdResponse) {
option (google.api.http) = {
get: "/v1/round/id/{id}"
};
};
rpc GetEventStream(GetEventStreamRequest) returns (stream GetEventStreamResponse) {
option (google.api.http) = {
get: "/v1/events"
@@ -92,6 +97,13 @@ message GetRoundResponse {
Round round = 1;
}
message GetRoundByIdRequest {
string id = 1;
}
message GetRoundByIdResponse {
Round round = 1;
}
message GetEventStreamRequest {}
message GetEventStreamResponse {
oneof event {
@@ -166,6 +178,14 @@ message RoundFailed {
// TYPES
enum RoundStage {
ROUND_STAGE_UNSPECIFIED = 0;
ROUND_STAGE_REGISTRATION = 1;
ROUND_STAGE_FINALIZATION = 2;
ROUND_STAGE_FINALIZED = 3;
ROUND_STAGE_FAILED = 4;
}
message Round {
string id = 1;
int64 start = 2;
@@ -174,6 +194,7 @@ message Round {
Tree congestion_tree = 5;
repeated string forfeit_txs = 6;
repeated string connectors = 7;
RoundStage stage = 8;
}
message Input {

File diff suppressed because it is too large Load Diff

View File

@@ -161,6 +161,58 @@ func local_request_ArkService_GetRound_0(ctx context.Context, marshaler runtime.
}
func request_ArkService_GetRoundById_0(ctx context.Context, marshaler runtime.Marshaler, client ArkServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq GetRoundByIdRequest
var metadata runtime.ServerMetadata
var (
val string
ok bool
err error
_ = err
)
val, ok = pathParams["id"]
if !ok {
return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "id")
}
protoReq.Id, err = runtime.String(val)
if err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "id", err)
}
msg, err := client.GetRoundById(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
}
func local_request_ArkService_GetRoundById_0(ctx context.Context, marshaler runtime.Marshaler, server ArkServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq GetRoundByIdRequest
var metadata runtime.ServerMetadata
var (
val string
ok bool
err error
_ = err
)
val, ok = pathParams["id"]
if !ok {
return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "id")
}
protoReq.Id, err = runtime.String(val)
if err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "id", err)
}
msg, err := server.GetRoundById(ctx, &protoReq)
return msg, metadata, err
}
func request_ArkService_GetEventStream_0(ctx context.Context, marshaler runtime.Marshaler, client ArkServiceClient, req *http.Request, pathParams map[string]string) (ArkService_GetEventStreamClient, runtime.ServerMetadata, error) {
var protoReq GetEventStreamRequest
var metadata runtime.ServerMetadata
@@ -458,6 +510,31 @@ func RegisterArkServiceHandlerServer(ctx context.Context, mux *runtime.ServeMux,
})
mux.Handle("GET", pattern_ArkService_GetRoundById_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
var stream runtime.ServerTransportStream
ctx = grpc.NewContextWithServerTransportStream(ctx, &stream)
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/ark.v1.ArkService/GetRoundById", runtime.WithHTTPPathPattern("/v1/round/id/{id}"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := local_request_ArkService_GetRoundById_0(annotatedContext, inboundMarshaler, server, req, pathParams)
md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer())
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_ArkService_GetRoundById_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
mux.Handle("GET", pattern_ArkService_GetEventStream_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
err := status.Error(codes.Unimplemented, "streaming calls are not yet supported in the in-process transport")
_, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
@@ -719,6 +796,28 @@ func RegisterArkServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux,
})
mux.Handle("GET", pattern_ArkService_GetRoundById_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/ark.v1.ArkService/GetRoundById", runtime.WithHTTPPathPattern("/v1/round/id/{id}"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := request_ArkService_GetRoundById_0(annotatedContext, inboundMarshaler, client, req, pathParams)
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_ArkService_GetRoundById_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
mux.Handle("GET", pattern_ArkService_GetEventStream_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
@@ -863,6 +962,8 @@ var (
pattern_ArkService_GetRound_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"v1", "round", "txid"}, ""))
pattern_ArkService_GetRoundById_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 1, 0, 4, 1, 5, 2}, []string{"v1", "round", "id"}, ""))
pattern_ArkService_GetEventStream_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "events"}, ""))
pattern_ArkService_Ping_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"v1", "ping", "payment_id"}, ""))
@@ -885,6 +986,8 @@ var (
forward_ArkService_GetRound_0 = runtime.ForwardResponseMessage
forward_ArkService_GetRoundById_0 = runtime.ForwardResponseMessage
forward_ArkService_GetEventStream_0 = runtime.ForwardResponseStream
forward_ArkService_Ping_0 = runtime.ForwardResponseMessage

View File

@@ -23,6 +23,7 @@ type ArkServiceClient interface {
FinalizePayment(ctx context.Context, in *FinalizePaymentRequest, opts ...grpc.CallOption) (*FinalizePaymentResponse, error)
// TODO BTC: signTree rpc
GetRound(ctx context.Context, in *GetRoundRequest, opts ...grpc.CallOption) (*GetRoundResponse, error)
GetRoundById(ctx context.Context, in *GetRoundByIdRequest, opts ...grpc.CallOption) (*GetRoundByIdResponse, error)
GetEventStream(ctx context.Context, in *GetEventStreamRequest, opts ...grpc.CallOption) (ArkService_GetEventStreamClient, error)
Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error)
ListVtxos(ctx context.Context, in *ListVtxosRequest, opts ...grpc.CallOption) (*ListVtxosResponse, error)
@@ -75,6 +76,15 @@ func (c *arkServiceClient) GetRound(ctx context.Context, in *GetRoundRequest, op
return out, nil
}
func (c *arkServiceClient) GetRoundById(ctx context.Context, in *GetRoundByIdRequest, opts ...grpc.CallOption) (*GetRoundByIdResponse, error) {
out := new(GetRoundByIdResponse)
err := c.cc.Invoke(ctx, "/ark.v1.ArkService/GetRoundById", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *arkServiceClient) GetEventStream(ctx context.Context, in *GetEventStreamRequest, opts ...grpc.CallOption) (ArkService_GetEventStreamClient, error) {
stream, err := c.cc.NewStream(ctx, &ArkService_ServiceDesc.Streams[0], "/ark.v1.ArkService/GetEventStream", opts...)
if err != nil {
@@ -161,6 +171,7 @@ type ArkServiceServer interface {
FinalizePayment(context.Context, *FinalizePaymentRequest) (*FinalizePaymentResponse, error)
// TODO BTC: signTree rpc
GetRound(context.Context, *GetRoundRequest) (*GetRoundResponse, error)
GetRoundById(context.Context, *GetRoundByIdRequest) (*GetRoundByIdResponse, error)
GetEventStream(*GetEventStreamRequest, ArkService_GetEventStreamServer) error
Ping(context.Context, *PingRequest) (*PingResponse, error)
ListVtxos(context.Context, *ListVtxosRequest) (*ListVtxosResponse, error)
@@ -185,6 +196,9 @@ func (UnimplementedArkServiceServer) FinalizePayment(context.Context, *FinalizeP
func (UnimplementedArkServiceServer) GetRound(context.Context, *GetRoundRequest) (*GetRoundResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetRound not implemented")
}
func (UnimplementedArkServiceServer) GetRoundById(context.Context, *GetRoundByIdRequest) (*GetRoundByIdResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetRoundById not implemented")
}
func (UnimplementedArkServiceServer) GetEventStream(*GetEventStreamRequest, ArkService_GetEventStreamServer) error {
return status.Errorf(codes.Unimplemented, "method GetEventStream not implemented")
}
@@ -287,6 +301,24 @@ func _ArkService_GetRound_Handler(srv interface{}, ctx context.Context, dec func
return interceptor(ctx, in, info, handler)
}
func _ArkService_GetRoundById_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetRoundByIdRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ArkServiceServer).GetRoundById(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/ark.v1.ArkService/GetRoundById",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ArkServiceServer).GetRoundById(ctx, req.(*GetRoundByIdRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ArkService_GetEventStream_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(GetEventStreamRequest)
if err := stream.RecvMsg(m); err != nil {
@@ -421,6 +453,10 @@ var ArkService_ServiceDesc = grpc.ServiceDesc{
MethodName: "GetRound",
Handler: _ArkService_GetRound_Handler,
},
{
MethodName: "GetRoundById",
Handler: _ArkService_GetRoundById_Handler,
},
{
MethodName: "Ping",
Handler: _ArkService_Ping_Handler,

View File

@@ -41,6 +41,7 @@ type Service interface {
ClaimVtxos(ctx context.Context, creds string, receivers []domain.Receiver) error
SignVtxos(ctx context.Context, forfeitTxs []string) error
GetRoundByTxid(ctx context.Context, poolTxid string) (*domain.Round, error)
GetRoundById(ctx context.Context, id string) (*domain.Round, error)
GetCurrentRound(ctx context.Context) (*domain.Round, error)
GetEventsChannel(ctx context.Context) <-chan domain.RoundEvent
UpdatePaymentStatus(ctx context.Context, id string) (unsignedForfeitTxs []string, round *domain.Round, err error)
@@ -223,6 +224,10 @@ func (s *service) GetCurrentRound(ctx context.Context) (*domain.Round, error) {
return domain.NewRoundFromEvents(s.currentRound.Events()), nil
}
func (s *service) GetRoundById(ctx context.Context, id string) (*domain.Round, error) {
return s.repoManager.Rounds().GetRoundWithId(ctx, id)
}
func (s *service) GetInfo(ctx context.Context) (*ServiceInfo, error) {
pubkey := hex.EncodeToString(s.pubkey.SerializeCompressed())

View File

@@ -182,6 +182,7 @@ func (h *handler) GetRound(ctx context.Context, req *arkv1.GetRoundRequest) (*ar
CongestionTree: castCongestionTree(round.CongestionTree),
ForfeitTxs: round.ForfeitTxs,
Connectors: round.Connectors,
Stage: toRoundStage(round.Stage),
},
}, nil
}
@@ -200,6 +201,34 @@ func (h *handler) GetRound(ctx context.Context, req *arkv1.GetRoundRequest) (*ar
CongestionTree: castCongestionTree(round.CongestionTree),
ForfeitTxs: round.ForfeitTxs,
Connectors: round.Connectors,
Stage: toRoundStage(round.Stage),
},
}, nil
}
func (h *handler) GetRoundById(
ctx context.Context, req *arkv1.GetRoundByIdRequest,
) (*arkv1.GetRoundByIdResponse, error) {
id := req.GetId()
if len(id) <= 0 {
return nil, status.Error(codes.InvalidArgument, "missing round id")
}
round, err := h.svc.GetRoundById(ctx, id)
if err != nil {
return nil, err
}
return &arkv1.GetRoundByIdResponse{
Round: &arkv1.Round{
Id: round.Id,
Start: round.StartingTimestamp,
End: round.EndingTimestamp,
PoolTx: round.UnsignedTx,
CongestionTree: castCongestionTree(round.CongestionTree),
ForfeitTxs: round.ForfeitTxs,
Connectors: round.Connectors,
Stage: toRoundStage(round.Stage),
},
}, nil
}

View File

@@ -62,3 +62,21 @@ func parseReceivers(outs []*arkv1.Output) ([]domain.Receiver, error) {
}
return receivers, nil
}
func toRoundStage(stage domain.Stage) arkv1.RoundStage {
if stage.Failed {
return arkv1.RoundStage_ROUND_STAGE_FAILED
}
switch stage.Code {
case domain.RegistrationStage:
return arkv1.RoundStage_ROUND_STAGE_REGISTRATION
case domain.FinalizationStage:
if stage.Ended {
return arkv1.RoundStage_ROUND_STAGE_FINALIZED
}
return arkv1.RoundStage_ROUND_STAGE_FINALIZATION
default:
return arkv1.RoundStage_ROUND_STAGE_UNSPECIFIED
}
}