@@ -21,6 +21,7 @@ import (
21
21
"encoding/binary"
22
22
"errors"
23
23
"fmt"
24
+ "maps"
24
25
"math/big"
25
26
26
27
"github.com/consensys/gnark-crypto/ecc"
@@ -46,9 +47,12 @@ type PrecompiledContract interface {
46
47
Run (input []byte ) ([]byte , error ) // Run runs the precompiled contract
47
48
}
48
49
50
+ // PrecompiledContracts contains the precompiled contracts supported at the given fork.
51
+ type PrecompiledContracts map [common.Address ]PrecompiledContract
52
+
49
53
// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
50
54
// contracts used in the Frontier and Homestead releases.
51
- var PrecompiledContractsHomestead = map [common. Address ] PrecompiledContract {
55
+ var PrecompiledContractsHomestead = PrecompiledContracts {
52
56
common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
53
57
common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
54
58
common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
@@ -57,7 +61,7 @@ var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
57
61
58
62
// PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
59
63
// contracts used in the Byzantium release.
60
- var PrecompiledContractsByzantium = map [common. Address ] PrecompiledContract {
64
+ var PrecompiledContractsByzantium = PrecompiledContracts {
61
65
common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
62
66
common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
63
67
common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
@@ -70,7 +74,7 @@ var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
70
74
71
75
// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
72
76
// contracts used in the Istanbul release.
73
- var PrecompiledContractsIstanbul = map [common. Address ] PrecompiledContract {
77
+ var PrecompiledContractsIstanbul = PrecompiledContracts {
74
78
common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
75
79
common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
76
80
common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
@@ -84,7 +88,7 @@ var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
84
88
85
89
// PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum
86
90
// contracts used in the Berlin release.
87
- var PrecompiledContractsBerlin = map [common. Address ] PrecompiledContract {
91
+ var PrecompiledContractsBerlin = PrecompiledContracts {
88
92
common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
89
93
common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
90
94
common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
@@ -98,7 +102,7 @@ var PrecompiledContractsBerlin = map[common.Address]PrecompiledContract{
98
102
99
103
// PrecompiledContractsCancun contains the default set of pre-compiled Ethereum
100
104
// contracts used in the Cancun release.
101
- var PrecompiledContractsCancun = map [common. Address ] PrecompiledContract {
105
+ var PrecompiledContractsCancun = PrecompiledContracts {
102
106
common .BytesToAddress ([]byte {0x1 }): & ecrecover {},
103
107
common .BytesToAddress ([]byte {0x2 }): & sha256hash {},
104
108
common .BytesToAddress ([]byte {0x3 }): & ripemd160hash {},
@@ -113,7 +117,7 @@ var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{
113
117
114
118
// PrecompiledContractsPrague contains the set of pre-compiled Ethereum
115
119
// contracts used in the Prague release.
116
- var PrecompiledContractsPrague = map [common. Address ] PrecompiledContract {
120
+ var PrecompiledContractsPrague = PrecompiledContracts {
117
121
common .BytesToAddress ([]byte {0x01 }): & ecrecover {},
118
122
common .BytesToAddress ([]byte {0x02 }): & sha256hash {},
119
123
common .BytesToAddress ([]byte {0x03 }): & ripemd160hash {},
@@ -169,7 +173,31 @@ func init() {
169
173
}
170
174
}
171
175
172
- // ActivePrecompiles returns the precompiles enabled with the current configuration.
176
+ func activePrecompiledContracts (rules params.Rules ) PrecompiledContracts {
177
+ switch {
178
+ case rules .IsVerkle :
179
+ return PrecompiledContractsVerkle
180
+ case rules .IsPrague :
181
+ return PrecompiledContractsPrague
182
+ case rules .IsCancun :
183
+ return PrecompiledContractsCancun
184
+ case rules .IsBerlin :
185
+ return PrecompiledContractsBerlin
186
+ case rules .IsIstanbul :
187
+ return PrecompiledContractsIstanbul
188
+ case rules .IsByzantium :
189
+ return PrecompiledContractsByzantium
190
+ default :
191
+ return PrecompiledContractsHomestead
192
+ }
193
+ }
194
+
195
+ // ActivePrecompiledContracts returns a copy of precompiled contracts enabled with the current configuration.
196
+ func ActivePrecompiledContracts (rules params.Rules ) PrecompiledContracts {
197
+ return maps .Clone (activePrecompiledContracts (rules ))
198
+ }
199
+
200
+ // ActivePrecompiles returns the precompile addresses enabled with the current configuration.
173
201
func ActivePrecompiles (rules params.Rules ) []common.Address {
174
202
switch {
175
203
case rules .IsPrague :
0 commit comments