Skip to content

Commit

Permalink
Update buildreport to use gasprice answer (#442)
Browse files Browse the repository at this point in the history
* update buildreport to use gasprice answer

* update tests

* fix tests

* fix last relayer test
  • Loading branch information
augustbleeds authored May 31, 2024
1 parent 4bf8c47 commit 99118e4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
Binary file not shown.
15 changes: 9 additions & 6 deletions relayer/pkg/chainlink/ocr2/medianreport/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (c ReportCodec) BuildReport(oo []median.ParsedAttributedObservation) (types
}

for _, o := range oo {
if o.Value.Sign() == -1 || o.JuelsPerFeeCoin.Sign() == -1 {
return nil, fmt.Errorf("starknet does not support negative values: value = (%v), fee = (%v)", o.Value, o.JuelsPerFeeCoin)
if o.Value.Sign() == -1 || o.JuelsPerFeeCoin.Sign() == -1 || o.GasPriceSubunits.Sign() == -1 {
return nil, fmt.Errorf("starknet does not support negative values: value = (%v), fee = (%v), gas = (%v)", o.Value, o.JuelsPerFeeCoin, o.GasPriceSubunits)
}
}

Expand All @@ -60,9 +60,12 @@ func (c ReportCodec) BuildReport(oo []median.ParsedAttributedObservation) (types
juelsPerFeeCoin := oo[num/2].JuelsPerFeeCoin
juelsPerFeeCoinFelt := starknetutils.BigIntToFelt(juelsPerFeeCoin)

// TODO: source from observations
gasPrice := big.NewInt(1) // := oo[num/2].GasPrice
gasPriceFelt := starknetutils.BigIntToFelt(gasPrice)
sort.Slice(oo, func(i, j int) bool {
return oo[i].GasPriceSubunits.Cmp(oo[j].GasPriceSubunits) < 0
})
// gas price in FRI
gasPriceSubunits := oo[num/2].GasPriceSubunits
gasPriceSubunitsFelt := starknetutils.BigIntToFelt(gasPriceSubunits)

// sort by values
sort.Slice(oo, func(i, j int) bool {
Expand Down Expand Up @@ -91,7 +94,7 @@ func (c ReportCodec) BuildReport(oo []median.ParsedAttributedObservation) (types
}
buf = juelsPerFeeCoinFelt.Bytes()
report = append(report, buf[:]...)
buf = gasPriceFelt.Bytes()
buf = gasPriceSubunitsFelt.Bytes()
report = append(report, buf[:]...)

return report, nil
Expand Down
9 changes: 5 additions & 4 deletions relayer/pkg/chainlink/ocr2/medianreport/report_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
func FuzzReportCodecMedianFromReport(f *testing.F) {
cdc := ReportCodec{}
report, err := cdc.BuildReport([]median.ParsedAttributedObservation{
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(100000)},
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(200000)},
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(11), JuelsPerFeeCoin: big.NewInt(300000)}})
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(100000), GasPriceSubunits: big.NewInt(100000)},
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(200000), GasPriceSubunits: big.NewInt(200000)},
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(11), JuelsPerFeeCoin: big.NewInt(300000), GasPriceSubunits: big.NewInt(300000)},
})
require.NoError(f, err)

// Seed with valid report
Expand All @@ -28,7 +29,7 @@ func FuzzReportCodecMedianFromReport(f *testing.F) {
med, err := cdc.MedianFromReport(report)
if err == nil {
// Should always be able to build a report from the medians extracted
_, err = cdc.BuildReport([]median.ParsedAttributedObservation{{Timestamp: uint32(time.Now().Unix()), Value: med, JuelsPerFeeCoin: med}})
_, err = cdc.BuildReport([]median.ParsedAttributedObservation{{Timestamp: uint32(time.Now().Unix()), Value: med, JuelsPerFeeCoin: med, GasPriceSubunits: med}})
require.NoError(t, err)
}
})
Expand Down
52 changes: 34 additions & 18 deletions relayer/pkg/chainlink/ocr2/medianreport/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,39 @@ func TestBuildReportWithNegativeValues(t *testing.T) {
oo := []median.ParsedAttributedObservation{}

oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Value: big.NewInt(-10),
JuelsPerFeeCoin: big.NewInt(10),
Observer: commontypes.OracleID(1),
Timestamp: uint32(time.Now().Unix()),
Value: big.NewInt(-10),
JuelsPerFeeCoin: big.NewInt(10),
GasPriceSubunits: big.NewInt(10),
Observer: commontypes.OracleID(1),
})

_, err := c.BuildReport(oo)
assert.ErrorContains(t, err, "starknet does not support negative values: value = (-10), fee = (10)")
assert.ErrorContains(t, err, "starknet does not support negative values: value = (-10), fee = (10), gas = (10)")

oo = []median.ParsedAttributedObservation{}
oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Value: big.NewInt(10),
JuelsPerFeeCoin: big.NewInt(-10),
Observer: commontypes.OracleID(1),
Timestamp: uint32(time.Now().Unix()),
Value: big.NewInt(10),
JuelsPerFeeCoin: big.NewInt(-10),
GasPriceSubunits: big.NewInt(10),
Observer: commontypes.OracleID(1),
})

_, err = c.BuildReport(oo)
assert.ErrorContains(t, err, "starknet does not support negative values: value = (10), fee = (-10)")
assert.ErrorContains(t, err, "starknet does not support negative values: value = (10), fee = (-10), gas = (10)")

oo = []median.ParsedAttributedObservation{}
oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Value: big.NewInt(10),
JuelsPerFeeCoin: big.NewInt(10),
GasPriceSubunits: big.NewInt(-10),
Observer: commontypes.OracleID(1),
})

_, err = c.BuildReport(oo)
assert.ErrorContains(t, err, "starknet does not support negative values: value = (10), fee = (10), gas = (-10)")
}

func TestBuildReport(t *testing.T) {
Expand All @@ -51,10 +65,11 @@ func TestBuildReport(t *testing.T) {

for i := 0; i < n; i++ {
oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Value: big.NewInt(1234567890),
JuelsPerFeeCoin: v,
Observer: commontypes.OracleID(i),
Timestamp: uint32(time.Now().Unix()),
Value: big.NewInt(1234567890),
GasPriceSubunits: big.NewInt(2),
JuelsPerFeeCoin: v,
Observer: commontypes.OracleID(i),
})

// create expected outputs
Expand Down Expand Up @@ -93,7 +108,7 @@ func TestBuildReport(t *testing.T) {

// validate gasPrice
index += juelsPerFeeCoinSizeBytes
expectedGasPrice := big.NewInt(1)
expectedGasPrice := big.NewInt(2)
assert.Equal(t, expectedGasPrice.FillBytes(make([]byte, gasPriceSizeBytes)), []byte(report[index:index+gasPriceSizeBytes]), "validate gasPrice")
}

Expand Down Expand Up @@ -145,9 +160,10 @@ func TestMedianFromReport(t *testing.T) {
var pos []median.ParsedAttributedObservation
for i, obs := range tc.obs {
pos = append(pos, median.ParsedAttributedObservation{
Value: obs,
JuelsPerFeeCoin: obs,
Observer: commontypes.OracleID(uint8(i))},
Value: obs,
JuelsPerFeeCoin: obs,
GasPriceSubunits: obs,
Observer: commontypes.OracleID(uint8(i))},
)
}
report, err := cdc.BuildReport(pos)
Expand Down

0 comments on commit 99118e4

Please sign in to comment.