Skip to content

Commit

Permalink
Separate class for range checks
Browse files Browse the repository at this point in the history
  • Loading branch information
edwin committed Dec 21, 2022
1 parent 069b459 commit 90dd8e3
Showing 1 changed file with 51 additions and 28 deletions.
79 changes: 51 additions & 28 deletions relayer/pkg/chainlink/ocr2/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ocr2

import (
"encoding/hex"
"fmt"
"math"
"math/big"
"time"
Expand All @@ -23,14 +24,25 @@ var (
NewTransmissionEventSelector = "019e22f866f4c5aead2809bf160d2b29e921e335d899979732101c6f3c38ff81"
ConfigSetEventSelector = "9a144bf4a6a8fd083c93211e163e59221578efcc86b93f8c97c620e7b9608a"

feltLowerBound = new(big.Int).SetUint64(0)
feltUpperBound = caigotypes.MaxFelt.Big()
feltRange = Range{
new(big.Int).SetUint64(0),
caigotypes.MaxFelt.Big(),
}

uint32LowerBound = new(big.Int).SetUint64(0)
uint32UpperBound = new(big.Int).SetUint64(math.MaxUint32)
uint32Range = Range{
new(big.Int).SetUint64(0),
new(big.Int).SetUint64(math.MaxUint32),
}

uint64LowerBound = new(big.Int).SetUint64(0)
uint64UpperBound = new(big.Int).SetUint64(math.MaxUint64)
uint64Range = Range{
new(big.Int).SetUint64(0),
new(big.Int).SetUint64(math.MaxUint64),
}

int64Range = Range{
new(big.Int).SetInt64(math.MinInt64),
new(big.Int).SetInt64(math.MaxInt64),
}
)

// NewTransmissionEvent represents the 'NewTransmission' event
Expand All @@ -50,9 +62,14 @@ type NewTransmissionEvent struct {
Reimbursement *big.Int
}

func rangeCheck(felt *caigotypes.Felt, lowerBound *big.Int, upperBound *big.Int) error {
if !(felt.Int.Cmp(lowerBound) >= 0 && felt.Int.Cmp(upperBound) <= 0) {
return errors.New("invalid: value is out of range")
type Range struct {
Lower *big.Int
Upper *big.Int
}

func (r *Range) Check(n *big.Int) error {
if !(n.Cmp(r.Lower) >= 0 && n.Cmp(r.Upper) <= 0) {
return fmt.Errorf("invalid: value %v is out of range [%v,%v]", n, r.Lower, r.Upper)
}

return nil
Expand All @@ -68,7 +85,7 @@ func ParseNewTransmissionEvent(eventData []*caigotypes.Felt) (NewTransmissionEve
return NewTransmissionEvent{}, errors.New("invalid: event data")
}

err := rangeCheck(eventData[observationsLenIndex], uint64LowerBound, uint64UpperBound)
err := uint64Range.Check(eventData[observationsLenIndex].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
Expand All @@ -80,31 +97,31 @@ func ParseNewTransmissionEvent(eventData []*caigotypes.Felt) (NewTransmissionEve

// round_id
index := 0
err := rangeCheck(eventData[index], uint32LowerBound, uint32UpperBound)
err := uint32Range.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
roundId := uint32(eventData[index].Uint64())

// answer
index++
err = rangeCheck(eventData[index], feltLowerBound, feltUpperBound)
err = feltRange.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
latestAnswer := starknet.HexToSignedBig(eventData[index].String())

// transmitter
index++
err = rangeCheck(eventData[index], feltLowerBound, feltUpperBound)
err = feltRange.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
transmitter := eventData[index]

// observation_timestamp
index++
err = rangeCheck(eventData[index], new(big.Int).SetInt64(math.MinInt64), new(big.Int).SetInt64(math.MaxInt64))
err = int64Range.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
Expand All @@ -117,7 +134,7 @@ func ParseNewTransmissionEvent(eventData []*caigotypes.Felt) (NewTransmissionEve

// observation_len
index++
err = rangeCheck(eventData[index], uint32LowerBound, uint32UpperBound)
err = uint32Range.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
Expand All @@ -132,7 +149,7 @@ func ParseNewTransmissionEvent(eventData []*caigotypes.Felt) (NewTransmissionEve
// observations (based on observationsLen)
var observations []*big.Int
for i := 0; i < int(observationsLen); i++ {
err = rangeCheck(eventData[index], feltLowerBound, feltUpperBound)
err = feltRange.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
Expand All @@ -141,15 +158,15 @@ func ParseNewTransmissionEvent(eventData []*caigotypes.Felt) (NewTransmissionEve

// juels_per_fee_coin
index += int(observationsLen) + 1
err = rangeCheck(eventData[index], feltLowerBound, feltUpperBound)
err = feltRange.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
juelsPerFeeCoin := eventData[index].Big()

// juels_per_fee_coin
index++
err = rangeCheck(eventData[index], feltLowerBound, feltUpperBound)
err = feltRange.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
Expand All @@ -168,7 +185,7 @@ func ParseNewTransmissionEvent(eventData []*caigotypes.Felt) (NewTransmissionEve

// reimbursement
index++
err = rangeCheck(eventData[index], feltLowerBound, feltUpperBound)
err = feltRange.Check(eventData[index].Big())
if err != nil {
return NewTransmissionEvent{}, err
}
Expand All @@ -193,13 +210,19 @@ func ParseNewTransmissionEvent(eventData []*caigotypes.Felt) (NewTransmissionEve

// ParseConfigSetEvent is decoding binary felt data as the libocr ContractConfig type
func ParseConfigSetEvent(eventData []*caigotypes.Felt) (types.ContractConfig, error) {
maxObserversRange := Range{
uint64Range.Lower,
new(big.Int).SetUint64(uint64(MaxObservers)),
}

{
const oraclesLenIdx = 3
if len(eventData) < oraclesLenIdx {
return types.ContractConfig{}, errors.New("invalid: event data")
}

err := rangeCheck(eventData[oraclesLenIdx], uint64LowerBound, new(big.Int).SetUint64(uint64(MaxObservers)))
err := maxObserversRange.Check(eventData[oraclesLenIdx].Big())

if err != nil {
return types.ContractConfig{}, err
}
Expand All @@ -210,7 +233,7 @@ func ParseConfigSetEvent(eventData []*caigotypes.Felt) (types.ContractConfig, er
return types.ContractConfig{}, errors.New("invalid: event data")
}

err = rangeCheck(eventData[onchainConfigLenIdx], uint64LowerBound, uint64UpperBound)
err = uint64Range.Check(eventData[onchainConfigLenIdx].Big())
if err != nil {
return types.ContractConfig{}, err
}
Expand All @@ -222,7 +245,7 @@ func ParseConfigSetEvent(eventData []*caigotypes.Felt) (types.ContractConfig, er
}

offchainConfigLen := eventData[offchainConfigLenIdx].Uint64()
err = rangeCheck(eventData[onchainConfigLenIdx], uint64LowerBound, uint64UpperBound)
err = uint64Range.Check(eventData[onchainConfigLenIdx].Big())
if err != nil {
return types.ContractConfig{}, err
}
Expand All @@ -244,15 +267,15 @@ func ParseConfigSetEvent(eventData []*caigotypes.Felt) (types.ContractConfig, er

// config_count
index++
err = rangeCheck(eventData[index], uint64LowerBound, uint64UpperBound)
err = uint64Range.Check(eventData[index].Big())
if err != nil {
return types.ContractConfig{}, err
}
configCount := eventData[index].Uint64()

// oracles_len
index++
err = rangeCheck(eventData[index], uint64LowerBound, new(big.Int).SetUint64(uint64(MaxObservers)))
err = maxObserversRange.Check(eventData[index].Big())
if err != nil {
return types.ContractConfig{}, err
}
Expand All @@ -273,15 +296,15 @@ func ParseConfigSetEvent(eventData []*caigotypes.Felt) (types.ContractConfig, er

// f
index = index + int(oraclesLen)*2
err = rangeCheck(eventData[index], uint64LowerBound, uint64UpperBound)
err = uint64Range.Check(eventData[index].Big())
if err != nil {
return types.ContractConfig{}, err
}
f := eventData[index].Uint64()

// onchain_config length
index++
err = rangeCheck(eventData[index], uint64LowerBound, uint64UpperBound)
err = uint64Range.Check(eventData[index].Big())
if err != nil {
return types.ContractConfig{}, err
}
Expand All @@ -301,15 +324,15 @@ func ParseConfigSetEvent(eventData []*caigotypes.Felt) (types.ContractConfig, er

// offchain_config_version
index += int(onchainConfigLen)
err = rangeCheck(eventData[index], uint64LowerBound, uint64UpperBound)
err = uint64Range.Check(eventData[index].Big())
if err != nil {
return types.ContractConfig{}, err
}
offchainConfigVersion := eventData[index].Uint64()

// offchain_config_len
index++
err = rangeCheck(eventData[index], uint64LowerBound, uint64UpperBound)
err = uint64Range.Check(eventData[index].Big())
if err != nil {
return types.ContractConfig{}, err
}
Expand Down

0 comments on commit 90dd8e3

Please sign in to comment.