Skip to content

Commit

Permalink
Server parameter validation (#250)
Browse files Browse the repository at this point in the history
* Test for missing parameters

* Test for invalid parameters

* Test for emtpy parameters

* Rename tests

* Flip `assert.Equal` arguments (expected/actual)

* Extend invalid parameter testing

* Test for not found in DB

* Remove additional `&`

* Test for missing parameter

* Test `GetOperatorSetUpdateAggregation` not found

* Test for too large values

* Check for invalid ranges

* Add tests for large integers

* Tests for `GetCheckpointMessages`

* Return serialization errors

* Check for handler error

* Use `assert.NoError`

* Use `assert.AnError`
  • Loading branch information
emlautarom1 authored Jun 14, 2024
1 parent 18544a6 commit d4dda75
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 27 deletions.
4 changes: 4 additions & 0 deletions aggregator/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ func (d *Database) FetchCheckpointMessages(fromTimestamp uint64, toTimestamp uin
return nil, errors.New("timestamp does not fit in int64")
}

if (toTimestamp < fromTimestamp) {
return nil, errors.New("toTimestamp is less than fromTimestamp")
}

start := time.Now()
defer func() { d.listener.OnFetch(time.Since(start)) }()

Expand Down
57 changes: 57 additions & 0 deletions aggregator/database/database_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database_test

import (
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -313,3 +314,59 @@ func TestFetchCheckpointMessages(t *testing.T) {
OperatorSetUpdateMessageAggregations: []messages.MessageBlsAggregation{},
})
}

func TestFetchCheckpointMessages_TimestampTooLarge(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

db, err := database.NewDatabase(":memory:")
assert.Nil(t, err)

t.Run("fromTimestamp too large", func(t *testing.T) {
_, err := db.FetchCheckpointMessages(uint64(0x8000000000000000), 0)
assert.NotNil(t, err)
})

t.Run("toTimestamp too large", func(t *testing.T) {
_, err := db.FetchCheckpointMessages(0, uint64(0x8000000000000000))
assert.NotNil(t, err)
})
}

func TestFetchCheckpointMessages_InvalidRange(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

db, err := database.NewDatabase(":memory:")
assert.Nil(t, err)

_, err = db.FetchCheckpointMessages(101, 100)
assert.NotNil(t, err)
}

func TestStoreStateRootUpdate_LargeMsgValues(t *testing.T) {
t.Skip("Currently impossible to store all uint64 values in the DB")

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

db, err := database.NewDatabase(":memory:")
assert.Nil(t, err)

msg := messages.StateRootUpdateMessage{
RollupId: math.MaxUint32,
BlockHeight: math.MaxUint64, // TODO: Cannot be stored, maximum possible value is `math.MaxInt64`
Timestamp: math.MaxUint64, // TODO: Cannot be stored, maximum possible value is `math.MaxInt64`
NearDaTransactionId: [32]byte{0xFF},
NearDaCommitment: [32]byte{0xFF},
StateRoot: [32]byte{0xFF},
}
err = db.StoreStateRootUpdate(msg)
assert.Nil(t, err)

stored, err := db.FetchStateRootUpdate(math.MaxUint32, math.MaxUint64)
assert.NotNil(t, stored)
assert.Nil(t, err)

assert.Equal(t, &msg, stored)
}
12 changes: 6 additions & 6 deletions aggregator/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func (agg *Aggregator) handleGetStateRootUpdateAggregation(w http.ResponseWriter

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(aggtypes.GetStateRootUpdateAggregationResponse{
err = json.NewEncoder(w).Encode(aggtypes.GetStateRootUpdateAggregationResponse{
Message: *message,
Aggregation: *aggregation,
})

return nil
return err
}

func (agg *Aggregator) handleGetOperatorSetUpdateAggregation(w http.ResponseWriter, r *http.Request) error {
Expand All @@ -101,12 +101,12 @@ func (agg *Aggregator) handleGetOperatorSetUpdateAggregation(w http.ResponseWrit

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(aggtypes.GetOperatorSetUpdateAggregationResponse{
err = json.NewEncoder(w).Encode(aggtypes.GetOperatorSetUpdateAggregationResponse{
Message: *message,
Aggregation: *aggregation,
})

return nil
return err
}

func (agg *Aggregator) handleGetCheckpointMessages(w http.ResponseWriter, r *http.Request) error {
Expand All @@ -133,9 +133,9 @@ func (agg *Aggregator) handleGetCheckpointMessages(w http.ResponseWriter, r *htt

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(aggtypes.GetCheckpointMessagesResponse{
err = json.NewEncoder(w).Encode(aggtypes.GetCheckpointMessagesResponse{
CheckpointMessages: *checkpointMessages,
})

return nil
return err
}
Loading

0 comments on commit d4dda75

Please sign in to comment.