Skip to content

Commit

Permalink
ATL-3693 DID Update operation causes operations counter blew up in PR…
Browse files Browse the repository at this point in the history
…ISM Node (#810)
  • Loading branch information
Shota Jolbordi authored Mar 17, 2023
1 parent ba7b3e3 commit a801d7f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ object OperationsCounters {
private val REVOKE_CREDENTIALS_TAG_VALUE = "revoke-credentials"
private val CREATE_DID_TAG_VALUE = "create-did"
private val ISSUE_CREDENTIAL_BATCH_TAG_VALUE = "issue-credential-batch"
private val PROTOCOL_VERSION_UPDATE_OPERATION_VALUE = "protocol-version-update"
private val UPDATE_DID_OPERATION_TAG_VALUE = "did-update"
private val DEACTIVATE_DID_TAG_VALUE = "deactivate-did"

// Values for atala update did operations
private val EMPTY_ACTION_TAG_VALUE = "empty-did-update"
private val ADD_KEY_ACTION_TAG_VALUE = "add-key"
private val REMOVE_KEY_ACTION_TAG_VALUE = "remove-key"
private val ADD_SERVICE_ACTION_TAG_VALUE = "add-service"
private val REMOVE_SERVICE_ACTION_TAG_VALUE = "remove-service"
private val UPDATE_SERVICE_ACTION_TAG_VALUE = "update-service"

private lazy val receivedObjectsCounter: Metric.Counter =
Kamon.counter(RECEIVED_OPERATION_METRIC_NAME)
Expand All @@ -47,7 +52,7 @@ object OperationsCounters {
private lazy val failedToProcessBlocksCounter =
Kamon.counter(FAILED_TO_PROCESS_BLOCKS_METRIC_NAME)

def countReceivedAtalaOperations(in: List[SignedAtalaOperation]): Unit =
def countReceivedAtalaOperations(in: List[SignedAtalaOperation]): Either[Throwable, Unit] =
increaseOperationsOccurrencesCounter(
in,
receivedObjectsCounter,
Expand All @@ -57,7 +62,7 @@ object OperationsCounters {
def failedToStoreToDbAtalaOperations(
in: List[SignedAtalaOperation],
error: NodeError
): Unit =
): Either[Throwable, Unit] =
increaseOperationsOccurrencesCounter(
in,
failedToStoreObjectsCounter,
Expand Down Expand Up @@ -97,14 +102,18 @@ object OperationsCounters {
in: List[SignedAtalaOperation],
counter: Metric.Counter,
tagSetBuilder: TagSet.Builder
): Unit =
Try {
): Either[Throwable, Unit] = {
val res = Try {
val operationAndOccurrences =
in.map(_.getOperation).groupBy(_.operation).view.mapValues(_.size)
operationAndOccurrences.foreach { case (operation, occurrences) =>
countAtalaDidOperations(operation, occurrences, counter, tagSetBuilder)
}
}.toEither.left.foreach(error => logger.error(s"${counter.name} counter just blew up", error))
}.toEither

res.left.foreach(error => logger.error(s"${counter.name} counter just blew up", error))
res
}

private def countAtalaDidOperations(
in: AtalaOperation.Operation,
Expand Down Expand Up @@ -165,6 +174,8 @@ object OperationsCounters {
case AtalaOperation.Operation.RevokeCredentials(_) =>
REVOKE_CREDENTIALS_TAG_VALUE
case AtalaOperation.Operation.CreateDid(_) => CREATE_DID_TAG_VALUE
case AtalaOperation.Operation.DeactivateDid(_) => DEACTIVATE_DID_TAG_VALUE
case AtalaOperation.Operation.ProtocolVersionUpdate(_) => PROTOCOL_VERSION_UPDATE_OPERATION_VALUE
case AtalaOperation.Operation.IssueCredentialBatch(_) =>
ISSUE_CREDENTIAL_BATCH_TAG_VALUE
// Just in case, must be impossible
Expand All @@ -175,6 +186,9 @@ object OperationsCounters {
case Action.Empty => EMPTY_ACTION_TAG_VALUE
case Action.AddKey(_) => ADD_KEY_ACTION_TAG_VALUE
case Action.RemoveKey(_) => REMOVE_KEY_ACTION_TAG_VALUE
case Action.AddService(_) => ADD_SERVICE_ACTION_TAG_VALUE
case Action.RemoveService(_) => REMOVE_SERVICE_ACTION_TAG_VALUE
case Action.UpdateService(_) => UPDATE_SERVICE_ACTION_TAG_VALUE
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private final class ObjectManagementServiceImpl[F[_]: MonadCancelThrow](
OperationsCounters
.failedToStoreToDbAtalaOperations(List(atalaOperation), err)
AtalaOperationId.of(atalaOperation).asRight[NodeError]
// success case
case ((atalaOperation, _), Right(_)) =>
OperationsCounters
.countReceivedAtalaOperations(List(atalaOperation))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.iohk.atala.prism.node.metrics

import io.iohk.atala.prism.crypto.EC.{INSTANCE => EC}
import org.scalatest.wordspec.AnyWordSpec
import io.iohk.atala.prism.node.services.BlockProcessingServiceSpec
import io.iohk.atala.prism.protos.node_models.{AtalaOperation, SignedAtalaOperation}
import org.scalatest.EitherValues._
import org.scalatest.matchers.must.Matchers

import io.iohk.atala.prism.node.operations.{
CreateDIDOperationSpec,
UpdateDIDOperationSpec,
IssueCredentialBatchOperationSpec,
RevokeCredentialsOperationSpec,
ProtocolVersionUpdateOperationSpec,
DeactivateDIDOperationSpec
}

class OperationsCounterSpec extends AnyWordSpec with Matchers {
"countReceivedAtalaOperations" should {
"count all types of operations" in {
val signingKeyId = "master"
val signingKey = EC.generateKeyPair().getPrivateKey
def sign(op: AtalaOperation): SignedAtalaOperation = BlockProcessingServiceSpec.signOperation(
op,
signingKeyId,
signingKey
)

val createDidOperation = sign(CreateDIDOperationSpec.exampleOperation)

// Includes all type of update actions
val updateDidOperation = sign(UpdateDIDOperationSpec.exampleAllActionsOperation)
val issueCredentialBatchOperation = sign(IssueCredentialBatchOperationSpec.exampleOperation)
val revokeCredentialsOperation = sign(RevokeCredentialsOperationSpec.revokeFullBatchOperation)
val protocolVersionUpdateOperation = sign(
ProtocolVersionUpdateOperationSpec.protocolUpdateOperation(
ProtocolVersionUpdateOperationSpec.protocolVersionInfo1
)
)
val deactivateDIDOperation = sign(DeactivateDIDOperationSpec.exampleOperation)

val operations = List(
createDidOperation,
updateDidOperation,
issueCredentialBatchOperation,
revokeCredentialsOperation,
protocolVersionUpdateOperation,
deactivateDIDOperation
)

OperationsCounters.countReceivedAtalaOperations(operations).value mustBe ()

}
}
}

0 comments on commit a801d7f

Please sign in to comment.