@@ -33,21 +33,21 @@ import (
3333var _ PrecompileEnvironment = (* environment )(nil )
3434
3535type environment struct {
36- evm * EVM
37- self * Contract
38- callType CallType
36+ evm * EVM
37+ self * Contract
38+ callType CallType
39+ view , pure bool
3940}
4041
4142func (e * environment ) Gas () uint64 { return e .self .Gas }
4243func (e * environment ) UseGas (gas uint64 ) bool { return e .self .UseGas (gas ) }
4344func (e * environment ) Value () * uint256.Int { return new (uint256.Int ).Set (e .self .Value ()) }
4445
45- func (e * environment ) ChainConfig () * params.ChainConfig { return e .evm .chainConfig }
46- func (e * environment ) Rules () params.Rules { return e .evm .chainRules }
47- func (e * environment ) ReadOnlyState () libevm.StateReader { return e .evm .StateDB }
48- func (e * environment ) IncomingCallType () CallType { return e .callType }
49- func (e * environment ) BlockNumber () * big.Int { return new (big.Int ).Set (e .evm .Context .BlockNumber ) }
50- func (e * environment ) BlockTime () uint64 { return e .evm .Context .Time }
46+ func (e * environment ) ChainConfig () * params.ChainConfig { return e .evm .chainConfig }
47+ func (e * environment ) Rules () params.Rules { return e .evm .chainRules }
48+ func (e * environment ) IncomingCallType () CallType { return e .callType }
49+ func (e * environment ) BlockNumber () * big.Int { return new (big.Int ).Set (e .evm .Context .BlockNumber ) }
50+ func (e * environment ) BlockTime () uint64 { return e .evm .Context .Time }
5151
5252func (e * environment ) refundGas (add uint64 ) error {
5353 gas , overflow := math .SafeAdd (e .self .Gas , add )
@@ -58,6 +58,9 @@ func (e *environment) refundGas(add uint64) error {
5858 return nil
5959}
6060
61+ func (e * environment ) SetReadOnly () { e .view = true }
62+ func (e * environment ) SetPure () { e .pure = true }
63+
6164func (e * environment ) ReadOnly () bool {
6265 // A switch statement provides clearer code coverage for difficult-to-test
6366 // cases.
@@ -69,17 +72,18 @@ func (e *environment) ReadOnly() bool {
6972 return true
7073 case e .evm .interpreter .readOnly :
7174 return true
75+ case e .view || e .pure :
76+ return true
7277 default :
7378 return false
7479 }
7580}
7681
77- func (e * environment ) Addresses () * libevm.AddressContext {
78- return & libevm.AddressContext {
79- Origin : e .evm .Origin ,
80- Caller : e .self .CallerAddress ,
81- Self : e .self .Address (),
82+ func (e * environment ) ReadOnlyState () libevm.StateReader {
83+ if e .pure {
84+ return nil
8285 }
86+ return e .evm .StateDB
8387}
8488
8589func (e * environment ) StateDB () StateDB {
@@ -89,6 +93,14 @@ func (e *environment) StateDB() StateDB {
8993 return e .evm .StateDB
9094}
9195
96+ func (e * environment ) Addresses () * libevm.AddressContext {
97+ return & libevm.AddressContext {
98+ Origin : e .evm .Origin ,
99+ Caller : e .self .CallerAddress ,
100+ Self : e .self .Address (),
101+ }
102+ }
103+
92104func (e * environment ) BlockHeader () (types.Header , error ) {
93105 hdr := e .evm .Context .Header
94106 if hdr == nil {
0 commit comments