Skip to content
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
3a73d21
Feat: relconfig Bridge -> RFQAddress, add CCTPAddress
dwasse Feb 20, 2024
6664fd9
Feat: add RebalanceMethod enum
dwasse Feb 20, 2024
91017b7
Feat: add MaintenanceBalancePct and InitialBalancePct
dwasse Feb 20, 2024
f7d56f2
Feat: add RebalanceMethodNone
dwasse Feb 20, 2024
cbf01e7
Feat: add Rebalance() to inventory manager
dwasse Feb 20, 2024
512fb30
Cleanup: remove rebalance.go
dwasse Feb 20, 2024
536f227
Feat: add submitter to inventory manager
dwasse Feb 20, 2024
1e9e1d6
Cleanup: Adress -> Address
dwasse Feb 20, 2024
708eab0
WIP: add Rebalance() impl to inventory manager
dwasse Feb 20, 2024
eabdda0
Feat: add token address and chain id to tokenMetadata
dwasse Feb 20, 2024
1f7c3a3
Cleanup: utilize new tokenMetadata fields
dwasse Feb 20, 2024
44be2b3
Feat: add approval for CCTP bridge
dwasse Feb 20, 2024
d2acc79
Feat: impl rebalanceCCTP()
dwasse Feb 20, 2024
4ad03dc
Fix: regenerate
dwasse Feb 21, 2024
7c0cf7e
Cleanup: lint
dwasse Feb 21, 2024
fda6f90
WIP: export inventory structs for use in tests
dwasse Feb 21, 2024
4db711f
Feat: add TestGetRebalance
dwasse Feb 21, 2024
06e8e57
Feat: add rebalance case to TestGetRebalance
dwasse Feb 21, 2024
6b38ba5
Cleanup: remove duplicate data from RebalanceData
dwasse Feb 21, 2024
90a63e6
Feat: address rebalance case where origin chain cannot pass initial t…
dwasse Feb 21, 2024
6c70e96
Cleanup: regenerate inventory
dwasse Feb 21, 2024
3d561b0
Feat: remove handleClaimCompleted and move Rebalance() call to chaini…
dwasse Feb 21, 2024
e5868de
Feat: enforce rebalance methods to match from config
dwasse Feb 21, 2024
eca3b63
WIP: set rebalance params in e2e test
dwasse Feb 21, 2024
383ece0
WIP: add Rebalance interface and rebalanceManagerCCTP
dwasse Feb 21, 2024
0095c7c
Feat: add Start() to inventory manager
dwasse Feb 21, 2024
58c8a7f
Feat: add rebalance db models and functions
dwasse Feb 21, 2024
5aeb2c5
Feat: rebalance manager listens for cctp events and updates db
dwasse Feb 22, 2024
baab066
Feat: rebalance on interval instead of on DepositClaimed
dwasse Feb 22, 2024
752763b
Cleanup: DBSelectorIntervalSeconds -> DBSelectorInterval
dwasse Feb 22, 2024
ab90a63
Feat: check for pending rebalances
dwasse Feb 22, 2024
989c2d5
Feat: attempt rebalance on DepositClaimed
dwasse Feb 22, 2024
73e027d
Cleanup: lint
dwasse Feb 22, 2024
a4e308c
Fix: start inventory manager
dwasse Feb 23, 2024
03891fb
WIP: add cctp contract setup for integration test
dwasse Feb 23, 2024
c9ca663
WIP: use origin instead of origin tx hash for rebalance updating
dwasse Feb 26, 2024
81cfc79
WIP: enforce only one row affected in rebalance update
dwasse Feb 26, 2024
3a6ea21
WIP: don't track origin tx hash for now
dwasse Feb 26, 2024
42f24ce
WIP: logs
dwasse Feb 26, 2024
ed841c8
WIP: working test with MockMintBurnToken
dwasse Feb 27, 2024
628733d
WIP: adjust amounts
dwasse Feb 27, 2024
efaf3bd
WIP: fix approvals
dwasse Feb 27, 2024
1e7175f
Feat: remove fastbridge ref from chain listener
dwasse Feb 27, 2024
5c99c21
Feat: use iota+1 for rebalance status, regen model
dwasse Feb 27, 2024
8fcfbcb
WIP: don't return error in listener handler
dwasse Feb 27, 2024
88de62a
Feat: check for pending rebalances in integration test
dwasse Feb 27, 2024
ce32aa9
Cleanup: remove balance logs
dwasse Feb 27, 2024
819dd14
Cleanup: logs / comments
dwasse Feb 27, 2024
5f4486b
Cleanup: dedup code in inv manager
dwasse Feb 27, 2024
c8f2c67
Cleanup: lints
dwasse Feb 27, 2024
b7dd419
Feat: add CCTPStartBlock to relconfig
dwasse Feb 27, 2024
53e8129
[goreleaser]
dwasse Feb 27, 2024
2e33da3
Cleanup: lints
dwasse Feb 27, 2024
45c2ad9
Cleanup: lints
dwasse Feb 27, 2024
ba58876
add directive (#2122)
trajan0x Feb 28, 2024
4febe40
Feat: move listener to ethergo/chain/listener package
dwasse Feb 28, 2024
000549f
Merge branch 'feat/relayer-rebalance' of github.com:synapsecns/sangui…
dwasse Feb 28, 2024
ceb8479
[goreleaser]
dwasse Feb 28, 2024
19dc080
Cleanup: lints
dwasse Feb 28, 2024
085ac5e
update db
trajan0x Feb 28, 2024
782b869
Cleanup: remove unnecessary errgroup
dwasse Feb 28, 2024
6195502
Merge branch 'master' into feat/relayer-rebalance
dwasse Feb 28, 2024
07dbaeb
[goreleaser]
dwasse Feb 28, 2024
7a9a30b
fast
trajan0x Feb 29, 2024
f2d7ab8
cleanup
trajan0x Feb 29, 2024
cd760c2
more cleanup
trajan0x Feb 29, 2024
4055168
Merge pull request #2129 from synapsecns/feat/relayer-rebalance-gener…
trajan0x Feb 29, 2024
7ff3b2a
[goreleaser]
trajan0x Feb 29, 2024
d91525a
Feat: add tracing
dwasse Feb 29, 2024
ecbdc0e
Merge branch 'feat/relayer-rebalance' of github.com:synapsecns/sangui…
dwasse Feb 29, 2024
edeeb0e
[goreleaser]
dwasse Feb 29, 2024
5743f58
Feat: add more tracing around rebalance trigger
dwasse Mar 1, 2024
d8deade
[goreleaser]
dwasse Mar 1, 2024
e1296a4
Feat: add MaxRebalanceAmount and getter test
dwasse Mar 1, 2024
edae452
Feat: incorporate MaxRebalanceAmount and test
dwasse Mar 1, 2024
ab2a9ab
Feat: use big.Float for MaxRebalanceAmount parsing
dwasse Mar 1, 2024
996a82b
[goreleaser]
dwasse Mar 1, 2024
93a1e04
Feat: add check for address match in cctp event
dwasse Mar 1, 2024
b0f9d22
[goreleaser]
dwasse Mar 1, 2024
3285fe6
Cleanup: lint
dwasse Mar 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package listener

import (
"context"

"github.com/ethereum/go-ethereum/common"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/ethergo/client"
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
)

Expand All @@ -21,18 +21,19 @@ func (c chainListener) GetMetadata(ctx context.Context) (startBlock, chainID uin
}

type TestChainListenerArgs struct {
Address common.Address
Client client.EVM
Contract *fastbridge.FastBridgeRef
Store reldb.Service
Handler metrics.Handler
Address common.Address
InitialBlock uint64
Client client.EVM
Store reldb.Service
Handler metrics.Handler
}

func NewTestChainListener(args TestChainListenerArgs) TestChainListener {
return &chainListener{
client: args.Client,
contract: args.Contract,
store: args.Store,
handler: args.Handler,
client: args.Client,
address: args.Address,
initialBlock: args.InitialBlock,
store: args.Store,
handler: args.Handler,
Comment on lines +32 to +36
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The verification process revealed that while one usage of NewTestChainListener in suite_test.go has been correctly updated to include both the Address and InitialBlock fields, another usage has been updated to include only the Address field, omitting the InitialBlock field. This indicates that not all references and usages of NewTestChainListener throughout the test suite have been updated accordingly to pass the correct arguments, as required by the modifications in the TestChainListenerArgs struct.

Analysis chain

The NewTestChainListener function has been updated to initialize a chainListener with the new Address and InitialBlock fields. This change is consistent and necessary due to the modifications in the TestChainListenerArgs struct. It's important to ensure that all references and usages of NewTestChainListener throughout the test suite are updated accordingly to pass the correct arguments.


Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all usages of NewTestChainListener have been updated to include the new arguments.
rg 'NewTestChainListener\(' --type go

Length of output: 384



Script:

#!/bin/bash
# Extract the relevant lines from suite_test.go where NewTestChainListener is called to inspect the arguments.
rg 'NewTestChainListener\(' services/rfq/relayer/listener/suite_test.go -A 5

Length of output: 479

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import (
"context"
"errors"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ipfs/go-log"
"github.com/jpillora/backoff"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/ethergo/client"
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
"math/big"
"time"
)

// ContractListener listens for chain events and calls HandleLog.
Expand All @@ -37,11 +35,12 @@ type ContractListener interface {
type HandleLog func(ctx context.Context, log types.Log) error

type chainListener struct {
client client.EVM
contract *fastbridge.FastBridgeRef
store reldb.Service
Comment thread
dwasse marked this conversation as resolved.
handler metrics.Handler
backoff *backoff.Backoff
client client.EVM
address common.Address
initialBlock uint64
store reldb.Service
handler metrics.Handler
backoff *backoff.Backoff
// IMPORTANT! These fields cannot be used until they has been set. They are NOT
// set in the constructor
startBlock, chainID, latestBlock uint64
Expand All @@ -52,18 +51,14 @@ type chainListener struct {
var logger = log.Logger("chainlistener-logger")

// NewChainListener creates a new chain listener.
func NewChainListener(omnirpcClient client.EVM, store reldb.Service, address common.Address, handler metrics.Handler) (ContractListener, error) {
fastBridge, err := fastbridge.NewFastBridgeRef(address, omnirpcClient)
if err != nil {
return nil, fmt.Errorf("could not create fast bridge contract: %w", err)
}

func NewChainListener(omnirpcClient client.EVM, store reldb.Service, address common.Address, initialBlock uint64, handler metrics.Handler) (ContractListener, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this/the test to ethergo and import if we can

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mention this b/c I've recreated this exact file twice in feat/communication

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this here: 4febe40

Now lives in ethergo/chain/listener package

return &chainListener{
handler: handler,
store: store,
client: omnirpcClient,
contract: fastBridge,
backoff: newBackoffConfig(),
handler: handler,
address: address,
initialBlock: initialBlock,
store: store,
client: omnirpcClient,
backoff: newBackoffConfig(),
}, nil
}

Expand Down Expand Up @@ -91,13 +86,12 @@ func (c *chainListener) Listen(ctx context.Context, handler HandleLog) (err erro
if err != nil {
logger.Warn(err)
}

}
}
}

func (c *chainListener) Address() common.Address {
return c.contract.Address()
return c.address
}

func (c *chainListener) LatestBlock() uint64 {
Expand Down Expand Up @@ -125,9 +119,8 @@ func (c *chainListener) doPoll(parentCtx context.Context, handler HandleLog) (er
}

// Check if latest block is the same as start block (for chains with slow block times)

if c.latestBlock == c.startBlock {
return
return nil
}

// Handle if the listener is more than one get logs range behind the head
Expand Down Expand Up @@ -164,7 +157,7 @@ func (c *chainListener) doPoll(parentCtx context.Context, handler HandleLog) (er
}

func (c chainListener) getMetadata(parentCtx context.Context) (startBlock, chainID uint64, err error) {
var deployBlock, lastIndexed uint64
var lastIndexed uint64
ctx, span := c.handler.Tracer().Start(parentCtx, "getMetadata")

defer func() {
Expand All @@ -173,48 +166,29 @@ func (c chainListener) getMetadata(parentCtx context.Context) (startBlock, chain

// TODO: consider some kind of backoff here in case rpcs are down at boot.
// this becomes more of an issue as we add more chains
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
Comment thread
dwasse marked this conversation as resolved.
deployBlock, err := c.contract.DeployBlock(&bind.CallOpts{Context: ctx})
if err != nil {
return fmt.Errorf("could not get deploy block: %w", err)
}

startBlock = deployBlock.Uint64()
return nil
})

g.Go(func() error {
// TODO: one thing I've been going back and forth on is whether or not this method should be chain aware
// passing in the chain ID would allow us to pull everything directly from the config, but be less testable
// for now, this is probably the best solution for testability, but it's certainly a bit annoying we need to do
// an rpc call in order to get the chain id
//
rpcChainID, err := c.client.ChainID(ctx)
if err != nil {
return fmt.Errorf("could not get chain ID: %w", err)
}
chainID = rpcChainID.Uint64()

lastIndexed, err = c.store.LatestBlockForChain(ctx, chainID)
if errors.Is(err, reldb.ErrNoLatestBlockForChainID) {
// TODO: consider making this negative 1, requires type change
lastIndexed = 0
return nil
}
if err != nil {
return fmt.Errorf("could not get the latest block for chainID: %w", err)
}
return nil
})

err = g.Wait()
// TODO: one thing I've been going back and forth on is whether or not this method should be chain aware
// passing in the chain ID would allow us to pull everything directly from the config, but be less testable
// for now, this is probably the best solution for testability, but it's certainly a bit annoying we need to do
// an rpc call in order to get the chain id
//
rpcChainID, err := c.client.ChainID(ctx)
if err != nil {
return 0, 0, fmt.Errorf("could not get metadata: %w", err)
return 0, 0, fmt.Errorf("could not get chain ID: %w", err)
}
chainID = rpcChainID.Uint64()

lastIndexed, err = c.store.LatestBlockForChain(ctx, chainID)
if errors.Is(err, reldb.ErrNoLatestBlockForChainID) {
// TODO: consider making this negative 1, requires type change
lastIndexed = 0
} else if err != nil {
return 0, 0, fmt.Errorf("could not get the latest block for chainID: %w", err)
}

if lastIndexed > deployBlock {
if lastIndexed > c.startBlock {
startBlock = lastIndexed
} else {
startBlock = c.initialBlock
}

return startBlock, chainID, nil
Expand All @@ -233,6 +207,6 @@ func (c chainListener) buildFilterQuery(fromBlock, toBlock uint64) ethereum.Filt
return ethereum.FilterQuery{
FromBlock: new(big.Int).SetUint64(fromBlock),
ToBlock: new(big.Int).SetUint64(toBlock),
Addresses: []common.Address{c.contract.Address()},
Addresses: []common.Address{c.address},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"time"

"github.com/brianvoe/gofakeit/v6"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/synapsecns/sanguine/ethergo/chain/listener"
"github.com/synapsecns/sanguine/services/rfq/contracts/testcontracts/fastbridgemock"
"github.com/synapsecns/sanguine/services/rfq/relayer/listener"
)

func (l *ListenerTestSuite) TestListenForEvents() {
Expand All @@ -27,7 +28,7 @@ func (l *ListenerTestSuite) TestListenForEvents() {
testAddress := common.BigToAddress(big.NewInt(int64(i)))
auth := l.backend.GetTxContext(l.GetTestContext(), nil)

//nolint: typecheck
//nolint:typecheck
txID := [32]byte(crypto.Keccak256(testAddress.Bytes()))
bridgeRequestTX, err := handle.MockBridgeRequest(auth.TransactOpts, txID, testAddress, fastbridgemock.IFastBridgeBridgeParams{
DstChainId: gofakeit.Uint32(),
Expand Down Expand Up @@ -72,7 +73,10 @@ func (l *ListenerTestSuite) TestListenForEvents() {

wg.Wait()

cl, err := listener.NewChainListener(l.backend, l.store, handle.Address(), l.metrics)
startBlock, err := handle.DeployBlock(&bind.CallOpts{Context: l.GetTestContext()})
l.NoError(err)

cl, err := listener.NewChainListener(l.backend, l.store, handle.Address(), uint64(startBlock.Int64()), l.metrics)
l.NoError(err)

eventCount := 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package listener_test

import (
"math/big"
"testing"

"github.com/Flaque/filet"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/suite"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/core/testsuite"
"github.com/synapsecns/sanguine/ethergo/backends"
"github.com/synapsecns/sanguine/ethergo/backends/geth"
"github.com/synapsecns/sanguine/ethergo/chain/listener"
"github.com/synapsecns/sanguine/ethergo/contracts"
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge"
"github.com/synapsecns/sanguine/services/rfq/relayer/listener"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb/sqlite"
"github.com/synapsecns/sanguine/services/rfq/testutil"
"math/big"
"testing"
)

const chainID = 10
Expand Down Expand Up @@ -55,31 +55,30 @@ func (l *ListenerTestSuite) SetupTest() {
}

func (l *ListenerTestSuite) TestGetMetadataNoStore() {
deployBlock, err := l.fastBridge.DeployBlock(&bind.CallOpts{Context: l.GetTestContext()})
l.NoError(err)

// nothing stored, should use start block
cl := listener.NewTestChainListener(listener.TestChainListenerArgs{
Address: common.Address{},
Client: l.backend,
Contract: l.fastBridge,
Store: l.store,
Handler: l.metrics,
Address: l.fastBridge.Address(),
InitialBlock: deployBlock.Uint64(),
Client: l.backend,
Store: l.store,
Handler: l.metrics,
})

startBlock, myChainID, err := cl.GetMetadata(l.GetTestContext())
l.NoError(err)
l.Equal(myChainID, uint64(chainID))

deployBlock, err := l.fastBridge.DeployBlock(&bind.CallOpts{Context: l.GetTestContext()})
l.NoError(err)
l.Equal(startBlock, deployBlock.Uint64())
}

func (l *ListenerTestSuite) TestStartBlock() {
cl := listener.NewTestChainListener(listener.TestChainListenerArgs{
Address: common.Address{},
Client: l.backend,
Contract: l.fastBridge,
Store: l.store,
Handler: l.metrics,
Address: l.fastBridge.Address(),
Client: l.backend,
Store: l.store,
Handler: l.metrics,
})

deployBlock, err := l.fastBridge.DeployBlock(&bind.CallOpts{Context: l.GetTestContext()})
Expand Down
Loading