Skip to content

Commit

Permalink
Fix: Lazily evaluate acceptedPublicKeys
Browse files Browse the repository at this point in the history
If the `acceptedPublicKeys` val is evaluated eagerly within the
`Verification` trait then the `activePublicKey` and `alsoAccepted` vals
won't have been initialised yet from the class which extends the trait.

This was leading to `acceptedPublicKeys` evaluating to 'null' at
runtime.

The added test was failing with the pre-existing code with a very
similar message to the one observed in CODE, so we have some
confidence that it's testing for this case effectively.

See: https://docs.scala-lang.org/tutorials/FAQ/initialization-order.html

Co-authored-by: Roberto Tyley <52038+rtyley@users.noreply.github.com>
  • Loading branch information
bryophyta and rtyley committed Sep 12, 2024
1 parent 97cd645 commit a6ae5e8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object CryptoConf {
val activePublicKey: PublicKey
val alsoAccepted: Seq[PublicKey]

val acceptedPublicKeys: LazyList[PublicKey] = LazyList(activePublicKey) ++ alsoAccepted
lazy val acceptedPublicKeys: LazyList[PublicKey] = LazyList(activePublicKey) ++ alsoAccepted

private[CryptoConf] def acceptsActiveKeyFrom(other: Verification): Boolean = acceptedPublicKeys.contains(other.activePublicKey)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CryptoConfTest extends AnyFreeSpec with Matchers with EitherValues {
val rotationUpcomingConf = loadExample("1.rotation-upcoming")
rotationUpcomingConf.activeKeyPair should === (legacyConf.activeKeyPair)
rotationUpcomingConf.alsoAccepted should not be empty
val expectedAcceptedPublicKeys = rotationUpcomingConf.activeKeyPair.publicKey +: rotationUpcomingConf.alsoAccepted
rotationUpcomingConf.acceptedPublicKeys should === (expectedAcceptedPublicKeys)

val rotationInProgressConf = loadExample("2.rotation-in-progress")
rotationInProgressConf.activeKeyPair should !== (legacyConf.activeKeyPair)
Expand Down

0 comments on commit a6ae5e8

Please sign in to comment.