Skip to content

Commit

Permalink
wip unit test proposer height cache
Browse files Browse the repository at this point in the history
  • Loading branch information
iansuvak committed Oct 14, 2024
1 parent fe4cd5a commit cad40d8
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
4 changes: 3 additions & 1 deletion relayer/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
offchainregistry "github.com/ava-labs/awm-relayer/messages/off-chain-registry"
"github.com/ava-labs/awm-relayer/messages/teleporter"
"github.com/ava-labs/awm-relayer/peers"
"github.com/ava-labs/awm-relayer/peers/validators"
"github.com/ava-labs/awm-relayer/relayer"
"github.com/ava-labs/awm-relayer/relayer/api"
"github.com/ava-labs/awm-relayer/relayer/checkpoint"
Expand Down Expand Up @@ -220,9 +221,10 @@ func main() {
panic(err)
}

canonicalValidatorClient := validators.NewCanonicalValidatorClient(logger, cfg.GetPChainAPI())
proposerHeightCache, err := aggregator.NewProposerHeightCache(
logger,
cfg.GetPChainAPI(),
canonicalValidatorClient,
time.Second*2,
)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions signature-aggregator/aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package aggregator

import (
Expand Down
6 changes: 2 additions & 4 deletions signature-aggregator/aggregator/proposer_height_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ava-labs/avalanchego/utils/linked"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
basecfg "github.com/ava-labs/awm-relayer/config"
"github.com/ava-labs/awm-relayer/peers/validators"
"go.uber.org/zap"
)
Expand All @@ -39,10 +38,9 @@ type ProposerHeightCache struct {

func NewProposerHeightCache(
logger logging.Logger,
pChainApiConfig *basecfg.APIConfig,
pChainClient validators.CanonicalValidatorClient,
updateInterval time.Duration,
) (*ProposerHeightCache, error) {
pChainClient := validators.NewCanonicalValidatorClient(logger, pChainApiConfig)
pHeightCache := &ProposerHeightCache{
logger: logger,
pChainClient: pChainClient,
Expand Down Expand Up @@ -88,7 +86,7 @@ func (p *ProposerHeightCache) updateData() {
return
}

for i := currentMaxHeight; i < height; i++ {
for i := currentMaxHeight + 1; i <= height; i++ {
err := p.writeTimeForHeight(i)
if err != nil {
// Log the warning and continue
Expand Down
90 changes: 90 additions & 0 deletions signature-aggregator/aggregator/proposer_height_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package aggregator

import (
"context"
"sync/atomic"
"testing"
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/awm-relayer/peers/validators/mocks"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

const startCurrentHeight = uint64(10)

func TestProposerHeightInit(t *testing.T) {
logger := logging.NoLog{}
updateInterval := time.Second * 2

mockClient := mocks.NewMockCanonicalValidatorClient(gomock.NewController(t))
mockClient.EXPECT().GetCurrentHeight(gomock.Any()).Return(startCurrentHeight, nil)
proposerHeights, err := NewProposerHeightCache(logger, mockClient, updateInterval)
if err != nil {
t.Errorf("Expected no error, but got %s", err)
}

currentHeight := atomic.LoadUint64(&proposerHeights.currentMaxHeight)
require.Equal(t, startCurrentHeight, currentHeight)

// When the cache is empty the optimal height should be the current height - 1
require.Zero(t, proposerHeights.timeToHeight.Len())
require.Equal(t, currentHeight-1, proposerHeights.GetOptimalHeight())

}

Check failure on line 40 in signature-aggregator/aggregator/proposer_height_cache_test.go

View workflow job for this annotation

GitHub Actions / golangci

unnecessary trailing newline (whitespace)

func TestProposerHeightCatchup(t *testing.T) {
logger := logging.NoLog{}
updateInterval := time.Second * 2

mockClient := mocks.NewMockCanonicalValidatorClient(gomock.NewController(t))
mockClient.EXPECT().GetCurrentHeight(gomock.Any()).Return(startCurrentHeight, nil)
proposerHeights, err := NewProposerHeightCache(logger, mockClient, updateInterval)
if err != nil {
t.Errorf("Expected no error, but got %s", err)
}

currentHeight := atomic.LoadUint64(&proposerHeights.currentMaxHeight)
require.Equal(t, startCurrentHeight, currentHeight)

newHeight := uint64(20)
mockClient.EXPECT().GetCurrentHeight(gomock.Any()).Return(newHeight, nil).Times(1)
mockClient.EXPECT().GetBlockByHeight(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, height uint64) ([]byte, error) {
// TODO, make timings for the test less flaky
// treat newHeight as time.Now
// and block production as happening every 5 seconds.

blockTime := time.Now().Add(-time.Second * time.Duration(5*(newHeight-height)))
banffBlock := buildFakeBlockWithTime(t, blockTime, height)
return banffBlock.Bytes(), nil
}).Times(10)

// Now we have entered 10 elements from 11 to 20
// as verified with the mockClient but also should have evicted
// the first 4 elements as they are older than 30 seconds
proposerHeights.updateData()

currentHeight = atomic.LoadUint64(&proposerHeights.currentMaxHeight)
require.Equal(t, newHeight, currentHeight)
require.Equal(t, 6, proposerHeights.timeToHeight.Len())

_, oldestHeight, ok := proposerHeights.timeToHeight.Oldest()
require.True(t, ok)
require.Equal(t, uint64(15), oldestHeight)
optimalHeight := proposerHeights.GetOptimalHeight()
require.Equal(t, uint64(14), optimalHeight)
}

func buildFakeBlockWithTime(t *testing.T, blockTime time.Time, height uint64) *block.BanffStandardBlock {
// Create a fake block with a specific time and height
// these don't need to refer to each other so generating random test IDs.
b, err := block.NewBanffStandardBlock(blockTime, ids.GenerateTestID(), height, nil)
require.NoError(t, err)
return b
}
4 changes: 3 additions & 1 deletion signature-aggregator/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/awm-relayer/peers"
"github.com/ava-labs/awm-relayer/peers/validators"
"github.com/ava-labs/awm-relayer/signature-aggregator/aggregator"
"github.com/ava-labs/awm-relayer/signature-aggregator/api"
"github.com/ava-labs/awm-relayer/signature-aggregator/config"
Expand Down Expand Up @@ -113,9 +114,10 @@ func main() {
registry := metrics.Initialize(cfg.MetricsPort)
metricsInstance := metrics.NewSignatureAggregatorMetrics(registry)

canonicalValidatorClient := validators.NewCanonicalValidatorClient(logger, cfg.GetPChainAPI())
proposerHeightCache, err := aggregator.NewProposerHeightCache(
logger,
cfg.GetPChainAPI(),
canonicalValidatorClient,
time.Second*2,
)
if err != nil {
Expand Down

0 comments on commit cad40d8

Please sign in to comment.