Skip to content

Commit

Permalink
Simplify _fetchDeployedCodeLen
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Aug 28, 2024
1 parent 5cdc5dd commit 89245ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
23 changes: 8 additions & 15 deletions system-contracts/contracts/EvmInterpreterFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -264,24 +264,17 @@ function _fetchDeployedCodeWithDest(addr, _offset, _len, dest) -> codeLen {
function _fetchDeployedCodeLen(addr) -> codeLen {
let codeHash := _getRawCodeHash(addr)

mstore(0, codeHash)

let success := staticcall(gas(), CODE_ORACLE_SYSTEM_CONTRACT(), 0, 32, 0, 0)

switch iszero(success)
switch shr(248, codeHash)
case 1 {
// The code oracle call can only fail in the case where the contract
// we are querying is the current one executing and it has not yet been
// deployed, i.e., if someone calls codesize (or extcodesize(address()))
// inside the constructor. In that case, code length is zero.
codeLen := 0
// EraVM
let codeLengthInWords := and(shr(224, codeHash), 0xffff)
codeLen := shl(5, codeLengthInWords) // codeLengthInWords * 32
}
default {
// The first word is the true length of the bytecode
returndatacopy(0, 0, 32)
codeLen := mload(0)
case 2 {
// EVM
let codeLengthInBytes := and(shr(224, codeHash), 0xffff)
codeLen := codeLengthInBytes
}

}

function getDeployedBytecode() {
Expand Down
46 changes: 16 additions & 30 deletions system-contracts/contracts/EvmInterpreterPreprocessed.yul
Original file line number Diff line number Diff line change
Expand Up @@ -333,24 +333,17 @@ object "EVMInterpreter" {
function _fetchDeployedCodeLen(addr) -> codeLen {
let codeHash := _getRawCodeHash(addr)

mstore(0, codeHash)

let success := staticcall(gas(), CODE_ORACLE_SYSTEM_CONTRACT(), 0, 32, 0, 0)

switch iszero(success)
switch shr(248, codeHash)
case 1 {
// The code oracle call can only fail in the case where the contract
// we are querying is the current one executing and it has not yet been
// deployed, i.e., if someone calls codesize (or extcodesize(address()))
// inside the constructor. In that case, code length is zero.
codeLen := 0
// EraVM
let codeLengthInWords := and(shr(224, codeHash), 0xffff)
codeLen := shl(5, codeLengthInWords) // codeLengthInWords * 32
}
default {
// The first word is the true length of the bytecode
returndatacopy(0, 0, 32)
codeLen := mload(0)
case 2 {
// EVM
let codeLengthInBytes := and(shr(224, codeHash), 0xffff)
codeLen := codeLengthInBytes
}

}

function getDeployedBytecode() {
Expand Down Expand Up @@ -3299,24 +3292,17 @@ object "EVMInterpreter" {
function _fetchDeployedCodeLen(addr) -> codeLen {
let codeHash := _getRawCodeHash(addr)

mstore(0, codeHash)

let success := staticcall(gas(), CODE_ORACLE_SYSTEM_CONTRACT(), 0, 32, 0, 0)

switch iszero(success)
switch shr(248, codeHash)
case 1 {
// The code oracle call can only fail in the case where the contract
// we are querying is the current one executing and it has not yet been
// deployed, i.e., if someone calls codesize (or extcodesize(address()))
// inside the constructor. In that case, code length is zero.
codeLen := 0
// EraVM
let codeLengthInWords := and(shr(224, codeHash), 0xffff)
codeLen := shl(5, codeLengthInWords) // codeLengthInWords * 32
}
default {
// The first word is the true length of the bytecode
returndatacopy(0, 0, 32)
codeLen := mload(0)
case 2 {
// EVM
let codeLengthInBytes := and(shr(224, codeHash), 0xffff)
codeLen := codeLengthInBytes
}

}

function getDeployedBytecode() {
Expand Down

0 comments on commit 89245ad

Please sign in to comment.