Skip to content

Commit

Permalink
fix(zetaclient): use on-chain chain struct (#2834)
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Sep 10, 2024
1 parent 7f18905 commit 6a83fd9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
9 changes: 5 additions & 4 deletions zetaclient/chains/evm/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayevm.sol"

"github.com/zeta-chain/zetacore/pkg/bg"
"github.com/zeta-chain/zetacore/pkg/chains"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/chains/base"
"github.com/zeta-chain/zetacore/zetaclient/chains/evm"
"github.com/zeta-chain/zetacore/zetaclient/chains/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/db"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
)
Expand Down Expand Up @@ -61,8 +61,9 @@ type priorityFeeConfig struct {
// NewObserver returns a new EVM chain observer
func NewObserver(
ctx context.Context,
evmCfg config.EVMConfig,
chain chains.Chain,
evmClient interfaces.EVMRPCClient,
evmJSONRPC interfaces.EVMJSONRPCClient,
chainParams observertypes.ChainParams,
zetacoreClient interfaces.ZetacoreClient,
tss interfaces.TSSSigner,
Expand All @@ -72,7 +73,7 @@ func NewObserver(
) (*Observer, error) {
// create base observer
baseObserver, err := base.NewObserver(
evmCfg.Chain,
chain,
chainParams,
zetacoreClient,
tss,
Expand All @@ -90,7 +91,7 @@ func NewObserver(
ob := &Observer{
Observer: *baseObserver,
evmClient: evmClient,
evmJSONRPC: ethrpc.NewEthRPC(evmCfg.Endpoint),
evmJSONRPC: evmJSONRPC,
outboundConfirmedReceipts: make(map[string]*ethtypes.Receipt),
outboundConfirmedTransactions: make(map[string]*ethtypes.Transaction),
priorityFeeConfig: priorityFeeConfig{},
Expand Down
28 changes: 24 additions & 4 deletions zetaclient/chains/evm/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func MockEVMObserver(
evmClient = mocks.NewMockEvmClient().WithBlockNumber(1000)
}

// use default mock evm client if not provided
if evmJSONRPC == nil {
evmJSONRPC = mocks.NewMockJSONRPCClient()
}

// use default mock zetacore client if not provided
if zetacoreClient == nil {
zetacoreClient = mocks.NewZetacoreClient(t).
Expand All @@ -113,7 +118,7 @@ func MockEVMObserver(
tss = mocks.NewTSSMainnet()
}
// create AppContext
appContext, evmCfg := getAppContext(t, chain, "", &params)
appContext, _ := getAppContext(t, chain, "", &params)

database, err := db.NewFromSqliteInMemory(true)
require.NoError(t, err)
Expand All @@ -122,9 +127,19 @@ func MockEVMObserver(
logger := base.Logger{Std: testLogger, Compliance: testLogger}

// create observer
ob, err := observer.NewObserver(ctx, evmCfg, evmClient, params, zetacoreClient, tss, database, logger, nil)
ob, err := observer.NewObserver(
ctx,
chain,
evmClient,
evmJSONRPC,
params,
zetacoreClient,
tss,
database,
logger,
nil,
)
require.NoError(t, err)
ob.WithEvmJSONRPC(evmJSONRPC)
ob.WithLastBlock(lastBlock)

return ob, appContext
Expand All @@ -143,6 +158,7 @@ func Test_NewObserver(t *testing.T) {
evmCfg config.EVMConfig
chainParams observertypes.ChainParams
evmClient interfaces.EVMRPCClient
evmJSONRPC interfaces.EVMJSONRPCClient
tss interfaces.TSSSigner
logger base.Logger
before func()
Expand All @@ -159,6 +175,7 @@ func Test_NewObserver(t *testing.T) {
},
chainParams: params,
evmClient: mocks.NewMockEvmClient().WithBlockNumber(1000),
evmJSONRPC: mocks.NewMockJSONRPCClient(),
tss: mocks.NewTSSMainnet(),
logger: base.Logger{},
ts: nil,
Expand All @@ -172,6 +189,7 @@ func Test_NewObserver(t *testing.T) {
},
chainParams: params,
evmClient: mocks.NewMockEvmClient().WithError(fmt.Errorf("error RPC")),
evmJSONRPC: mocks.NewMockJSONRPCClient(),
tss: mocks.NewTSSMainnet(),
logger: base.Logger{},
ts: nil,
Expand All @@ -186,6 +204,7 @@ func Test_NewObserver(t *testing.T) {
},
chainParams: params,
evmClient: mocks.NewMockEvmClient().WithBlockNumber(1000),
evmJSONRPC: mocks.NewMockJSONRPCClient(),
tss: mocks.NewTSSMainnet(),
before: func() {
envVar := base.EnvVarLatestBlockByChain(chain)
Expand Down Expand Up @@ -222,8 +241,9 @@ func Test_NewObserver(t *testing.T) {
// create observer
ob, err := observer.NewObserver(
ctx,
tt.evmCfg,
chain,
tt.evmClient,
tt.evmJSONRPC,
tt.chainParams,
zetacoreClient,
tt.tss,
Expand Down
10 changes: 4 additions & 6 deletions zetaclient/chains/evm/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,11 @@ func getNewEvmChainObserver(t *testing.T, tss interfaces.TSSSigner) (*observer.O
if tss == nil {
tss = mocks.NewTSSMainnet()
}
cfg := config.New(false)

// prepare mock arguments to create observer
evmcfg := config.EVMConfig{Chain: chains.BscMainnet, Endpoint: "http://localhost:8545"}
evmClient := mocks.NewMockEvmClient().WithBlockNumber(1000)
params := mocks.MockChainParams(evmcfg.Chain.ChainId, 10)
cfg.EVMChainConfigs[chains.BscMainnet.ChainId] = evmcfg
//appContext := context.New(cfg, zerolog.Nop())
evmJSONRPCClient := mocks.NewMockJSONRPCClient()
params := mocks.MockChainParams(chains.BscMainnet.ChainId, 10)
logger := base.Logger{}
ts := &metrics.TelemetryServer{}

Expand All @@ -85,8 +82,9 @@ func getNewEvmChainObserver(t *testing.T, tss interfaces.TSSSigner) (*observer.O

return observer.NewObserver(
ctx,
evmcfg,
chains.BscMainnet,
evmClient,
evmJSONRPCClient,
params,
mocks.NewZetacoreClient(t),
tss,
Expand Down
6 changes: 5 additions & 1 deletion zetaclient/orchestrator/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"
solrpc "github.com/gagliardetto/solana-go/rpc"
ethrpc2 "github.com/onrik/ethrpc"
"github.com/pkg/errors"

"github.com/zeta-chain/zetacore/zetaclient/chains/base"
Expand Down Expand Up @@ -298,11 +299,14 @@ func syncObserverMap(
continue
}

evmJSONRPCClient := ethrpc2.NewEthRPC(cfg.Endpoint, ethrpc2.WithHttpClient(httpClient))

// create EVM chain observer
observer, err := evmobserver.NewObserver(
ctx,
cfg,
*rawChain,
evmClient,
evmJSONRPCClient,
*params,
client,
tss,
Expand Down

0 comments on commit 6a83fd9

Please sign in to comment.