Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added observability to the DependencyAudit contract #36

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion contracts/DependencyAudit.cdc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some comments to these functions to explain what they do? Hard to understand if you're not already familiar with it

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
Loading