Skip to content

Commit ecf5970

Browse files
authored
Merge pull request #2234 from CortexFoundation/dev
precompiled
2 parents cf576f3 + 062b4e1 commit ecf5970

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

core/vm/contracts.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type PrecompiledContract interface {
4747
Run(input []byte) ([]byte, error) // Run runs the precompiled contract
4848
}
4949

50+
// PrecompiledContracts contains the precompiled contracts supported at the given fork.
51+
type PrecompiledContracts map[common.Address]PrecompiledContract
52+
5053
// PrecompiledContractsHomestead contains the default set of pre-compiled CortexTheseus
5154
// contracts used in the Frontier and Homestead releases.
5255
var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
@@ -170,6 +173,17 @@ func init() {
170173
}
171174
}
172175

176+
func activePrecompiledContracts(rules params.Rules) PrecompiledContracts {
177+
switch {
178+
case rules.IsIstanbul:
179+
return PrecompiledContractsIstanbul
180+
case rules.IsByzantium:
181+
return PrecompiledContractsByzantium
182+
default:
183+
return PrecompiledContractsHomestead
184+
}
185+
}
186+
173187
// ActivePrecompiles returns the precompiles enabled with the current configuration.
174188
func ActivePrecompiles(rules params.Rules) []common.Address {
175189
switch {

core/vm/cvm.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ type CVM struct {
123123
// applied in opCall*.
124124
callGasTemp uint64
125125
//Fs *torrentfs.FileStorage
126+
127+
// precompiles holds the precompiled contracts for the current epoch
128+
precompiles map[common.Address]PrecompiledContract
126129
}
127130

128131
// NewCVM returns a new CVM. The returned CVM is not thread safe and should
@@ -138,11 +141,19 @@ func NewCVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon
138141
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
139142
}
140143

144+
cvm.precompiles = activePrecompiledContracts(cvm.chainRules)
141145
cvm.interpreter = NewCVMInterpreter(cvm)
142146

143147
return cvm
144148
}
145149

150+
// SetPrecompiles sets the precompiled contracts for the EVM.
151+
// This method is only used through RPC calls.
152+
// It is not thread-safe.
153+
func (cvm *CVM) SetPrecompiles(precompiles PrecompiledContracts) {
154+
cvm.precompiles = precompiles
155+
}
156+
146157
// Reset resets the CVM with a new transaction context.Reset
147158
// This is not threadsafe and should only be done very cautiously.
148159
func (cvm *CVM) Reset(txCtx TxContext, statedb StateDB) {

0 commit comments

Comments
 (0)