-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add PolicyBuilder Make slot a Long (not Int) Add test for DISH policy * Ensure only one Bound of each type exists. * Correct the org name in sbt file Remove redundant test Make slots Long instead of Int
- Loading branch information
1 parent
61d6ad9
commit adeb55b
Showing
9 changed files
with
183 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/scala/iog/psg/cardano/experimental/cli/model/PolicyBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package iog.psg.cardano.experimental.cli.model | ||
|
||
import cats.data.NonEmptyList | ||
import iog.psg.cardano.experimental.cli.api.KeyType | ||
import iog.psg.cardano.experimental.cli.model.Policy.Script | ||
import iog.psg.cardano.experimental.cli.util.RandomTempFolder | ||
|
||
case class PolicyBuilder( | ||
private val scripts: Seq[Script] = Seq.empty, | ||
private val kind: Policy.Kind = Policy.Kind.All | ||
) { | ||
|
||
def withAllSigsRequired(): PolicyBuilder = this.copy(kind = Policy.Kind.All) | ||
def withAnySigRequired(): PolicyBuilder = this.copy(kind = Policy.Kind.Any) | ||
|
||
def withAtLeastSigsRequired(numberOfSigsRequired: Int): PolicyBuilder = | ||
this.copy(kind = Policy.Kind.AtLeast(numberOfSigsRequired)) | ||
|
||
def withSignatureOf(keyHash: KeyHash[_ <: KeyType]): PolicyBuilder = | ||
this.copy(scripts = scripts :+ Policy.Script.Signature(keyHash)) | ||
|
||
|
||
def withBeforeConstraint(slot: Long): PolicyBuilder = | ||
this.copy(scripts = scripts.filter { | ||
case Policy.Script.Bound(_, false) => false | ||
case _ => true | ||
} :+ Policy.Script.Bound(slot, after = false)) | ||
|
||
def withAfterConstraint(slot: Long): PolicyBuilder = | ||
this.copy(scripts = scripts.filter { | ||
case Policy.Script.Bound(_, true) => false | ||
case _ => true | ||
} :+ Policy.Script.Bound(slot, after = true)) | ||
|
||
def build(implicit rootFolder: RandomTempFolder): Policy = { | ||
require( | ||
scripts.exists { | ||
case _: Script.Signature => true | ||
case _ => false | ||
}, | ||
"There must be a at least one script of type Signature!" | ||
) | ||
|
||
Policy(NonEmptyList.of[Policy.Script](scripts.head, scripts.tail: _*), kind) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/test/scala/iog/psg/cardano/experimental/cli/model/PolicyBuilderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package iog.psg.cardano.experimental.cli.model | ||
|
||
import iog.psg.cardano.experimental.cli.api.Verification | ||
import iog.psg.cardano.experimental.cli.model.Policy.Script.{Bound, Signature} | ||
import iog.psg.cardano.experimental.cli.util.RandomTempFolder | ||
import org.scalatest.EitherValues | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import java.nio.file.Path | ||
|
||
class PolicyBuilderSpec extends AnyFlatSpec with Matchers with EitherValues { | ||
|
||
implicit val rootFolder: RandomTempFolder = RandomTempFolder(Path.of(".")) | ||
|
||
"PolicyBuilder" should "fail if no signature" in { | ||
|
||
intercept[IllegalArgumentException] { | ||
PolicyBuilder().build | ||
} | ||
|
||
intercept[IllegalArgumentException] { | ||
PolicyBuilder().withAfterConstraint(3).build | ||
} | ||
|
||
} | ||
|
||
it should "create a minimal policy with one key" in { | ||
val kh = KeyHash[Verification]("somestring") | ||
val p = PolicyBuilder().withSignatureOf(kh).build | ||
p.scripts.toList shouldBe List(Signature(kh)) | ||
p.kind shouldBe Policy.Kind.All | ||
} | ||
|
||
it should "respect the `kind`" in { | ||
val kh = KeyHash[Verification]("somestring") | ||
var p = PolicyBuilder().withSignatureOf(kh).withAllSigsRequired().build | ||
|
||
p.scripts.toList shouldBe List(Signature(kh)) | ||
p.kind shouldBe Policy.Kind.All | ||
|
||
p = PolicyBuilder().withSignatureOf(kh).withAnySigRequired().build | ||
|
||
p.scripts.toList shouldBe List(Signature(kh)) | ||
p.kind shouldBe Policy.Kind.Any | ||
|
||
p = PolicyBuilder().withSignatureOf(kh).withAtLeastSigsRequired(2).build | ||
|
||
p.scripts.toList shouldBe List(Signature(kh)) | ||
p.kind shouldBe Policy.Kind.AtLeast(2) | ||
} | ||
|
||
it should "aggregrate signatures" in { | ||
val kh = KeyHash[Verification]("somestring") | ||
val kh2 = KeyHash[Verification]("somestring2") | ||
val p = PolicyBuilder() | ||
.withSignatureOf(kh) | ||
.withSignatureOf(kh2) | ||
.build | ||
|
||
p.scripts.toList should contain (Signature(kh)) | ||
p.scripts.toList should contain (Signature(kh2)) | ||
|
||
} | ||
|
||
it should "respect slot bounds" in { | ||
val kh = KeyHash[Verification]("somestring") | ||
|
||
val p = PolicyBuilder() | ||
.withSignatureOf(kh) | ||
.withAfterConstraint(400) | ||
.withBeforeConstraint(800) | ||
.build | ||
|
||
p.scripts.toList should contain (Signature(kh)) | ||
p.scripts.toList should contain (Bound(400, after = true)) | ||
p.scripts.toList should contain (Bound(800, after = false)) | ||
p.scripts.toList.size shouldBe(3) | ||
} | ||
|
||
it should "only allow one before and one after slot bound" in { | ||
val kh = KeyHash[Verification]("somestring") | ||
|
||
val p = PolicyBuilder() | ||
.withSignatureOf(kh) | ||
.withAfterConstraint(400) | ||
.withAfterConstraint(200) | ||
.withBeforeConstraint(800) | ||
.withBeforeConstraint(900) | ||
.build | ||
|
||
p.scripts.toList should contain (Signature(kh)) | ||
p.scripts.toList should contain (Bound(200, after = true)) | ||
p.scripts.toList should contain (Bound(900, after = false)) | ||
p.scripts.toList.size shouldBe(3) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters