Skip to content

Commit

Permalink
Merge pull request #36 from janezpodhostnik/janez/add-observabilitiy
Browse files Browse the repository at this point in the history
Added observability to the DependencyAudit contract
  • Loading branch information
sisyphusSmiling authored Jul 26, 2024
2 parents 0a0d1d8 + 8e4e266 commit 5f45a7f
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 4 deletions.
34 changes: 33 additions & 1 deletion contracts/DependencyAudit.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,42 @@ access(all) contract DependencyAudit {
}
}

access(self) fun getBoundaries(): Boundaries? {
access(all) fun getBoundaries(): Boundaries? {
return self.account.copy<Boundaries>(from: /storage/flowDependencyAuditBoundaries)
}

access(all) fun getCurrentFailureProbability(): UFix64 {
if !DependencyAudit.panicOnUnstaged {
return 0.0 as UFix64
}

let maybeBoundaries = self.getBoundaries()
if maybeBoundaries == nil {
return 1.0 as UFix64
}

let boundaries = maybeBoundaries!

let startBlock: UInt64 = boundaries.start
let endBlock: UInt64 = boundaries.end
let currentBlock: UInt64 = getCurrentBlock().height

if startBlock >= endBlock {
return 1.0 as UFix64
}
if currentBlock >= endBlock {
return 1.0 as UFix64
}
if currentBlock < startBlock {
return 0.0 as UFix64
}

let dif = endBlock - startBlock
let currentDif = currentBlock - startBlock

return UFix64(currentDif) / UFix64(dif)
}

access(self) fun setBoundaries(boundaries: Boundaries) {
self.account.load<Boundaries>(from: /storage/flowDependencyAuditBoundaries)
self.account.save(boundaries, to: /storage/flowDependencyAuditBoundaries)
Expand Down
6 changes: 3 additions & 3 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions scripts/dependency-audit/get_boundaries.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "DependencyAudit"

access(all) fun main(): DependencyAudit.Boundaries? {
return DependencyAudit.getBoundaries()
}
5 changes: 5 additions & 0 deletions scripts/dependency-audit/get_failure_probability.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "DependencyAudit"

access(all) fun main(): UFix64 {
return DependencyAudit.getCurrentFailureProbability()
}
5 changes: 5 additions & 0 deletions scripts/dependency-audit/get_should_panic_on_unstaged.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "DependencyAudit"

access(all) fun main(): Boolean {
return DependencyAudit.panicOnUnstaged
}
9 changes: 9 additions & 0 deletions tests/dependency_audit_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ access(all) fun testBoundaries() {
)
Test.expect(commitResult, Test.beSucceeded())

let probability = (executeScript(
"../scripts/dependency-audit/get_failure_probability.cdc",
[]
).returnValue as! UFix64?)!

// depends on the block, so its better to check the range
Test.expect(probability, Test.beGreaterThan(0.5))
Test.expect(probability, Test.beLessThan(0.7))

var i = 0
var failCount = 0
while i < 10 {
Expand Down

0 comments on commit 5f45a7f

Please sign in to comment.