From b306f01edd3f9e783319b0f1edc6014ec4fbc87d Mon Sep 17 00:00:00 2001 From: renaynay <41963722+renaynay@users.noreply.github.com> Date: Sun, 11 Oct 2020 22:37:20 +0200 Subject: [PATCH 1/4] add a method to eth, unsafe? chainID --- eth/api.go | 5 +++++ internal/web3ext/web3ext.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/eth/api.go b/eth/api.go index 76118e2d7fc6..5a2d44a89139 100644 --- a/eth/api.go +++ b/eth/api.go @@ -75,6 +75,11 @@ func (api *PublicEthereumAPI) ChainId() hexutil.Uint64 { return (hexutil.Uint64)(chainID.Uint64()) } +// ChainIDFromConfig returns the chain id for the current ethereum chain config. +func (api *PublicEthereumAPI) ChainIDFromConfig() hexutil.Uint64 { + return (hexutil.Uint64)(api.e.blockchain.Config().ChainID.Uint64()) +} + // PublicMinerAPI provides an API to control the miner. // It offers only methods that operate on data that pose no security risk when it is publicly accessible. type PublicMinerAPI struct { diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 20e0ea11f56f..5b2c87da3b08 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -482,6 +482,11 @@ web3._extend({ call: 'eth_chainId', params: 0 }), + new web3._extend.Method({ + name: 'chainIDFromConfig', + call: 'eth_chainIDFromConfig', + params: 0 + }), new web3._extend.Method({ name: 'sign', call: 'eth_sign', From 871a47bec25bc5f156f5ffd217ba5094e096a68d Mon Sep 17 00:00:00 2001 From: renaynay <41963722+renaynay@users.noreply.github.com> Date: Mon, 12 Oct 2020 12:19:38 +0200 Subject: [PATCH 2/4] chainID returns config chainID, extra method EIP155 to check if blockchain past eip155 block --- eth/api.go | 13 ++++++------- internal/web3ext/web3ext.go | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/eth/api.go b/eth/api.go index 5a2d44a89139..6bac506dc36d 100644 --- a/eth/api.go +++ b/eth/api.go @@ -66,18 +66,17 @@ func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 { return hexutil.Uint64(api.e.Miner().HashRate()) } -// ChainId is the EIP-155 replay-protection chain id for the current ethereum chain config. +// ChainId is the chain id for the current ethereum chain config. func (api *PublicEthereumAPI) ChainId() hexutil.Uint64 { chainID := new(big.Int) - if config := api.e.blockchain.Config(); config.IsEIP155(api.e.blockchain.CurrentBlock().Number()) { - chainID = config.ChainID - } + chainID = api.e.blockchain.Config().ChainID return (hexutil.Uint64)(chainID.Uint64()) } -// ChainIDFromConfig returns the chain id for the current ethereum chain config. -func (api *PublicEthereumAPI) ChainIDFromConfig() hexutil.Uint64 { - return (hexutil.Uint64)(api.e.blockchain.Config().ChainID.Uint64()) +// IsEIP155 returns whether the current chain is on or past the +// EIP-155 replay-protection fork block. +func (api *PublicEthereumAPI) IsEIP155() bool { + return api.e.blockchain.Config().IsEIP155(api.e.blockchain.CurrentBlock().Number()) } // PublicMinerAPI provides an API to control the miner. diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 5b2c87da3b08..465af31cf768 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -483,8 +483,8 @@ web3._extend({ params: 0 }), new web3._extend.Method({ - name: 'chainIDFromConfig', - call: 'eth_chainIDFromConfig', + name: 'isEIP155', + call: 'eth_isEIP155', params: 0 }), new web3._extend.Method({ From 0af9293cb7445efa2cce5989075aa44aace0ef0f Mon Sep 17 00:00:00 2001 From: renaynay <41963722+renaynay@users.noreply.github.com> Date: Wed, 25 Nov 2020 09:33:45 +0100 Subject: [PATCH 3/4] simplify --- eth/api.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/eth/api.go b/eth/api.go index 6bac506dc36d..a4bd2be0262b 100644 --- a/eth/api.go +++ b/eth/api.go @@ -68,9 +68,7 @@ func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 { // ChainId is the chain id for the current ethereum chain config. func (api *PublicEthereumAPI) ChainId() hexutil.Uint64 { - chainID := new(big.Int) - chainID = api.e.blockchain.Config().ChainID - return (hexutil.Uint64)(chainID.Uint64()) + return (hexutil.Uint64)(api.e.blockchain.Config().ChainID.Uint64()) } // IsEIP155 returns whether the current chain is on or past the From 4310f635659ff4791f201c2d9d9124967ece3d73 Mon Sep 17 00:00:00 2001 From: renaynay <41963722+renaynay@users.noreply.github.com> Date: Tue, 8 Dec 2020 12:39:25 +0100 Subject: [PATCH 4/4] return err if chain not synced beyond EIP155 fork block --- eth/api.go | 16 +++++++--------- internal/web3ext/web3ext.go | 5 ----- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/eth/api.go b/eth/api.go index a4bd2be0262b..670e7d8eeeca 100644 --- a/eth/api.go +++ b/eth/api.go @@ -66,15 +66,13 @@ func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 { return hexutil.Uint64(api.e.Miner().HashRate()) } -// ChainId is the chain id for the current ethereum chain config. -func (api *PublicEthereumAPI) ChainId() hexutil.Uint64 { - return (hexutil.Uint64)(api.e.blockchain.Config().ChainID.Uint64()) -} - -// IsEIP155 returns whether the current chain is on or past the -// EIP-155 replay-protection fork block. -func (api *PublicEthereumAPI) IsEIP155() bool { - return api.e.blockchain.Config().IsEIP155(api.e.blockchain.CurrentBlock().Number()) +// ChainId is the EIP-155 replay-protection chain id for the current ethereum chain config. +func (api *PublicEthereumAPI) ChainId() (hexutil.Uint64, error) { + // if current block is at or past the EIP-155 replay-protection fork block, return chainID from config + if config := api.e.blockchain.Config(); config.IsEIP155(api.e.blockchain.CurrentBlock().Number()) { + return (hexutil.Uint64)(config.ChainID.Uint64()), nil + } + return hexutil.Uint64(0), fmt.Errorf("chain not synced beyond EIP-155 replay-protection fork block") } // PublicMinerAPI provides an API to control the miner. diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 465af31cf768..20e0ea11f56f 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -482,11 +482,6 @@ web3._extend({ call: 'eth_chainId', params: 0 }), - new web3._extend.Method({ - name: 'isEIP155', - call: 'eth_isEIP155', - params: 0 - }), new web3._extend.Method({ name: 'sign', call: 'eth_sign',