Skip to content

Commit

Permalink
Merge branch '36-replace-wid-list-digest' into 'dev'
Browse files Browse the repository at this point in the history
Replace wid list with its digest in trigger

Closes #36

See merge request ergo/rosen-bridge/contract!76
  • Loading branch information
vorujack committed Jan 17, 2024
2 parents 391f0d4 + 1614a7d commit 3274683
Show file tree
Hide file tree
Showing 4 changed files with 636 additions and 437 deletions.
40 changes: 25 additions & 15 deletions src/main/scala/rosen/bridge/scripts/Commitment.es
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@

val eventTriggerHash = fromBase64("EVENT_TRIGGER_SCRIPT_HASH");
val repoNFT = fromBase64("REPO_NFT");
val event = if (blake2b256(INPUTS(0).propositionBytes) == eventTriggerHash) INPUTS(0) else OUTPUTS(0)
val trigger = if (blake2b256(INPUTS(0).propositionBytes) == eventTriggerHash) INPUTS(0) else OUTPUTS(0)
val myWID = SELF.R4[Coll[Coll[Byte]]].get
val WIDs = event.R4[Coll[Coll[Byte]]].get
val paddedData = event.R5[Coll[Coll[Byte]]].get.fold(Coll(0.toByte), { (a: Coll[Byte], b: Coll[Byte]) => a ++ b } )
val eventData = paddedData.slice(1, paddedData.size)
val eventData = trigger.R5[Coll[Coll[Byte]]].get.fold(Coll[Byte](), {(a: Coll[Byte], b: Coll[Byte]) => a ++ b })
if(blake2b256(INPUTS(0).propositionBytes) == eventTriggerHash){
// Reward Distribution (for missed commitments)
// [EventTrigger, Commitments[], BridgeWallet] => [WatcherPermits[], BridgeWallet]
val WIDs = OUTPUTS.filter{(box:Box)
=> box.tokens.size > 0 && box.tokens(0)._1 == SELF.tokens(0)._1
}
.slice(0, trigger.R7[Int].get)
.map{(box:Box) => box.R4[Coll[Coll[Byte]]].get(0)}
val permitBox = OUTPUTS.filter {(box:Box) =>
box.R4[Coll[Coll[Byte]]].isDefined &&
box.R4[Coll[Coll[Byte]]].get == myWID
Expand All @@ -39,12 +42,18 @@
} else if (blake2b256(OUTPUTS(0).propositionBytes) == eventTriggerHash){
// Event Trigger Creation
// [Commitments[]] + [Repo(DataInput)] => [EventTrigger]
val commitmentBoxes = INPUTS.filter { (box: Box) => SELF.propositionBytes == box.propositionBytes }
val commitmentBoxes = INPUTS.filter{
(box: Box) =>
SELF.propositionBytes == box.propositionBytes &&
box.tokens.size > 0 &&
box.tokens(0)._1 == SELF.tokens(0)._1
}
val WIDs = commitmentBoxes.map{(box:Box) => box.R4[Coll[Coll[Byte]]].get(0)}
val widListDigest = blake2b256(WIDs.fold(Coll[Byte](), {(a: Coll[Byte], b: Coll[Byte]) => a++b}))
val myWIDCommitments = commitmentBoxes.filter{ (box: Box) => box.R4[Coll[Coll[Byte]]].get == myWID }
val EventBoxErgs = commitmentBoxes.map { (box: Box) => box.value }.fold(0L, { (a: Long, b: Long) => a + b })
val myWIDExists = WIDs.exists{ (WID: Coll[Byte]) => Coll(WID) == myWID }
val repo = CONTEXT.dataInputs(0)
val requestId = blake2b256(event.R5[Coll[Coll[Byte]]].get(0))
val eventId = blake2b256(trigger.R5[Coll[Coll[Byte]]].get(0))
val repoR6 = repo.R6[Coll[Long]].get
val maxCommitment = repoR6(3)
val requiredCommitmentFromFormula: Long = repoR6(2) + repoR6(1) * (repo.R4[Coll[Coll[Byte]]].get.size - 1L) / 100L
Expand All @@ -59,22 +68,23 @@
//check repo
repo.tokens(0)._1 == repoNFT,
repo.tokens(1)._1 == SELF.tokens(0)._1,

OUTPUTS(0).value >= EventBoxErgs,
// prevent duplicate commitments
myWIDCommitments.size == 1,
myWIDExists,
event.R6[Coll[Byte]].get == SELF.R7[Coll[Byte]].get,
WIDs.size == commitmentBoxes.size,
// verify trigger params
trigger.value >= EventBoxErgs,
trigger.R6[Coll[Byte]].get == SELF.R7[Coll[Byte]].get,
trigger.R7[Int].get == commitmentBoxes.size,
trigger.R4[Coll[Coll[Byte]]].get(0) == widListDigest,
// verify commitment to be correct
blake2b256(eventData ++ myWID(0)) == SELF.R6[Coll[Byte]].get,
// check event id
SELF.R5[Coll[Coll[Byte]]].get == Coll(requestId),
SELF.R5[Coll[Coll[Byte]]].get == Coll(eventId),
// check commitment count
commitmentBoxes.size > requiredCommitment,
// Check required RWT
SELF.tokens(0)._2 == repoR6(0),
event.tokens(0)._2 == repoR6(0) * commitmentBoxes.size,
event.tokens(0)._1 == SELF.tokens(0)._1
trigger.tokens(0)._2 == repoR6(0) * commitmentBoxes.size,
trigger.tokens(0)._1 == SELF.tokens(0)._1
)
)
)
Expand Down
26 changes: 16 additions & 10 deletions src/main/scala/rosen/bridge/scripts/EventTrigger.es
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
// ----------------- REGISTERS
// R4: Coll[Coll[Byte]] [WID[]]
// R4: Coll[Coll[Byte]] [WID list digest]
// R5: Coll[Coll[Byte]] Event data
// R6: Coll[Byte] Permit contract script digest
// R7: Int Commitment Count
// ----------------- TOKENS
// 0: RWT

Expand All @@ -28,20 +29,25 @@
)
)
}
val WIDs: Coll[Coll[Byte]] = SELF.R4[Coll[Coll[Byte]]].get
val mergeBoxes = OUTPUTS.slice(0, WIDs.size)
val checkAllWIDs = WIDs.zip(mergeBoxes).forall {
(data: (Coll[Byte], Box)) => {
Coll(data._1) == data._2.R4[Coll[Coll[Byte]]].get &&
data._2.propositionBytes == OUTPUTS(0).propositionBytes &&
data._2.tokens(0)._1 == SELF.tokens(0)._1 &&
data._2.tokens(0)._2 == SELF.tokens(0)._2 / WIDs.size
val commitmentCount = SELF.R7[Int].get
val rewards = OUTPUTS.filter{(box:Box)
=> box.tokens.size > 0 && box.tokens(0)._1 == SELF.tokens(0)._1
}
.slice(0, commitmentCount)
val WIDs = rewards.map{(box:Box) => box.R4[Coll[Coll[Byte]]].get(0)}
val widListDigest = blake2b256(WIDs.fold(Coll[Byte](), {(a: Coll[Byte], b: Coll[Byte]) => a++b}))
val checkAllWIDs = rewards.forall {
(data: Box) => {
data.propositionBytes == OUTPUTS(0).propositionBytes &&
data.tokens(0)._1 == SELF.tokens(0)._1 &&
data.tokens(0)._2 == SELF.tokens(0)._2 / commitmentCount
}
}
sigmaProp(
allOf(
Coll(
WIDs.size == mergeBoxes.size,
rewards.size == commitmentCount,
SELF.R4[Coll[Coll[Byte]]].get(0) == widListDigest,
checkAllWIDs,
fraudScriptCheck,
)
Expand Down
Loading

0 comments on commit 3274683

Please sign in to comment.