Skip to content

Commit

Permalink
new payoutround action
Browse files Browse the repository at this point in the history
  • Loading branch information
boid-com committed Feb 12, 2024
1 parent b598dbc commit c4264fe
Show file tree
Hide file tree
Showing 23 changed files with 962 additions and 530 deletions.
9 changes: 7 additions & 2 deletions src/assembly/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowExpressions": true,
"allowTypedFunctionExpressions": true
}
],
"spaced-comment": "off",
"key-spacing": [
"error",
Expand All @@ -37,7 +44,6 @@
"after": false
}
],
"@typescript-eslint/explicit-function-return-type": "error",
"space-before-function-paren": [
"error",
"never"
Expand All @@ -57,5 +63,4 @@
"./external/*",
"./util/*"
]

}
19 changes: 10 additions & 9 deletions src/assembly/actions/1-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class GlobalActions extends Contract {

/** Determine the minimum weight required for consensus */
minWeightThreshold(config:Config = this.getConfig(), global:Global = this.globalT.get()):u16 {
return u16(Math.min(u32(Math.max(global.expected_active_weight * config.consensus.min_pct, config.consensus.min_weight)), u16.MAX_VALUE))
return u16(Math.min(u32(Math.max(global.expected_active_weight * config.consensus.min_weight_pct, config.consensus.min_weight)), u16.MAX_VALUE))
}

/** Adds active oracle to global row, does not update table */
Expand Down Expand Up @@ -112,18 +112,19 @@ export class GlobalActions extends Contract {
configSet(config:Config):void {
requireAuth(this.receiver)
check(config.slashLow.slash_quantity_collateral_pct >= 0, "slash_quantity_collateral_pct must be greater than or equal zero")
check(config.slashLow.slash_quantity_collateral_pct <= f32(1), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.slashLow.slash_quantity_collateral_pct <= f32(100), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.slashMed.slash_quantity_collateral_pct >= 0, "slash_quantity_collateral_pct must be greater than or equal zero")
check(config.slashMed.slash_quantity_collateral_pct <= f32(1), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.slashMed.slash_quantity_collateral_pct <= f32(100), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.slashHigh.slash_quantity_collateral_pct >= 0, "slash_quantity_collateral_pct must be greater than or equal zero")
check(config.slashHigh.slash_quantity_collateral_pct <= f32(1), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.slashHigh.slash_quantity_collateral_pct <= f32(100), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.reports_accumulate_weight_round_pct >= 0, "reports_accumulate_weight_round_pct must be higher or equal zero")
check(config.reports_accumulate_weight_round_pct <= f32(1), "reports_accumulate_weight_round_pct must be less or equal to 100%")
check(config.payment.collateral_pct_pay_per_round >= 0, "collateral_pct_pay_per_round must be higher or equal zero")
check(config.consensus.min_pct >= 0, "min_consensus_pct must be higher or equal zero")
check(config.consensus.min_pct <= f32(1), "min_consensus_pct must be less or equal to 100%")
check(config.merge_deviation_pct >= 0, "merge_deviation_pct must be higher or equal zero")
check(config.merge_deviation_pct <= f32(1), "merge_deviation_pct must be less or equal to 100%")
check(config.payment.collateral_pct_pay_per_round_mult >= 0, "collateral_pct_pay_per_round_mult must be higher or equal zero")
check(config.payment.collateral_pct_pay_per_round_mult <= 1, "collateral_pct_pay_per_round_mult must be less")
check(config.consensus.min_weight_pct >= 0, "min_consensus_pct must be higher or equal zero")
check(config.consensus.min_weight_pct <= f32(1), "min_consensus_pct must be less or equal to 100%")
check(config.consensus.merge_deviation_pct >= 0, "merge_deviation_pct must be higher or equal zero")
check(config.consensus.merge_deviation_pct <= f32(1), "merge_deviation_pct must be less or equal to 100%")
check(config.min_pay_report_share_threshold >= 0, "min_pay_report_share_threshold must be higher or equal zero")
check(config.min_pay_report_share_threshold <= f32(1), "min_pay_report_share_threshold must be less or equal to 100%")
this.configT.set(config, this.receiver)
Expand Down
10 changes: 9 additions & 1 deletion src/assembly/actions/2-oracle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, ActionData, check, EMPTY_NAME, hasAuth, isAccount, Name, requireAuth } from "proton-tsc"
import { Action, ActionData, check, EMPTY_NAME, hasAuth, isAccount, Name, requireAuth, SAME_PAYER } from "proton-tsc"
import { Oracle, OracleCollateral, OracleFunds } from "../tables/oracles"
import { GlobalActions } from "./1-global"

Expand Down Expand Up @@ -64,6 +64,14 @@ export class OracleActions extends GlobalActions {
action.send()
}

@action("setweight")
setWeight(oracle:Name, weight:u8):void {
const oracleRow = this.oraclesT.requireGet(oracle.value, "oracle doesn't exist")
check(oracleRow.standby, "oracle must be in standby to set weight")
oracleRow.weight = weight
this.oraclesT.update(oracleRow, SAME_PAYER)
}

/**
* Action called by oracles or the contract to set an oracle in/out of standby mode.
*
Expand Down
55 changes: 43 additions & 12 deletions src/assembly/actions/3-pwrreport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export class PwrReportActions extends OracleActions {
let oRoundCommit = this.roundCommitT(oracle)
let commitExists = oRoundCommit.getBySecondaryU128(RoundCommit.getByRoundProtocolBoidId(boid_id_scope, report.protocol_id, report.round), 1)
check(commitExists == null, "oracle already commited report for this user and round")

const activeRound = this.currentRound() - 1
// ensure the report is for a round that is valid
check(report.round < this.currentRound(), "report round must target a past round")
check(report.round == this.currentRound() - 1, "round is too far in the past")
check(report.round == activeRound, "report round is too far in the past, must report for round: " + activeRound.toString())

// ensure the oracle can make reports
const oracleRow = this.oraclesT.requireGet(oracle.value, "oracle not registered")
Expand Down Expand Up @@ -110,14 +110,15 @@ export class PwrReportActions extends OracleActions {

// if report was sent, we need to update the stats for all oracles that participated
if (reportSent) {
if (!existing) return
for (let i = 0; i < existing.approvals.length; i++) {
const oracleName = existing.approvals[i]
let row:Oracle|null = null
if (oracleName == oracle) row = oracleRow
else row = this.oraclesT.get(oracleName.value)
if (!row) continue
else this.updateOracleStats(row, reportSent, false, report.round)
if (existing) {
for (let i = 0; i < existing.approvals.length; i++) {
const oracleName = existing.approvals[i]
let row:Oracle|null = null
if (oracleName == oracle) row = oracleRow
else row = this.oraclesT.get(oracleName.value)
if (!row) continue
else this.updateOracleStats(row, reportSent, false, report.round)
}
}
} else {
// otherwise we just need to update our own oracle stats
Expand Down Expand Up @@ -162,7 +163,37 @@ export class PwrReportActions extends OracleActions {
* @param {u64[]} pwrreport_ids a vector of reports that could be combined
*/
@action("finishreport")
finishReport(boid_id_scope:Name, pwrreport_ids:u64[]):void {
finishReport(boid_id_scope:Name, pwrreport_id:u64):void {
const config = this.getConfig()
const pwrReport = this.pwrReportsT(boid_id_scope).requireGet(pwrreport_id, "invalid id provided")
check(!pwrReport.reported, "can't merge reports already reported")
check(!pwrReport.merged, "can't merge reports already merged")
check(this.shouldFinalizeReports(u16(pwrReport.report.round), config), "can't finalize/merge reports this early in a round")
const global = this.globalT.get()
const minThreshold = this.minWeightThreshold(config, global)
check(pwrReport.approval_weight >= minThreshold, "aggregate approval_weight isn't high enough. Minimum:" + minThreshold.toString() + " report has:" + pwrReport.approval_weight.toString())
this.sendReport(boid_id_scope, pwrReport.report)
pwrReport.reported = true
this.pwrReportsT(boid_id_scope).update(pwrReport, SAME_PAYER)

const oStatsT = this.oracleStatsT(pwrReport.proposer)
for (let i = 0; i < pwrReport.approvals.length; i++) {
const oracleName = pwrReport.approvals[i]
let row:Oracle|null = null
row = this.oraclesT.get(oracleName.value)
if (!row) continue
else this.updateOracleStats(row, true, false, pwrReport.report.round)
}
global.reports.reported++
global.reports.unreported_and_unmerged--
// this saves the modified global if stats needs updating, otherwise we save global directly
const saved = this.updateStats(global, config)
if (!saved) this.globalT.set(global, SAME_PAYER)
}

@action("mergereports")
mergeReports(boid_id_scope:Name, pwrreport_ids:u64[]):void {
check(pwrreport_ids.length > 1, "must provide at least two reports to merge")
const config = this.getConfig()
const targetReports:PwrReportRow[] = []
let targetProtocol:i16 = -1
Expand Down Expand Up @@ -199,7 +230,7 @@ export class PwrReportActions extends OracleActions {
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.merge_deviation_pct, 1)
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))
Expand Down
Loading

0 comments on commit c4264fe

Please sign in to comment.