@@ -50,7 +50,8 @@ type Contract struct {
5050 caller ContractRef
5151 self ContractRef
5252
53- header EOF1Header
53+ CodeBeginOffset uint64 // starting offset of the code section if EOF or 0 if legacy
54+ CodeSize uint64 // size of the code section if EOF or bytecode size if legacy
5455
5556 jumpdests map [common.Hash ]bitvec // Aggregated result of JUMPDEST analysis.
5657 analysis bitvec // Locally cached result of JUMPDEST analysis
@@ -90,7 +91,7 @@ func (c *Contract) validJumpdest(dest *uint256.Int) bool {
9091 udest , overflow := dest .Uint64WithOverflow ()
9192 // PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
9293 // Don't bother checking for JUMPDEST in that case.
93- if overflow || udest >= c .CodeSize () {
94+ if overflow || udest >= c .CodeSize {
9495 return false
9596 }
9697 // Only JUMPDESTs allowed for destinations
@@ -148,8 +149,8 @@ func (c *Contract) AsDelegate() *Contract {
148149// GetOp returns the n'th element in the contract's byte array
149150// n is offset inside code section in case of EOF contract
150151func (c * Contract ) GetOp (n uint64 ) OpCode {
151- if n < c .CodeSize () {
152- return OpCode (c .Code [c .CodeBeginOffset () + n ])
152+ if n < c .CodeSize {
153+ return OpCode (c .Code [c .CodeBeginOffset + n ])
153154 }
154155
155156 return STOP
@@ -182,34 +183,19 @@ func (c *Contract) Value() *big.Int {
182183 return c .value
183184}
184185
185- // IsLegacy returns true if contract is not EOF
186- func (c * Contract ) IsLegacy () bool {
186+ // getCodeBounds returns beginning offset and size of the code section if EOF
187+ // or 0 and bytecode length if legacy
188+ func getCodeBounds (container []byte , header * EOF1Header ) (begin uint64 , size uint64 ) {
187189 // EOF1 doesn't allow contracts without code section
188- return c .header .codeSize == 0
189- }
190-
191- // CodeBeginOffset returns starting offset of the code section
192- func (c * Contract ) CodeBeginOffset () uint64 {
193- if c .IsLegacy () {
194- return 0
195- }
196- return c .header .CodeBeginOffset ()
197- }
198-
199- // CodeEndOffset returns offset of the code section end
200- func (c * Contract ) CodeEndOffset () uint64 {
201- if c .IsLegacy () {
202- return uint64 (len (c .Code ))
203- }
204- return c .header .CodeEndOffset ()
205- }
206-
207- // CodeSize returns the size of the code (if legacy) or code section (if EOF)
208- func (c * Contract ) CodeSize () uint64 {
209- if c .IsLegacy () {
210- return uint64 (len (c .Code ))
190+ isLegacy := (header .codeSize == 0 )
191+ if isLegacy {
192+ begin = 0
193+ size = uint64 (len (container ))
194+ } else {
195+ begin = header .CodeBeginOffset ()
196+ size = uint64 (header .codeSize )
211197 }
212- return uint64 ( c . header . codeSize )
198+ return
213199}
214200
215201// SetCallCode sets the code of the contract and address of the backing data
@@ -219,8 +205,7 @@ func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []by
219205 c .CodeHash = hash
220206 c .CodeAddr = addr
221207
222- c .header .codeSize = header .codeSize
223- c .header .dataSize = header .dataSize
208+ c .CodeBeginOffset , c .CodeSize = getCodeBounds (code , header )
224209}
225210
226211// SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
@@ -230,6 +215,5 @@ func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAn
230215 c .CodeHash = codeAndHash .hash
231216 c .CodeAddr = addr
232217
233- c .header .codeSize = header .codeSize
234- c .header .dataSize = header .dataSize
218+ c .CodeBeginOffset , c .CodeSize = getCodeBounds (codeAndHash .code , header )
235219}
0 commit comments