From 6be279da746cc11a9550da03b5db72518cd7678b Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 1 Nov 2021 10:24:27 +0000 Subject: [PATCH] Add Quorum Privacy Precompile to ActivePrecompiles list This is currently only used for the experimental YoloV2 EIP 2929 (where all active precompiles for the current block height must be known) so not critical but worth fixing ahead of the concrete release that will be pulled in from a future upstream geth merge Also includes core/vm: fix Byzantium address list (#22603) (cherry picked from commit 44fe466999ce7f8b02de7e83de955f8847342309) --- core/vm/contracts.go | 2 +- core/vm/evm.go | 15 +++++++ core/vm/evm_test.go | 97 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 core/vm/evm_test.go diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 0c828b1cd9..0c76284809 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -113,7 +113,7 @@ func init() { PrecompiledAddressesHomestead = append(PrecompiledAddressesHomestead, k) } for k := range PrecompiledContractsByzantium { - PrecompiledAddressesHomestead = append(PrecompiledAddressesByzantium, k) + PrecompiledAddressesByzantium = append(PrecompiledAddressesByzantium, k) } for k := range PrecompiledContractsIstanbul { PrecompiledAddressesIstanbul = append(PrecompiledAddressesIstanbul, k) diff --git a/core/vm/evm.go b/core/vm/evm.go index 261c62903f..818418c2c8 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -60,6 +60,13 @@ type ( // ActivePrecompiles returns the addresses of the precompiles enabled with the current // configuration func (evm *EVM) ActivePrecompiles() []common.Address { + return append(evm.activePrecompiles(), evm.activeQuorumPrecompiles()...) +} + +// (Quorum) moved upstream ActivePrecompiles() logic to new method activePrecompiles() +// This functionality is part of an experimental feature so may be subject to future changes. Keeping the original code +// untouched in a new method should flag any changes from future merges. +func (evm *EVM) activePrecompiles() []common.Address { switch { case evm.chainRules.IsYoloV2: return PrecompiledAddressesYoloV2 @@ -72,6 +79,14 @@ func (evm *EVM) ActivePrecompiles() []common.Address { } } +func (evm *EVM) activeQuorumPrecompiles() []common.Address { + var p []common.Address + if evm.chainRules.IsPrivacyPrecompile { + p = append(p, common.QuorumPrivacyPrecompileContractAddress()) + } + return p +} + func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { var precompiles map[common.Address]PrecompiledContract switch { diff --git a/core/vm/evm_test.go b/core/vm/evm_test.go new file mode 100644 index 0000000000..ecef512c11 --- /dev/null +++ b/core/vm/evm_test.go @@ -0,0 +1,97 @@ +package vm + +import ( + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" + "github.com/stretchr/testify/require" +) + +func TestActivePrecompiles(t *testing.T) { + tests := []struct { + name string + evm *EVM + want []common.Address + }{ + { + name: "istanbul-plus-quorum-privacy", + evm: &EVM{ + chainRules: params.Rules{ + IsIstanbul: true, + IsPrivacyPrecompile: true, + }, + }, + want: []common.Address{ + common.BytesToAddress([]byte{1}), + common.BytesToAddress([]byte{2}), + common.BytesToAddress([]byte{3}), + common.BytesToAddress([]byte{4}), + common.BytesToAddress([]byte{5}), + common.BytesToAddress([]byte{6}), + common.BytesToAddress([]byte{7}), + common.BytesToAddress([]byte{8}), + common.BytesToAddress([]byte{9}), + common.QuorumPrivacyPrecompileContractAddress(), + }, + }, + { + name: "homestead-plus-quorum-privacy", + evm: &EVM{ + chainRules: params.Rules{ + IsHomestead: true, + IsPrivacyPrecompile: true, + }, + }, + want: []common.Address{ + common.BytesToAddress([]byte{1}), + common.BytesToAddress([]byte{2}), + common.BytesToAddress([]byte{3}), + common.BytesToAddress([]byte{4}), + common.QuorumPrivacyPrecompileContractAddress(), + }, + }, + { + name: "istanbul", + evm: &EVM{ + chainRules: params.Rules{ + IsIstanbul: true, + IsPrivacyPrecompile: false, + }, + }, + want: []common.Address{ + common.BytesToAddress([]byte{1}), + common.BytesToAddress([]byte{2}), + common.BytesToAddress([]byte{3}), + common.BytesToAddress([]byte{4}), + common.BytesToAddress([]byte{5}), + common.BytesToAddress([]byte{6}), + common.BytesToAddress([]byte{7}), + common.BytesToAddress([]byte{8}), + common.BytesToAddress([]byte{9}), + }, + }, + { + name: "homestead", + evm: &EVM{ + chainRules: params.Rules{ + IsHomestead: true, + IsPrivacyPrecompile: false, + }, + }, + want: []common.Address{ + common.BytesToAddress([]byte{1}), + common.BytesToAddress([]byte{2}), + common.BytesToAddress([]byte{3}), + common.BytesToAddress([]byte{4}), + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.evm.ActivePrecompiles() + require.ElementsMatchf(t, tt.want, got, "want: %v, got: %v", tt.want, got) + }) + } +}