Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Oct 10, 2024
1 parent 9aa0731 commit 2975d45
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 54 deletions.
29 changes: 21 additions & 8 deletions relayer/pkg/chainlink/ocr2/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ocr2
import (
"errors"
"fmt"
"math"
"math/big"
"time"

Expand Down Expand Up @@ -57,15 +58,19 @@ func ParseNewTransmissionEvent(event starknetrpc.EmittedEvent) (NewTransmissionE
return NewTransmissionEvent{}, errors.New("invalid: event data")
}

observationsLen := eventData[observationsLenIndex].BigInt(big.NewInt(0)).Uint64()
observationsLen := eventData[observationsLenIndex].BigInt(big.NewInt(0)).Int64()
if len(eventData) != constNumOfElements+int(observationsLen) {
return NewTransmissionEvent{}, errors.New("invalid: event data")
}
}

// keys[0] == event_id
// round_id
roundID := uint32(event.Keys[1].BigInt(big.NewInt(0)).Uint64())
roundID64 := event.Keys[1].BigInt(big.NewInt(0)).Uint64()
if roundID64 > math.MaxUint32 {
return NewTransmissionEvent{}, fmt.Errorf("round overflows uint32: %d", roundID64)
}
roundID := uint32(roundID64)
// transmitter
transmitter := event.Keys[2]

Expand All @@ -85,7 +90,11 @@ func ParseNewTransmissionEvent(event starknetrpc.EmittedEvent) (NewTransmissionE

// observation_len
index++
observationsLen := uint32(eventData[index].BigInt(big.NewInt(0)).Uint64())
observationsLen64 := eventData[index].BigInt(big.NewInt(0)).Uint64()
if observationsLen64 > math.MaxUint32 {
return NewTransmissionEvent{}, fmt.Errorf("observation count overflows uint32: %d", observationsLen64)
}
observationsLen := uint32(observationsLen64)

// observers (based on observationsLen)
var observers []uint8
Expand Down Expand Up @@ -178,7 +187,7 @@ func ParseConfigSetEvent(event starknetrpc.EmittedEvent) (types.ContractConfig,

// oracles_len
index++
oraclesLen := eventData[index].BigInt(big.NewInt(0)).Uint64()
oraclesLen := eventData[index].BigInt(big.NewInt(0)).Int64()

// oracles
index++
Expand All @@ -196,11 +205,15 @@ func ParseConfigSetEvent(event starknetrpc.EmittedEvent) (types.ContractConfig,

// f
index = index + int(oraclesLen)*2
f := eventData[index].BigInt(big.NewInt(0)).Uint64()
f64 := eventData[index].BigInt(big.NewInt(0)).Uint64()
if f64 > math.MaxUint8 {
return types.ContractConfig{}, fmt.Errorf("f overflows uint8: %d", f64)
}
f := uint8(f64)

// onchain_config length
index++
onchainConfigLen := eventData[index].BigInt(big.NewInt(0)).Uint64()
onchainConfigLen := eventData[index].BigInt(big.NewInt(0)).Int64()

// onchain_config (version=1, min, max)
index++
Expand All @@ -220,7 +233,7 @@ func ParseConfigSetEvent(event starknetrpc.EmittedEvent) (types.ContractConfig,

// offchain_config_len
index++
offchainConfigLen := eventData[index].BigInt(big.NewInt(0)).Uint64()
offchainConfigLen := eventData[index].BigInt(big.NewInt(0)).Int64()

// offchain_config
index++
Expand All @@ -235,7 +248,7 @@ func ParseConfigSetEvent(event starknetrpc.EmittedEvent) (types.ContractConfig,
ConfigCount: configCount,
Signers: signers,
Transmitters: transmitters,
F: uint8(f),
F: f,
OnchainConfig: onchainConfig,
OffchainConfigVersion: offchainConfigVersion,
OffchainConfig: offchainConfig,
Expand Down
2 changes: 1 addition & 1 deletion relayer/pkg/chainlink/ocr2/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestNewTransmissionEvent_Parse(t *testing.T) {

require.Equal(t, e.Observers, []uint8{0, 1, 2, 3})
require.Equal(t, e.ObservationsLen, uint32(4))
require.Equal(t, e.ObservationsLen, uint32(len(e.Observers)))
require.Equal(t, int(e.ObservationsLen), len(e.Observers))

configDigest := XXXMustBytesToConfigDigest(starknet.XXXMustHexDecodeString("000485341c18461d70eac6ded4b8b17147f173308ddd56216a86f9ec4d994453"))
require.Equal(t, len(configDigest), 32) // padded to 32 bytes
Expand Down
14 changes: 10 additions & 4 deletions relayer/pkg/chainlink/ocr2/medianreport/report_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package medianreport

import (
"math"
"math/big"
"testing"
"time"
Expand All @@ -19,10 +20,15 @@ import (
func FuzzReportCodecMedianFromReport(f *testing.F) {
ctx := tests.Context(f)
cdc := ReportCodec{}
now := time.Now().Unix()
if now > math.MaxUint32 || now < 0 {
f.Fatalf("unix timestamp overflows uint32: %d", now)
}
ts := uint32(now)
report, err := cdc.BuildReport(ctx, []median.ParsedAttributedObservation{
{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)},
{Timestamp: ts, Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(100000), GasPriceSubunits: big.NewInt(100000)},
{Timestamp: ts, Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(200000), GasPriceSubunits: big.NewInt(200000)},
{Timestamp: ts, Value: big.NewInt(11), JuelsPerFeeCoin: big.NewInt(300000), GasPriceSubunits: big.NewInt(300000)},
})
require.NoError(f, err)

Expand All @@ -33,7 +39,7 @@ func FuzzReportCodecMedianFromReport(f *testing.F) {
med, err := cdc.MedianFromReport(ctx, report)
if err == nil {
// Should always be able to build a report from the medians extracted
_, err = cdc.BuildReport(ctx, []median.ParsedAttributedObservation{{Timestamp: uint32(time.Now().Unix()), Value: med, JuelsPerFeeCoin: med, GasPriceSubunits: med}})
_, err = cdc.BuildReport(ctx, []median.ParsedAttributedObservation{{Timestamp: ts, Value: med, JuelsPerFeeCoin: med, GasPriceSubunits: med}})
require.NoError(t, err)
}
})
Expand Down
54 changes: 37 additions & 17 deletions relayer/pkg/chainlink/ocr2/medianreport/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package medianreport

import (
"fmt"
"math"
"math/big"
"testing"
"time"
Expand All @@ -22,8 +23,14 @@ func TestBuildReportWithNegativeValues(t *testing.T) {
c := ReportCodec{}
oo := []median.ParsedAttributedObservation{}

now64 := time.Now().Unix()
if now64 > math.MaxUint32 || now64 < 0 {
t.Fatalf("unix timestamp overflows uint32: %d", now64)
}
now := uint32(now64)

oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Timestamp: now,
Value: big.NewInt(-10),
JuelsPerFeeCoin: big.NewInt(10),
GasPriceSubunits: big.NewInt(10),
Expand All @@ -35,7 +42,7 @@ func TestBuildReportWithNegativeValues(t *testing.T) {

oo = []median.ParsedAttributedObservation{}
oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Timestamp: now,
Value: big.NewInt(10),
JuelsPerFeeCoin: big.NewInt(-10),
GasPriceSubunits: big.NewInt(10),
Expand All @@ -47,7 +54,7 @@ func TestBuildReportWithNegativeValues(t *testing.T) {

oo = []median.ParsedAttributedObservation{}
oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Timestamp: now,
Value: big.NewInt(10),
JuelsPerFeeCoin: big.NewInt(10),
GasPriceSubunits: big.NewInt(-10),
Expand All @@ -66,14 +73,20 @@ func TestBuildReportNoObserversOverflow(t *testing.T) {
v := big.NewInt(0)
v.SetString("1000000000000000000", 10)

now64 := time.Now().Unix()
if now64 > math.MaxUint32 || now64 < 0 {
t.Fatalf("unix timestamp overflows uint32: %d", now64)
}
now := uint32(now64)

// test largest possible encoded observers byte array
for i := 30; i >= 0; i-- {
for i := commontypes.OracleID(0); i < 30; i-- {
oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Timestamp: now,
Value: big.NewInt(1234567890),
GasPriceSubunits: big.NewInt(2),
JuelsPerFeeCoin: v,
Observer: commontypes.OracleID(i),
Observer: i,
})
}

Expand All @@ -94,20 +107,26 @@ func TestBuildReport(t *testing.T) {
oo := []median.ParsedAttributedObservation{}

// expected outputs
n := 4
n := commontypes.OracleID(4)
observers := make([]byte, 32)
v := big.NewInt(0)
v.SetString("1000000000000000000", 10)

now64 := time.Now().Unix()
if now64 > math.MaxUint32 || now64 < 0 {
t.Fatalf("unix timestamp overflows uint32: %d", now64)
}
now := uint32(now64)

// 0x01 pad the first byte
observers[0] = uint8(1)
for i := 0; i < n; i++ {
for i := commontypes.OracleID(0); i < n; i++ {
oo = append(oo, median.ParsedAttributedObservation{
Timestamp: uint32(time.Now().Unix()),
Timestamp: now,
Value: big.NewInt(1234567890),
GasPriceSubunits: big.NewInt(2),
JuelsPerFeeCoin: v,
Observer: commontypes.OracleID(i),
Observer: i,
})

// create expected outputs
Expand All @@ -119,7 +138,7 @@ func TestBuildReport(t *testing.T) {
assert.NoError(t, err)

// validate length
totalLen := prefixSizeBytes + observationSizeBytes*n + juelsPerFeeCoinSizeBytes + gasPriceSizeBytes
totalLen := prefixSizeBytes + observationSizeBytes*int(n) + juelsPerFeeCoinSizeBytes + gasPriceSizeBytes
assert.Equal(t, totalLen, len(report), "validate length")

// validate timestamp
Expand All @@ -133,16 +152,16 @@ func TestBuildReport(t *testing.T) {
// validate observer count
index += observersSizeBytes
count := new(big.Int).SetBytes(report[index : index+observationsLenBytes])
assert.Equal(t, uint8(n), uint8(count.Uint64()), "validate observer count")
assert.Equal(t, uint64(n), count.Uint64(), "validate observer count")

// validate observations
for i := 0; i < n; i++ {
for i := 0; i < int(n); i++ {
idx := prefixSizeBytes + observationSizeBytes*i
assert.Equal(t, oo[0].Value.FillBytes(make([]byte, observationSizeBytes)), []byte(report[idx:idx+observationSizeBytes]), fmt.Sprintf("validate median observation #%d", i))
}

// validate juelsPerFeeCoin
index = prefixSizeBytes + observationSizeBytes*n
index = prefixSizeBytes + observationSizeBytes*int(n)
assert.Equal(t, v.FillBytes(make([]byte, juelsPerFeeCoinSizeBytes)), []byte(report[index:index+juelsPerFeeCoinSizeBytes]), "validate juelsPerFeeCoin")

// validate gasPrice
Expand Down Expand Up @@ -199,13 +218,14 @@ func TestMedianFromReport(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctx := tests.Context(t)
var pos []median.ParsedAttributedObservation
for i, obs := range tc.obs {
for i := commontypes.OracleID(0); int(i) < len(tc.obs); i++ {
obs := tc.obs[i]
pos = append(pos, median.ParsedAttributedObservation{
Value: obs,
JuelsPerFeeCoin: obs,
GasPriceSubunits: obs,
Observer: commontypes.OracleID(uint8(i))},
)
Observer: i,
})
}
report, err := cdc.BuildReport(ctx, pos)
require.NoError(t, err)
Expand Down
8 changes: 6 additions & 2 deletions relayer/pkg/chainlink/ocr2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ func NewRoundData(felts []*felt.Felt) (data RoundData, err error) {
return data, fmt.Errorf("expected number of felts to be 5 but got %d", len(felts))
}
roundID := felts[0].BigInt(big.NewInt(0))
if !roundID.IsUint64() && roundID.Uint64() > math.MaxUint32 {
if !roundID.IsUint64() {
return data, fmt.Errorf("aggregator round id does not fit in a uint64 '%s'", felts[0].String())
}
roundID64 := felts[0].BigInt(big.NewInt(0)).Uint64()
if roundID64 > math.MaxUint32 {
return data, fmt.Errorf("aggregator round id does not fit in a uint32 '%s'", felts[0].String())
}
data.RoundID = uint32(roundID.Uint64())
data.RoundID = uint32(roundID64)
data.Answer = felts[1].BigInt(big.NewInt(0))
blockNumber := felts[2].BigInt(big.NewInt(0))
if !blockNumber.IsUint64() {
Expand Down
24 changes: 12 additions & 12 deletions relayer/pkg/chainlink/txm/txstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func TestTxStore(t *testing.T) {

// init store
s := NewTxStore(new(felt.Felt).SetUint64(0))
for i := 0; i < 6; i++ {
require.NoError(t, s.AddUnconfirmed(new(felt.Felt).SetUint64(uint64(i)), "0x"+fmt.Sprintf("%d", i), call, publicKey))
for i := uint64(0); i < 6; i++ {
require.NoError(t, s.AddUnconfirmed(new(felt.Felt).SetUint64(i), "0x"+fmt.Sprintf("%d", i), call, publicKey))
}

// confirm in order
Expand Down Expand Up @@ -152,32 +152,32 @@ func TestTxStore(t *testing.T) {
}

publicKey := new(felt.Felt).SetUint64(7)
txCount := 6
txCount := uint64(6)

// init store
s := NewTxStore(new(felt.Felt).SetUint64(0))
for i := 0; i < txCount; i++ {
require.NoError(t, s.AddUnconfirmed(new(felt.Felt).SetUint64(uint64(i)), "0x"+fmt.Sprintf("%d", i), call, publicKey))
for i := uint64(0); i < txCount; i++ {
require.NoError(t, s.AddUnconfirmed(new(felt.Felt).SetUint64(i), "0x"+fmt.Sprintf("%d", i), call, publicKey))
}
assert.Equal(t, s.InflightCount(), txCount)
assert.Equal(t, s.InflightCount(), 6)

staleTxs := s.SetNextNonce(new(felt.Felt).SetUint64(0))

assert.Equal(t, len(staleTxs), txCount)
for i := 0; i < txCount; i++ {
assert.Equal(t, uint64(len(staleTxs)), txCount)
for i := uint64(0); i < txCount; i++ {
staleTx := staleTxs[i]
assert.Equal(t, staleTx.Nonce.Cmp(new(felt.Felt).SetUint64(uint64(i))), 0)
assert.Equal(t, staleTx.Nonce.Cmp(new(felt.Felt).SetUint64(i)), 0)
assert.Equal(t, staleTx.Call, call)
assert.Equal(t, staleTx.PublicKey.Cmp(publicKey), 0)
assert.Equal(t, staleTx.Hash, "0x"+fmt.Sprintf("%d", i))
}
assert.Equal(t, s.InflightCount(), 0)

for i := 0; i < txCount; i++ {
require.NoError(t, s.AddUnconfirmed(new(felt.Felt).SetUint64(uint64(i)), "0x"+fmt.Sprintf("%d", i), call, publicKey))
for i := uint64(0); i < txCount; i++ {
require.NoError(t, s.AddUnconfirmed(new(felt.Felt).SetUint64(i), "0x"+fmt.Sprintf("%d", i), call, publicKey))
}

newNextNonce := uint64(txCount - 1)
newNextNonce := txCount - 1
staleTxs = s.SetNextNonce(new(felt.Felt).SetUint64(newNextNonce))
assert.Equal(t, len(staleTxs), 1)
assert.Equal(t, staleTxs[0].Nonce.Cmp(new(felt.Felt).SetUint64(newNextNonce)), 0)
Expand Down
Loading

0 comments on commit 2975d45

Please sign in to comment.