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

feat: allow calculating diffs between two row consumptions #980

Merged
merged 1 commit into from
Aug 12, 2024
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
36 changes: 36 additions & 0 deletions core/types/row_consumption.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "slices"

type RowUsage struct {
IsOk bool `json:"is_ok"`
RowNumber uint64 `json:"row_number"`
Expand All @@ -12,4 +14,38 @@ type SubCircuitRowUsage struct {
RowNumber uint64 `json:"row_number" gencodec:"required"`
}

// RowConsumptionLimit is the max number of row we support per subcircuit
const RowConsumptionLimit = 1_000_000

type RowConsumption []SubCircuitRowUsage

// IsOverflown returns if any subcircuits are overflown
func (rc RowConsumption) IsOverflown() bool {
return slices.ContainsFunc(rc, func(scru SubCircuitRowUsage) bool {
return scru.RowNumber > RowConsumptionLimit
})
}

// Difference returns rc - other
// Assumes that rc > other for all subcircuits
HAOYUatHZ marked this conversation as resolved.
Show resolved Hide resolved
func (rc RowConsumption) Difference(other RowConsumption) RowConsumption {
subCircuitMap := make(map[string]uint64, len(rc))
for _, detail := range rc {
subCircuitMap[detail.Name] = detail.RowNumber
}

for _, detail := range other {
subCircuitMap[detail.Name] -= detail.RowNumber
}

diff := make([]SubCircuitRowUsage, 0, len(subCircuitMap))
for name, rowNumDiff := range subCircuitMap {
if rowNumDiff > 0 {
diff = append(diff, SubCircuitRowUsage{
Name: name,
RowNumber: rowNumDiff,
})
}
}
return diff
}
80 changes: 80 additions & 0 deletions core/types/row_consumption_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package types

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRowConsumptionDifference(t *testing.T) {
tests := []struct {
rc1 RowConsumption
rc2 RowConsumption
expected RowConsumption
}{
{
rc1: RowConsumption{
SubCircuitRowUsage{
"sc1",
123,
},
SubCircuitRowUsage{
"sc2",
456,
},
},
rc2: RowConsumption{
SubCircuitRowUsage{
"sc2",
111,
},
},
expected: RowConsumption{
SubCircuitRowUsage{
"sc1",
123,
},
SubCircuitRowUsage{
"sc2",
345,
},
},
},
{
rc1: RowConsumption{
SubCircuitRowUsage{
"sc1",
123,
},
SubCircuitRowUsage{
"sc2",
456,
},
},
rc2: RowConsumption{
SubCircuitRowUsage{
"sc2",
456,
},
},
expected: RowConsumption{
SubCircuitRowUsage{
"sc1",
123,
},
},
},
}

makeMap := func(rc RowConsumption) map[string]uint64 {
m := make(map[string]uint64)
for _, usage := range rc {
m[usage.Name] = usage.RowNumber
}
return m
}

for _, test := range tests {
assert.Equal(t, makeMap(test.expected), makeMap(test.rc1.Difference(test.rc2)))
}
}
Loading