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

[DPP-628][Self-service error codes] Adapt error codes in ApiTimeService #11295

Merged
merged 2 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ private[daml] object ApiServices {

val apiTimeServiceOpt =
optTimeServiceBackend.map(tsb =>
new TimeServiceAuthorization(ApiTimeService.create(ledgerId, tsb), authorizer)
new TimeServiceAuthorization(
ApiTimeService.create(ledgerId, tsb, errorsVersionsSwitcher),
authorizer,
)
)
val writeServiceBackedApiServices =
intitializeWriteServiceBackedApiServices(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import akka.NotUsed
import akka.stream.Materializer
import akka.stream.scaladsl.Source
import com.daml.api.util.TimestampConversion._
import com.daml.error.{ContextualizedErrorLogger, DamlContextualizedErrorLogger}
import com.daml.error.{
ContextualizedErrorLogger,
DamlContextualizedErrorLogger,
ErrorCodesVersionSwitcher,
}
import com.daml.grpc.adapter.ExecutionSequencerFactory
import com.daml.ledger.api.domain.LedgerId
import com.daml.ledger.api.v1.testing.time_service.TimeServiceGrpc.TimeService
Expand All @@ -17,8 +21,7 @@ import com.daml.platform.akkastreams.dispatcher.SignalDispatcher
import com.daml.platform.api.grpc.GrpcApiService
import com.daml.platform.apiserver.TimeServiceBackend
import com.daml.platform.server.api.ValidationLogger
import com.daml.platform.server.api.validation.ErrorFactories
import com.daml.platform.server.api.validation.FieldValidations._
import com.daml.platform.server.api.validation.{ErrorFactories, FieldValidations}
import com.google.protobuf.empty.Empty
import io.grpc.{ServerServiceDefinition, StatusRuntimeException}
import scalaz.syntax.tag._
Expand All @@ -29,6 +32,7 @@ import scala.concurrent.{ExecutionContext, Future}
private[apiserver] final class ApiTimeService private (
val ledgerId: LedgerId,
backend: TimeServiceBackend,
errorCodesVersionSwitcher: ErrorCodesVersionSwitcher,
)(implicit
protected val mat: Materializer,
protected val esf: ExecutionSequencerFactory,
Expand All @@ -45,10 +49,17 @@ private[apiserver] final class ApiTimeService private (
s"${getClass.getSimpleName} initialized with ledger ID ${ledgerId.unwrap}, start time ${backend.getCurrentTime}"
)

private val errorFactories = ErrorFactories(errorCodesVersionSwitcher)
private val fieldValidation = FieldValidations(errorFactories)
tudor-da marked this conversation as resolved.
Show resolved Hide resolved

private val dispatcher = SignalDispatcher[Instant]()
tudor-da marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this one as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do in a followup-PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi #11313


override protected def getTimeSource(request: GetTimeRequest): Source[GetTimeResponse, NotUsed] =
matchLedgerId(ledgerId)(LedgerId(request.ledgerId)).fold(
override protected def getTimeSource(
request: GetTimeRequest
): Source[GetTimeResponse, NotUsed] = {
val validated =
fieldValidation.matchLedgerId(ledgerId = ledgerId)(received = LedgerId(request.ledgerId))
tudor-da marked this conversation as resolved.
Show resolved Hide resolved
validated.fold(
t => Source.failed(ValidationLogger.logFailureWithContext(request, t)),
{ ledgerId =>
logger.info(s"Received request for time with ledger ID $ledgerId")
Expand All @@ -66,6 +77,7 @@ private[apiserver] final class ApiTimeService private (
.via(logger.logErrorsOnStream)
},
)
}

@SuppressWarnings(Array("org.wartremover.warts.JavaSerializable"))
override def setTime(request: SetTimeRequest): Future[Empty] = {
Expand All @@ -80,23 +92,25 @@ private[apiserver] final class ApiTimeService private (
if (success) Right(requestedTime)
else
Left(
ErrorFactories.invalidArgument(None)(
errorFactories.invalidArgument(None)(
s"current_time mismatch. Provided: $expectedTime. Actual: ${backend.getCurrentTime}"
)
)
)
}

val result = for {
_ <- matchLedgerId(ledgerId)(LedgerId(request.ledgerId))
expectedTime <- requirePresence(request.currentTime, "current_time").map(toInstant)
requestedTime <- requirePresence(request.newTime, "new_time").map(toInstant)
_ <- fieldValidation.matchLedgerId(ledgerId)(LedgerId(request.ledgerId))
expectedTime <- fieldValidation
.requirePresence(request.currentTime, "current_time")
.map(toInstant)
requestedTime <- fieldValidation.requirePresence(request.newTime, "new_time").map(toInstant)
_ <- {
if (!requestedTime.isBefore(expectedTime))
Right(())
else
Left(
ErrorFactories.invalidArgument(None)(
errorFactories.invalidArgument(None)(
s"new_time [$requestedTime] is before current_time [$expectedTime]. Setting time backwards is not allowed."
)
)
Expand Down Expand Up @@ -131,11 +145,15 @@ private[apiserver] final class ApiTimeService private (
}

private[apiserver] object ApiTimeService {
def create(ledgerId: LedgerId, backend: TimeServiceBackend)(implicit
def create(
ledgerId: LedgerId,
backend: TimeServiceBackend,
errorCodesVersionSwitcher: ErrorCodesVersionSwitcher,
)(implicit
mat: Materializer,
esf: ExecutionSequencerFactory,
executionContext: ExecutionContext,
loggingContext: LoggingContext,
): TimeService with GrpcApiService =
new ApiTimeService(ledgerId, backend)
new ApiTimeService(ledgerId, backend, errorCodesVersionSwitcher)
}