Skip to content

Commit

Permalink
Fix WID check in permit extend and return
Browse files Browse the repository at this point in the history
Previously, in partial returning permits, the out permit could have a
different WID rather than the registered WID. Now, we check the new
permit WID to match with the old spending one.
As a double-check we added the size equality constraint to RWT-repo wid
lists.
  • Loading branch information
fatemeh-ra committed Oct 15, 2023
1 parent 021fbd6 commit 120bf37
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/scala/rosen/bridge/scripts/Permit.es
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Coll(
OUTPUTS(1).tokens(0)._1 == SELF.tokens(0)._1,
OUTPUTS(1).propositionBytes == SELF.propositionBytes,
SELF.R4[Coll[Coll[Byte]]].get == OUTPUTS(1).R4[Coll[Coll[Byte]]].get
)
)
}else{
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/rosen/bridge/scripts/RwtRepo.es
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
repoOut.tokens(0)._2 == repo.tokens(0)._2,
repoOut.tokens(1)._1 == repo.tokens(1)._1,
repoOut.tokens(2)._1 == repo.tokens(2)._1,
repoOut.R4[Coll[Coll[Byte]]].get.size == repoOut.R5[Coll[Long]].get.size,
)
)
if(repo.tokens(1)._2 > repoOut.tokens(1)._2){
Expand Down
51 changes: 51 additions & 0 deletions src/test/scala/contracts/ContractTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ class ContractTest extends TestSuite {
})
}

property("test extend first permit while extended permit wid has changed") {
networkConfig._1.ergoClient.execute(ctx => {
assertThrows[AnyRef] {
val prover = getProver()
val WID = Base16.decode(Boxes.getRandomHexString()).get
val otherWID = Base16.decode(Boxes.getRandomHexString()).get
val userBox = Boxes.createBoxForUser(ctx, prover.getAddress, 1e9.toLong, new ErgoToken(WID, 1L), new ErgoToken(networkConfig._3.RSN, 100L))
val repoBox = Boxes.createRepo(ctx, 100000, 6459L, Seq(WID, otherWID), Seq(58L, 6400L)).convertToInputWith(Boxes.getRandomHexString(), 0)
val repoOut = Boxes.createRepoWithR7(ctx, 99900, 6559L, Seq(WID, otherWID), Seq(158L, 6400L), 1)
val permitBox = Boxes.createPermitBox(ctx, 100L, otherWID)
val WIDBox = Boxes.createBoxCandidateForUser(ctx, prover.getAddress, Configs.minBoxValue, new ErgoToken(WID, 1L))
val tx = ctx.newTxBuilder().addInputs(repoBox, userBox)
.fee(Configs.fee)
.addOutputs(repoOut, permitBox, WIDBox)
.sendChangeTo(prover.getAddress)
.build()
val signedTx = prover.sign(tx)
println(signedTx.toJson(false))
}
})
}

property("test extend first permit while extended permit first token is not RWT") {
networkConfig._1.ergoClient.execute(ctx => {
assertThrows[AnyRef] {
Expand Down Expand Up @@ -269,6 +291,35 @@ class ContractTest extends TestSuite {
})
}

property("test partially return permits when output permit WID has changed") {
networkConfig._1.ergoClient.execute(ctx => {
assertThrows[AnyRef] {
val prover = getProver()
val userWID = Base16.decode(Boxes.getRandomHexString()).get
val otherWID = Base16.decode(Boxes.getRandomHexString()).get
val WIDs = Seq(
Base16.decode(Boxes.getRandomHexString()).get,
otherWID,
userWID,
Base16.decode(Boxes.getRandomHexString()).get
)
val repoBox = Boxes.createRepo(ctx, 100000, 321L, WIDs, Seq(100L, 120L, 60L, 40L)).convertToInputWith(Boxes.getRandomHexString(), 0)
val permitBox = Boxes.createPermitBox(ctx, 60L, userWID).convertToInputWith(Boxes.getRandomHexString(), 0)
val WIDBox = Boxes.createBoxForUser(ctx, prover.getAddress, 1e9.toLong, new ErgoToken(userWID, 1L))
val repoOut = Boxes.createRepoWithR7(ctx, 100020, 301L, WIDs, Seq(100L, 120L, 40L, 40L), 3)
val permitOut = Boxes.createPermitBox(ctx, 40L, otherWID)
val userOut = Boxes.createBoxCandidateForUser(ctx, prover.getAddress, 1e8.toLong, new ErgoToken(userWID, 1), new ErgoToken(networkConfig._3.RSN, 20))
val tx = ctx.newTxBuilder().addInputs(repoBox, permitBox, WIDBox)
.fee(Configs.fee)
.addOutputs(repoOut, permitOut, userOut)
.sendChangeTo(prover.getAddress)
.build()
val signedTx = prover.sign(tx)
println(signedTx.toJson(false))
}
})
}

property("test complete return permits") {
networkConfig._1.ergoClient.execute(ctx => {
var userIndex = 0
Expand Down

0 comments on commit 120bf37

Please sign in to comment.