Skip to content

Commit

Permalink
ostats and pwrreport tests working, need to test table cleanup and th…
Browse files Browse the repository at this point in the history
…en ready
  • Loading branch information
boid-com committed Feb 12, 2024
1 parent c4264fe commit c11d6b7
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 149 deletions.
52 changes: 21 additions & 31 deletions src/assembly/actions/3-pwrreport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,48 +219,38 @@ export class PwrReportActions extends OracleActions {
const global = this.globalT.get()
this.updateStats(global)

let mergedRow = targetReports[0]
let medianUnits:u32 = targetReports[0].report.units
if (targetReports.length > 1) {
// find the median
targetReports.sort((a:PwrReportRow, b:PwrReportRow) => a.report.units - b.report.units)
let half = i32(Math.floor(targetReports.length / 2))
let medianUnits:u32 = 0
// find the median
targetReports.sort((a:PwrReportRow, b:PwrReportRow) => a.report.units - b.report.units)
let half = i32(Math.floor(targetReports.length / 2))

if (targetReports.length % 2) medianUnits = targetReports[half].report.units
else medianUnits = u32((targetReports[half - 1].report.units + targetReports[half].report.units) / 2)
if (targetReports.length % 2) medianUnits = targetReports[half].report.units
else medianUnits = u32((targetReports[half - 1].report.units + targetReports[half].report.units) / 2)

//find safe min/max values
const safeAmount = Math.max(f32(medianUnits) * config.consensus.merge_deviation_pct, 1)
const safeMax = u32(medianUnits + safeAmount)
check(safeMax >= medianUnits, "safeMax max reached")
const safeMin = u32(Math.max(f32(medianUnits) - safeAmount, 1))
//find safe min/max values
const safeAmount = Math.max(f32(medianUnits) * config.consensus.merge_deviation_pct, 1)
const safeMax = u32(medianUnits + safeAmount)
check(safeMax >= medianUnits, "safeMax max reached")
const safeMin = u32(Math.max(f32(medianUnits) - safeAmount, 1))

// ensure each report is a safe range from the median
for (let i = 0; i < targetReports.length; i++) {
const pwrReport = targetReports[i]
check(pwrReport.report.units <= safeMax, "report units " + pwrReport.report.units.toString() + " above maximum: " + safeMax.toString())
check(pwrReport.report.units >= safeMin, "report units " + pwrReport.report.units.toString() + " below required minimum: " + safeMin.toString())
}
mergedRow = targetReports[half]
// ensure each report is a safe range from the median
for (let i = 0; i < targetReports.length; i++) {
const pwrReport = targetReports[i]
check(pwrReport.report.units <= safeMax, "report units " + pwrReport.report.units.toString() + " above maximum: " + safeMax.toString())
check(pwrReport.report.units >= safeMin, "report units " + pwrReport.report.units.toString() + " below required minimum: " + safeMin.toString())
}
const mergedRow = targetReports[half]

// aggregate weights and see if it's above the minimum
aggregateWeight = targetReports.reduce((a:u16, b:PwrReportRow) => a + b.approval_weight, u16(0))
print("\n min threshold: " + this.minWeightThreshold(config, global).toString())
print("\n aggregate: " + aggregateWeight.toString())
check(aggregateWeight >= this.minWeightThreshold(config, global), "aggregate approval_weight isn't high enough " + this.minWeightThreshold(config, global).toString() + " " + aggregateWeight.toString())

// create or update merged report
if (targetReports.length % 2) {
mergedRow.reported = true
if (targetReports.length > 1) {
mergedRow.approvals.push(Name.fromString("merged.boid"))
}
} else {
const newReport:PwrReport = { protocol_id: u8(targetProtocol), round: u16(targetRound), units: medianUnits }
mergedRow = new PwrReportRow(Name.fromString("merged.boid"), newReport, [Name.fromString("merged.boid")], aggregateWeight, true, false)
this.pwrReportsT(boid_id_scope).store(mergedRow, this.receiver)
}
mergedRow.reported = true
mergedRow.merged = true
mergedRow.approvals.push(Name.fromString("merged.boid"))

this.sendReport(boid_id_scope, mergedRow.report)

let allOracles:Name[] = []
Expand Down
35 changes: 15 additions & 20 deletions tests/ostats.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { expectToThrow } from "@proton/vert"
import { beforeEach, describe, it } from "mocha"
import { act, addRounds, boid_id, chain, getReportId, init, oracleStats, setupOracle, global, oracle, reports, findRoundPayout } from "./util.js"
import { expect } from "chai"

const report = { protocol_id: 0, round: 10, units: 100 }
const report2 = { protocol_id: 0, round: 10, units: 100 }
Expand All @@ -12,7 +13,7 @@ beforeEach(async () => {
await init()
})
describe("reports", async() => {
describe("handleostat", async() => {
describe("payoutround", async() => {
it("Success", async() => {
await setupOracle("oracle1")
await act("protoset", { protocol: { protocol_id: 0, protocol_name: "testproto", unitPowerMult: 1, active:true } })
Expand Down Expand Up @@ -43,16 +44,11 @@ describe("reports", async() => {
await act("payoutround", { round: 10 ,oracle:"oracle3" }, "oracle1")
await act("payoutround", { round: 10 ,oracle:"oracle4" }, "oracle1")
await act("payoutround", { round: 10 ,oracle:"oracle5" }, "oracle1")
console.log('oracle1 earned:',oracle("oracle1").funds.unclaimed)
console.log('oracle2 earned:',oracle("oracle2").funds.unclaimed)
console.log('oracle3 earned:',oracle("oracle3").funds.unclaimed)
console.log('oracle4 earned:',oracle("oracle4").funds.unclaimed)
console.log('oracle5 earned:',oracle("oracle5").funds.unclaimed)
console.log('o1 payout estimate:',findRoundPayout("oracle1", 10))
console.log('o2 payout estimate:',findRoundPayout("oracle2", 10))
console.log('o3 payout estimate:',findRoundPayout("oracle3", 10))
console.log('o4 payout estimate:',findRoundPayout("oracle4", 10))
console.log('o5 payout estimate:',findRoundPayout("oracle5", 10))
expect(oracle("oracle1").funds.unclaimed).eq(findRoundPayout("oracle1", 10))
expect(oracle("oracle2").funds.unclaimed).eq(findRoundPayout("oracle2", 10))
expect(oracle("oracle3").funds.unclaimed).eq(findRoundPayout("oracle3", 10))
expect(oracle("oracle4").funds.unclaimed).eq(findRoundPayout("oracle4", 10))
expect(oracle("oracle5").funds.unclaimed).eq(findRoundPayout("oracle5", 10))
addRounds(1)
await act("pwrreport", { oracle: "oracle1", boid_id_scope: boid_id, report:report3 }, "oracle1")
await act("pwrreport", { oracle: "oracle2", boid_id_scope: boid_id, report:report3 }, "oracle2")
Expand All @@ -63,15 +59,14 @@ describe("reports", async() => {
await act("payoutround", { protocol_id: 0, round: 13 ,oracle:"oracle1" }, "oracle1")
await act("payoutround", { protocol_id: 0, round: 13 ,oracle:"oracle2" }, "oracle1")
await act("payoutround", { protocol_id: 0, round: 13 ,oracle:"oracle3" }, "oracle1")
console.log('oracle1 earned:',oracle("oracle1").funds.unclaimed)
console.log('oracle2 earned:',oracle("oracle2").funds.unclaimed)
console.log('oracle3 earned:',oracle("oracle2").funds.unclaimed)
console.log(chain.console)





await act("withdrawinit",["oracle1"],"oracle1")
await act("withdrawinit",["oracle2"],"oracle2")
await act("withdrawinit",["oracle3"],"oracle3")
await act("withdrawinit",["oracle4"],"oracle4")
await act("withdrawinit",["oracle5"],"oracle5")
addRounds(6)
await act("withdraw",["oracle1"],"oracle1")
expect(oracle("oracle1").funds.unclaimed).eq(0)
})
})
})
Loading

0 comments on commit c11d6b7

Please sign in to comment.