Skip to content

Commit

Permalink
Address PR comments [2]
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Jul 8, 2024
1 parent 358d971 commit 380d93c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 47 deletions.
4 changes: 2 additions & 2 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func start(_ *cobra.Command, _ []string) error {
}

// CreateSignerMap: This creates a map of all signers for each chain . Each signer is responsible for signing transactions for a particular chain
signerMap, err := CreateSignerMap(appContext, tss, logger, telemetryServer)
signerMap, err := CreateSignerMap(ctx, appContext, tss, logger, telemetryServer)
if err != nil {
log.Error().Err(err).Msg("CreateSignerMap")
return err
Expand All @@ -281,7 +281,7 @@ func start(_ *cobra.Command, _ []string) error {
dbpath := filepath.Join(userDir, ".zetaclient/chainobserver")

// Creates a map of all chain observers for each chain. Each chain observer is responsible for observing events on the chain and processing them.
observerMap, err := CreateChainObserverMap(appContext, zetacoreClient, tss, dbpath, logger, telemetryServer)
observerMap, err := CreateChainObserverMap(ctx, appContext, zetacoreClient, tss, dbpath, logger, telemetryServer)
if err != nil {
startLogger.Err(err).Msg("CreateChainObserverMap")
return err
Expand Down
8 changes: 7 additions & 1 deletion cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
gocontext "context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -57,6 +58,7 @@ func CreateZetacoreClient(cfg config.Config, hotkeyPassword string, logger zerol

// CreateSignerMap creates a map of ChainSigners for all chains in the config
func CreateSignerMap(
ctx gocontext.Context,
appContext *context.AppContext,
tss interfaces.TSSSigner,
logger base.Logger,
Expand All @@ -77,6 +79,7 @@ func CreateSignerMap(
mpiAddress := ethcommon.HexToAddress(evmChainParams.ConnectorContractAddress)
erc20CustodyAddress := ethcommon.HexToAddress(evmChainParams.Erc20CustodyContractAddress)
signer, err := evmsigner.NewSigner(
ctx,
evmConfig.Chain,
tss,
ts,
Expand All @@ -85,7 +88,8 @@ func CreateSignerMap(
config.GetConnectorABI(),
config.GetERC20CustodyABI(),
mpiAddress,
erc20CustodyAddress)
erc20CustodyAddress,
)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -108,6 +112,7 @@ func CreateSignerMap(

// CreateChainObserverMap creates a map of ChainObservers for all chains in the config
func CreateChainObserverMap(
ctx gocontext.Context,
appContext *context.AppContext,
zetacoreClient *zetacore.Client,
tss interfaces.TSSSigner,
Expand Down Expand Up @@ -136,6 +141,7 @@ func CreateChainObserverMap(

// create EVM chain observer
observer, err := evmobserver.NewObserver(
ctx,
evmConfig,
evmClient,
*chainParams,
Expand Down
10 changes: 5 additions & 5 deletions zetaclient/chains/evm/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (ob *Observer) ProcessInboundTrackers(ctx context.Context) error {
)
}

receipt, err := ob.evmClient.TransactionReceipt(context.Background(), ethcommon.HexToHash(tracker.TxHash))
receipt, err := ob.evmClient.TransactionReceipt(ctx, ethcommon.HexToHash(tracker.TxHash))

Check warning on line 134 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L134

Added line #L134 was not covered by tests
if err != nil {
return errors.Wrapf(
err,
Expand Down Expand Up @@ -168,7 +168,7 @@ func (ob *Observer) ProcessInboundTrackers(ctx context.Context) error {
// ObserveInbound observes the evm chain for inbounds and posts votes to zetacore
func (ob *Observer) ObserveInbound(ctx context.Context, sampledLogger zerolog.Logger) error {

Check warning on line 169 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L169

Added line #L169 was not covered by tests
// get and update latest block height
blockNumber, err := ob.evmClient.BlockNumber(context.Background())
blockNumber, err := ob.evmClient.BlockNumber(ctx)

Check warning on line 171 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L171

Added line #L171 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func (ob *Observer) ObserveZetaSent(ctx context.Context, startBlock, toBlock uin
iter, err := connector.FilterZetaSent(&bind.FilterOpts{
Start: startBlock,
End: &toBlock,
Context: context.TODO(),
Context: ctx,

Check warning on line 261 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L261

Added line #L261 was not covered by tests
}, []ethcommon.Address{}, []*big.Int{})
if err != nil {
ob.Logger().Chain.Warn().Err(err).Msgf(
Expand Down Expand Up @@ -341,7 +341,7 @@ func (ob *Observer) ObserveERC20Deposited(ctx context.Context, startBlock, toBlo
iter, err := erc20custodyContract.FilterDeposited(&bind.FilterOpts{
Start: startBlock,
End: &toBlock,
Context: context.TODO(),
Context: ctx,

Check warning on line 344 in zetaclient/chains/evm/observer/inbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/inbound.go#L344

Added line #L344 was not covered by tests
}, []ethcommon.Address{})
if err != nil {
ob.Logger().Inbound.Warn().Err(err).Msgf(
Expand Down Expand Up @@ -799,7 +799,7 @@ func (ob *Observer) ObserveTSSReceiveInBlock(ctx context.Context, blockNumber ui
for i := range block.Transactions {
tx := block.Transactions[i]
if ethcommon.HexToAddress(tx.To) == ob.TSS().EVMAddress() {
receipt, err := ob.evmClient.TransactionReceipt(context.Background(), ethcommon.HexToHash(tx.Hash))
receipt, err := ob.evmClient.TransactionReceipt(ctx, ethcommon.HexToHash(tx.Hash))
if err != nil {
return errors.Wrapf(err, "error getting receipt for inbound %s chain %d", tx.Hash, ob.Chain().ChainId)
}
Expand Down
25 changes: 13 additions & 12 deletions zetaclient/chains/evm/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Observer struct {

// NewObserver returns a new EVM chain observer
func NewObserver(
ctx context.Context,
evmCfg config.EVMConfig,
evmClient interfaces.EVMRPCClient,
chainParams observertypes.ChainParams,
Expand Down Expand Up @@ -90,7 +91,7 @@ func NewObserver(
}

// open database and load data
err = ob.LoadDB(dbpath)
err = ob.LoadDB(ctx, dbpath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -177,7 +178,7 @@ func (ob *Observer) Start(ctx context.Context) {
// WatchRPCStatus watches the RPC status of the evm chain
// TODO(revamp): move ticker to ticker file
// TODO(revamp): move inner logic to a separate function
func (ob *Observer) WatchRPCStatus(_ context.Context) error {
func (ob *Observer) WatchRPCStatus(ctx context.Context) error {

Check warning on line 181 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L181

Added line #L181 was not covered by tests
ob.Logger().Chain.Info().Msgf("Starting RPC status check for chain %d", ob.Chain().ChainId)
ticker := time.NewTicker(60 * time.Second)
for {
Expand All @@ -186,17 +187,17 @@ func (ob *Observer) WatchRPCStatus(_ context.Context) error {
if !ob.GetChainParams().IsSupported {
continue
}
bn, err := ob.evmClient.BlockNumber(context.Background())
bn, err := ob.evmClient.BlockNumber(ctx)

Check warning on line 190 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L190

Added line #L190 was not covered by tests
if err != nil {
ob.Logger().Chain.Error().Err(err).Msg("RPC Status Check error: RPC down?")
continue
}
gasPrice, err := ob.evmClient.SuggestGasPrice(context.Background())
gasPrice, err := ob.evmClient.SuggestGasPrice(ctx)

Check warning on line 195 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L195

Added line #L195 was not covered by tests
if err != nil {
ob.Logger().Chain.Error().Err(err).Msg("RPC Status Check error: RPC down?")
continue
}
header, err := ob.evmClient.HeaderByNumber(context.Background(), new(big.Int).SetUint64(bn))
header, err := ob.evmClient.HeaderByNumber(ctx, new(big.Int).SetUint64(bn))

Check warning on line 200 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L200

Added line #L200 was not covered by tests
if err != nil {
ob.Logger().Chain.Error().Err(err).Msg("RPC Status Check error: RPC down?")
continue
Expand Down Expand Up @@ -365,14 +366,14 @@ func (ob *Observer) TransactionByHash(txHash string) (*ethrpc.Transaction, bool,
}

// GetBlockHeaderCached get block header by number from cache
func (ob *Observer) GetBlockHeaderCached(blockNumber uint64) (*ethtypes.Header, error) {
func (ob *Observer) GetBlockHeaderCached(ctx context.Context, blockNumber uint64) (*ethtypes.Header, error) {
if result, ok := ob.HeaderCache().Get(blockNumber); ok {
if header, ok := result.(*ethtypes.Header); ok {
return header, nil
}
return nil, errors.New("cached value is not of type *ethtypes.Header")
}
header, err := ob.evmClient.HeaderByNumber(context.Background(), new(big.Int).SetUint64(blockNumber))
header, err := ob.evmClient.HeaderByNumber(ctx, new(big.Int).SetUint64(blockNumber))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -423,7 +424,7 @@ func (ob *Observer) BlockByNumber(blockNumber int) (*ethrpc.Block, error) {

// LoadDB open sql database and load data into EVM observer
// TODO(revamp): move to a db file
func (ob *Observer) LoadDB(dbPath string) error {
func (ob *Observer) LoadDB(ctx context.Context, dbPath string) error {
if dbPath == "" {
return errors.New("empty db path")
}
Expand All @@ -445,14 +446,14 @@ func (ob *Observer) LoadDB(dbPath string) error {
}

// load last block scanned
err = ob.LoadLastBlockScanned()
err = ob.LoadLastBlockScanned(ctx)

return err
}

// LoadLastBlockScanned loads the last scanned block from the database
// TODO(revamp): move to a db file
func (ob *Observer) LoadLastBlockScanned() error {
func (ob *Observer) LoadLastBlockScanned(ctx context.Context) error {
err := ob.Observer.LoadLastBlockScanned(ob.Logger().Chain)
if err != nil {
return errors.Wrapf(err, "error LoadLastBlockScanned for chain %d", ob.Chain().ChainId)
Expand All @@ -462,7 +463,7 @@ func (ob *Observer) LoadLastBlockScanned() error {
// 1. environment variable is set explicitly to "latest"
// 2. environment variable is empty and last scanned block is not found in DB
if ob.LastBlockScanned() == 0 {
blockNumber, err := ob.evmClient.BlockNumber(context.Background())
blockNumber, err := ob.evmClient.BlockNumber(ctx)
if err != nil {
return errors.Wrapf(err, "error BlockNumber for chain %d", ob.Chain().ChainId)
}
Expand All @@ -488,7 +489,7 @@ func (ob *Observer) postBlockHeader(ctx context.Context, tip uint64) error {
return fmt.Errorf("postBlockHeader: must post block confirmed block header: %d > %d", bn, tip)
}

header, err := ob.GetBlockHeaderCached(bn)
header, err := ob.GetBlockHeaderCached(ctx, bn)

Check warning on line 492 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L492

Added line #L492 was not covered by tests
if err != nil {
ob.Logger().Inbound.Error().Err(err).Msgf("postBlockHeader: error getting block: %d", bn)
return err
Expand Down
Loading

0 comments on commit 380d93c

Please sign in to comment.