From fa2ebae226805207771bede207c1454628586aaa Mon Sep 17 00:00:00 2001 From: Valentin Trinque Date: Tue, 13 Feb 2024 11:26:06 +0100 Subject: [PATCH] fix: Value assets chain ID using network parameters Non-validator nodes do not have a Ethereum client setup, so we can't use it to value the assets' chain ID. --- core/assets/assets.go | 7 ++++++- core/assets/erc20/erc20.go | 1 - core/assets/erc20/erc20_test.go | 4 ---- core/assets/erc20/mocks/eth_client_mock.go | 14 -------------- core/assets/snapshot.go | 2 +- core/client/eth/client.go | 4 ---- core/protocol/all_services.go | 12 ++++++++++++ 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/core/assets/assets.go b/core/assets/assets.go index 60bd59a8cce..c516a22f41c 100644 --- a/core/assets/assets.go +++ b/core/assets/assets.go @@ -75,7 +75,8 @@ type Service struct { ethToVega map[string]string isValidator bool - bridgeView ERC20BridgeView + bridgeView ERC20BridgeView + ethereumChainID string } func New( @@ -462,3 +463,7 @@ func (s *Service) validateAsset(a *Asset) error { return err } + +func (s *Service) OnEthereumChainIDUpdated(chainID string) { + s.ethereumChainID = chainID +} diff --git a/core/assets/erc20/erc20.go b/core/assets/erc20/erc20.go index 6ccf85491d4..21277de1406 100644 --- a/core/assets/erc20/erc20.go +++ b/core/assets/erc20/erc20.go @@ -44,7 +44,6 @@ type ETHClient interface { CollateralBridgeAddress() ethcommon.Address CurrentHeight(context.Context) (uint64, error) ConfirmationsRequired() uint64 - ConfiguredChainID() string } type ERC20 struct { diff --git a/core/assets/erc20/erc20_test.go b/core/assets/erc20/erc20_test.go index cf70a8e6600..86aa8f0144a 100644 --- a/core/assets/erc20/erc20_test.go +++ b/core/assets/erc20/erc20_test.go @@ -122,10 +122,6 @@ type testEthClient struct { bind.ContractBackend } -func (testEthClient) ConfiguredChainID() string { - return "1" -} - func (testEthClient) HeaderByNumber(context.Context, *big.Int) (*ethtypes.Header, error) { return nil, nil } diff --git a/core/assets/erc20/mocks/eth_client_mock.go b/core/assets/erc20/mocks/eth_client_mock.go index 64516a98c68..bf0ca67a3c4 100644 --- a/core/assets/erc20/mocks/eth_client_mock.go +++ b/core/assets/erc20/mocks/eth_client_mock.go @@ -82,20 +82,6 @@ func (mr *MockETHClientMockRecorder) CollateralBridgeAddress() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CollateralBridgeAddress", reflect.TypeOf((*MockETHClient)(nil).CollateralBridgeAddress)) } -// ConfiguredChainID mocks base method. -func (m *MockETHClient) ConfiguredChainID() string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ConfiguredChainID") - ret0, _ := ret[0].(string) - return ret0 -} - -// ConfiguredChainID indicates an expected call of ConfiguredChainID. -func (mr *MockETHClientMockRecorder) ConfiguredChainID() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConfiguredChainID", reflect.TypeOf((*MockETHClient)(nil).ConfiguredChainID)) -} - // ConfirmationsRequired mocks base method. func (m *MockETHClient) ConfirmationsRequired() uint64 { m.ctrl.T.Helper() diff --git a/core/assets/snapshot.go b/core/assets/snapshot.go index 849e654fd81..874804f1906 100644 --- a/core/assets/snapshot.go +++ b/core/assets/snapshot.go @@ -206,7 +206,7 @@ func (s *Service) applyMigrations(ctx context.Context, p *types.Asset) { // the chain ID they originated from. So, when loaded, assets without a chain // ID are automatically considered to originate from Ethereum Mainnet. if erc20 := p.Details.GetERC20(); erc20 != nil && erc20.ChainID == "" { - erc20.ChainID = s.ethClient.ConfiguredChainID() + erc20.ChainID = s.ethereumChainID // Ensure the assets are updated in the data-node. s.broker.Send(events.NewAssetEvent(ctx, *p)) } diff --git a/core/client/eth/client.go b/core/client/eth/client.go index 0cae254a55c..0ba521c82ff 100644 --- a/core/client/eth/client.go +++ b/core/client/eth/client.go @@ -70,10 +70,6 @@ func Dial(ctx context.Context, cfg Config) (*Client, error) { return &Client{ETHClient: newEthClientWrapper(ethClient), cfg: cfg}, nil } -func (c *Client) ConfiguredChainID() string { - return c.ethConfig.ChainID() -} - func (c *Client) UpdateEthereumConfig(ctx context.Context, ethConfig *types.EthereumConfig) error { if c == nil { return nil diff --git a/core/protocol/all_services.go b/core/protocol/all_services.go index f0885133e6d..6d3ed2a1243 100644 --- a/core/protocol/all_services.go +++ b/core/protocol/all_services.go @@ -877,6 +877,18 @@ func (svcs *allServices) setupNetParameters(powWatchers []netparams.WatchParam) return nil }, }, + { + Param: netparams.BlockchainsEthereumConfig, + Watcher: func(_ context.Context, cfg interface{}) error { + ethCfg, err := types.EthereumConfigFromUntypedProto(cfg) + if err != nil { + return fmt.Errorf("invalid ethereum configuration: %w", err) + } + + svcs.assets.OnEthereumChainIDUpdated(ethCfg.ChainID()) + return nil + }, + }, { Param: netparams.BlockchainsEthereumL2Configs, Watcher: func(ctx context.Context, cfg interface{}) error {