@@ -21,6 +21,7 @@ import (
2121 "encoding/binary"
2222 "errors"
2323 "fmt"
24+ "maps"
2425 "math/big"
2526
2627 "github.com/ethereum/go-ethereum/common"
@@ -45,6 +46,9 @@ type PrecompiledContract interface {
4546 Run (input []byte ) ([]byte , error ) // Run runs the precompiled contract
4647}
4748
49+ // PrecompiledContracts contains the precompiled contracts supported at the given fork.
50+ type PrecompiledContracts map [common.Address ]PrecompiledContract
51+
4852// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
4953// contracts specified in EIP-2537. These are exported for testing purposes.
5054var PrecompiledContractsBLS = map [common.Address ]PrecompiledContract {
@@ -70,35 +74,39 @@ var (
7074
7175 // PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
7276 // contracts used in the Frontier and Homestead releases.
73- PrecompiledContractsHomestead map [common. Address ] PrecompiledContract
77+ PrecompiledContractsHomestead PrecompiledContracts
7478
7579 // PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
7680 // contracts used in the Byzantium release.
77- PrecompiledContractsByzantium map [common. Address ] PrecompiledContract
81+ PrecompiledContractsByzantium PrecompiledContracts
7882
7983 // PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
8084 // contracts used in the Istanbul release.
81- PrecompiledContractsIstanbul map [common. Address ] PrecompiledContract
85+ PrecompiledContractsIstanbul PrecompiledContracts
8286
8387 // PrecompiledContractsConsortium contains additional Consortium precompiled contract
8488 // beside PrecompiledContractsIstanbul
85- PrecompiledContractsConsortium map [common. Address ] PrecompiledContract
89+ PrecompiledContractsConsortium PrecompiledContracts
8690
8791 // PrecompiledContractsConsortium contains proof of possession precompiled contract
8892 // beside PrecompiledContractsConsortium
89- PrecompiledContractsConsortiumMiko map [common. Address ] PrecompiledContract
93+ PrecompiledContractsConsortiumMiko PrecompiledContracts
9094
9195 // PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum
9296 // contracts used in the Berlin release.
93- PrecompiledContractsBerlin map [common. Address ] PrecompiledContract
97+ PrecompiledContractsBerlin PrecompiledContracts
9498
9599 // PrecompiledContractsCancun contains the default set of pre-compiled Ethereum
96100 // contracts used in the Cancun release.
97- PrecompiledContractsCancun map [common.Address ]PrecompiledContract
101+ PrecompiledContractsCancun PrecompiledContracts
102+
103+ // PrecompiledContractsPrague contains the default set of pre-compiled Ethereum
104+ // contracts used in the Prague release.
105+ PrecompiledContractsPrague PrecompiledContracts
98106)
99107
100- func copyPrecompiledContract (contracts map [common. Address ] PrecompiledContract ) map [common. Address ] PrecompiledContract {
101- cpy := make (map [common. Address ] PrecompiledContract )
108+ func copyPrecompiledContract (contracts PrecompiledContracts ) PrecompiledContracts {
109+ cpy := make (PrecompiledContracts )
102110
103111 for address , contract := range contracts {
104112 cpy [address ] = contract
@@ -108,7 +116,7 @@ func copyPrecompiledContract(contracts map[common.Address]PrecompiledContract) m
108116}
109117
110118func init () {
111- PrecompiledContractsHomestead = map [common. Address ] PrecompiledContract {
119+ PrecompiledContractsHomestead = PrecompiledContracts {
112120 common .BytesToAddress ([]byte {1 }): & ecrecover {},
113121 common .BytesToAddress ([]byte {2 }): & sha256hash {},
114122 common .BytesToAddress ([]byte {3 }): & ripemd160hash {},
@@ -202,6 +210,30 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
202210 }
203211}
204212
213+ func activePrecompiledContracts (rules params.Rules ) PrecompiledContracts {
214+ switch {
215+ case rules .IsCancun :
216+ return PrecompiledContractsCancun
217+ case rules .IsBerlin :
218+ return PrecompiledContractsBerlin
219+ case rules .IsMiko :
220+ return PrecompiledContractsConsortiumMiko
221+ case rules .IsLastConsortiumV1Block , rules .IsConsortiumV2 :
222+ return PrecompiledContractsConsortium
223+ case rules .IsIstanbul :
224+ return PrecompiledContractsIstanbul
225+ case rules .IsByzantium :
226+ return PrecompiledContractsByzantium
227+ default :
228+ return PrecompiledContractsHomestead
229+ }
230+ }
231+
232+ // ActivePrecompiledContracts returns a copy of precompiled contracts enabled with the current configuration.
233+ func ActivePrecompiledContracts (rules params.Rules ) PrecompiledContracts {
234+ return maps .Clone (activePrecompiledContracts (rules ))
235+ }
236+
205237// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
206238// It returns
207239// - the returned bytes,
@@ -220,9 +252,7 @@ func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uin
220252 return output , suppliedGas , err
221253}
222254
223- var (
224- errBlacklistedAddress = errors .New ("address is blacklisted" )
225- )
255+ var errBlacklistedAddress = errors .New ("address is blacklisted" )
226256
227257type blacklistedAddress struct {}
228258
@@ -282,6 +312,7 @@ type sha256hash struct{}
282312func (c * sha256hash ) RequiredGas (input []byte ) uint64 {
283313 return uint64 (len (input )+ 31 )/ 32 * params .Sha256PerWordGas + params .Sha256BaseGas
284314}
315+
285316func (c * sha256hash ) Run (input []byte ) ([]byte , error ) {
286317 h := sha256 .Sum256 (input )
287318 return h [:], nil
@@ -297,6 +328,7 @@ type ripemd160hash struct{}
297328func (c * ripemd160hash ) RequiredGas (input []byte ) uint64 {
298329 return uint64 (len (input )+ 31 )/ 32 * params .Ripemd160PerWordGas + params .Ripemd160BaseGas
299330}
331+
300332func (c * ripemd160hash ) Run (input []byte ) ([]byte , error ) {
301333 ripemd := ripemd160 .New ()
302334 ripemd .Write (input )
@@ -313,6 +345,7 @@ type dataCopy struct{}
313345func (c * dataCopy ) RequiredGas (input []byte ) uint64 {
314346 return uint64 (len (input )+ 31 )/ 32 * params .IdentityPerWordGas + params .IdentityBaseGas
315347}
348+
316349func (c * dataCopy ) Run (in []byte ) ([]byte , error ) {
317350 return common .CopyBytes (in ), nil
318351}
0 commit comments