Skip to content

Commit

Permalink
Fix RwtRepo register extending with arbitrary data
Browse files Browse the repository at this point in the history
Previously, the RwtRepo registers could be extended with arbitrary data
while returning the total permit of a watcher. Now, the issue is
resolved by the similar length constraint and checking the register size
to be decreased by one.
  • Loading branch information
fatemeh-ra committed Oct 15, 2023
1 parent 120bf37 commit 3996784
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/main/scala/rosen/bridge/scripts/RwtRepo.es
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@
allOf(
Coll(
permitCreation,
repoOut.R4[Coll[Coll[Byte]]].get.size == widListSize + 1,
widOutListSize == widListSize + 1,
repoOut.R4[Coll[Coll[Byte]]].get.slice(0, widOutListSize - 1) == repo.R4[Coll[Coll[Byte]]].get,
repoOut.R4[Coll[Coll[Byte]]].get(widOutListSize - 1) == repo.id,
repoOut.R5[Coll[Long]].get.size == widListSize + 1,
repoOut.R5[Coll[Long]].get.slice(0, widOutListSize - 1) == repo.R5[Coll[Long]].get,
repoOut.R5[Coll[Long]].get(widOutListSize - 1) == RWTOut,
permit.R4[Coll[Coll[Byte]]].get == Coll(repo.id),
Expand Down Expand Up @@ -105,7 +104,6 @@
val permit = INPUTS(1)
val RWTIn = repoOut.tokens(1)._2 - repo.tokens(1)._2
val WIDIndex = repoOut.R7[Int].get
val watcherCount = repo.R5[Coll[Long]].get.size
val WIDCheckInRepo = if(repo.R5[Coll[Long]].get(WIDIndex) > RWTIn) {
// Returning some RWTs
// [repo, Permit, WIDToken] => [repo, Permit(Optional), WIDToken(+userChange)]
Expand All @@ -115,7 +113,7 @@
repo.R5[Coll[Long]].get(WIDIndex) == repoOut.R5[Coll[Long]].get(WIDIndex) + RWTIn,
repo.R4[Coll[Coll[Byte]]].get == repoOut.R4[Coll[Coll[Byte]]].get,
repo.R5[Coll[Long]].get.slice(0, WIDIndex) == repoOut.R5[Coll[Long]].get.slice(0, WIDIndex),
repo.R5[Coll[Long]].get.slice(WIDIndex + 1, watcherCount) == repoOut.R5[Coll[Long]].get.slice(WIDIndex + 1, watcherCount)
repo.R5[Coll[Long]].get.slice(WIDIndex + 1, widListSize) == repoOut.R5[Coll[Long]].get.slice(WIDIndex + 1, widListSize)
)
)
}else{
Expand All @@ -124,11 +122,12 @@
val watcherCollateral = INPUTS(3);
allOf(
Coll(
widOutListSize == widListSize - 1,
repo.R5[Coll[Long]].get(WIDIndex) == RWTIn,
repo.R4[Coll[Coll[Byte]]].get.slice(0, WIDIndex) == repoOut.R4[Coll[Coll[Byte]]].get.slice(0, WIDIndex),
repo.R4[Coll[Coll[Byte]]].get.slice(WIDIndex + 1, watcherCount) == repoOut.R4[Coll[Coll[Byte]]].get.slice(WIDIndex, watcherCount - 1),
repo.R4[Coll[Coll[Byte]]].get.slice(WIDIndex + 1, widListSize) == repoOut.R4[Coll[Coll[Byte]]].get.slice(WIDIndex, widOutListSize),
repo.R5[Coll[Long]].get.slice(0, WIDIndex) == repoOut.R5[Coll[Long]].get.slice(0, WIDIndex),
repo.R5[Coll[Long]].get.slice(WIDIndex + 1, watcherCount) == repoOut.R5[Coll[Long]].get.slice(WIDIndex, watcherCount - 1),
repo.R5[Coll[Long]].get.slice(WIDIndex + 1, widListSize) == repoOut.R5[Coll[Long]].get.slice(WIDIndex, widOutListSize),
blake2b256(watcherCollateral.propositionBytes) == watcherCollateralScriptHash,
)
)
Expand Down
81 changes: 81 additions & 0 deletions src/test/scala/contracts/ContractTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,87 @@ class ContractTest extends TestSuite {
})
}

property("test complete return permit while extending the wid list in repo") {
networkConfig._1.ergoClient.execute(ctx => {
assertThrows[AnyRef] {
val prover = getProver()
val WIDs = generateRandomWIDList(6)
val amounts = Seq(100L, 120L, 140L, 20L, 40L, 250L)
val userIndex = 0
val userWID = WIDs(userIndex)
val totalPermitOut = amounts.sum
val repoBox = Boxes.createRepo(ctx, 100000L, totalPermitOut + 1L, WIDs, amounts).convertToInputWith(Boxes.getRandomHexString(), 0)
val permitBox = Boxes.createPermitBox(ctx, amounts(userIndex), userWID).convertToInputWith(Boxes.getRandomHexString(), 0)
val WIDBox = Boxes.createBoxForUser(ctx, prover.getAddress, 1e9.toLong, new ErgoToken(userWID, 1L))
val outputWIDs = WIDs.take(userIndex) ++ WIDs.drop(userIndex + 1) ++ Seq(WIDs(userIndex))
val outAmounts = amounts.take(userIndex) ++ amounts.drop(userIndex + 1)
val repoOut = Boxes.createRepoWithR7(ctx, 100000L + amounts(userIndex), (totalPermitOut - amounts(userIndex)) + 1, outputWIDs, outAmounts, userIndex + 1) // 4 + first element in WID list is chain name
val userOut = Boxes.createBoxCandidateForUser(ctx, prover.getAddress, 1e8.toLong, new ErgoToken(userWID, 1), new ErgoToken(networkConfig._3.RSN, amounts(userIndex)))
val watcherCollateral = Boxes.createWatcherCollateralBoxInput(ctx,1e9.toLong, 100, userWID)
val tx = ctx.newTxBuilder().addInputs(repoBox, permitBox, WIDBox, watcherCollateral)
.fee(Configs.fee)
.addOutputs(repoOut, userOut)
.sendChangeTo(prover.getAddress)
.build()
prover.sign(tx)
}
})
}

property("test complete return permit while extending the rwt count list in repo") {
networkConfig._1.ergoClient.execute(ctx => {
assertThrows[AnyRef] {
val prover = getProver()
val WIDs = generateRandomWIDList(6)
val amounts = Seq(100L, 120L, 140L, 20L, 40L, 250L)
val userIndex = 0
val userWID = WIDs(userIndex)
val totalPermitOut = amounts.sum
val repoBox = Boxes.createRepo(ctx, 100000L, totalPermitOut + 1L, WIDs, amounts).convertToInputWith(Boxes.getRandomHexString(), 0)
val permitBox = Boxes.createPermitBox(ctx, amounts(userIndex), userWID).convertToInputWith(Boxes.getRandomHexString(), 0)
val WIDBox = Boxes.createBoxForUser(ctx, prover.getAddress, 1e9.toLong, new ErgoToken(userWID, 1L))
val outputWIDs = WIDs.take(userIndex) ++ WIDs.drop(userIndex + 1)
val outAmounts = amounts.take(userIndex) ++ amounts.drop(userIndex + 1) ++ Seq(100L)
val repoOut = Boxes.createRepoWithR7(ctx, 100000L + amounts(userIndex), (totalPermitOut - amounts(userIndex)) + 1, outputWIDs, outAmounts, userIndex + 1) // 4 + first element in WID list is chain name
val userOut = Boxes.createBoxCandidateForUser(ctx, prover.getAddress, 1e8.toLong, new ErgoToken(userWID, 1), new ErgoToken(networkConfig._3.RSN, amounts(userIndex)))
val watcherCollateral = Boxes.createWatcherCollateralBoxInput(ctx, 1e9.toLong, 100, userWID)
val tx = ctx.newTxBuilder().addInputs(repoBox, permitBox, WIDBox, watcherCollateral)
.fee(Configs.fee)
.addOutputs(repoOut, userOut)
.sendChangeTo(prover.getAddress)
.build()
prover.sign(tx)
}
})
}

property("test complete return permit while extending both rwt count and wid list in repo") {
networkConfig._1.ergoClient.execute(ctx => {
assertThrows[AnyRef] {
val prover = getProver()
val WIDs = generateRandomWIDList(6)
val amounts = Seq(100L, 120L, 140L, 20L, 40L, 250L)
val userIndex = 0
val userWID = WIDs(userIndex)
val totalPermitOut = amounts.sum
val repoBox = Boxes.createRepo(ctx, 100000L, totalPermitOut + 1L, WIDs, amounts).convertToInputWith(Boxes.getRandomHexString(), 0)
val permitBox = Boxes.createPermitBox(ctx, amounts(userIndex), userWID).convertToInputWith(Boxes.getRandomHexString(), 0)
val WIDBox = Boxes.createBoxForUser(ctx, prover.getAddress, 1e9.toLong, new ErgoToken(userWID, 1L))
val outputWIDs = WIDs.take(userIndex) ++ WIDs.drop(userIndex + 1) ++ Seq(WIDs(userIndex))
val outAmounts = amounts.take(userIndex) ++ amounts.drop(userIndex + 1) ++ Seq(100L)
val repoOut = Boxes.createRepoWithR7(ctx, 100000L + amounts(userIndex), (totalPermitOut - amounts(userIndex)) + 1, outputWIDs, outAmounts, userIndex + 1) // 4 + first element in WID list is chain name
val userOut = Boxes.createBoxCandidateForUser(ctx, prover.getAddress, 1e8.toLong, new ErgoToken(userWID, 1), new ErgoToken(networkConfig._3.RSN, amounts(userIndex)))
val watcherCollateral = Boxes.createWatcherCollateralBoxInput(ctx, 1e9.toLong, 100, userWID)
val tx = ctx.newTxBuilder().addInputs(repoBox, permitBox, WIDBox, watcherCollateral)
.fee(Configs.fee)
.addOutputs(repoOut, userOut)
.sendChangeTo(prover.getAddress)
.build()
prover.sign(tx)
}
})
}

property("test complete return of last permit") {
networkConfig._1.ergoClient.execute(ctx => {
try {
Expand Down

0 comments on commit 3996784

Please sign in to comment.