Skip to content

Commit

Permalink
Optimize KnownCodesStorage interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Sep 27, 2024
1 parent 0ca4224 commit d9a1ca3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 41 deletions.
36 changes: 4 additions & 32 deletions system-contracts/contracts/ContractDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ contract ContractDeployer is IContractDeployer, SystemContractBase {
/// @dev For EOA and simple contracts (i.e. not accounts) this value is 0.
mapping(address => AccountInfo) internal accountInfo;

enum EvmContractState {
None,
ConstructorPending,
ConstructorCalled,
Deployed
}

uint256 private constant EVM_HASHES_PREFIX = 1 << 254;
uint256 private constant CONSTRUCTOR_RETURN_GAS_SLOT = 1;

Expand Down Expand Up @@ -379,24 +372,6 @@ contract ContractDeployer is IContractDeployer, SystemContractBase {
});
}

function convertToConstructorEVMInput(bytes calldata _input) internal pure returns (bytes memory) {
// With how the contracts work, the calldata to the constructor must be an ABI-encoded `bytes`.
// This means that it should also contain offset as well as length
uint256 _fullLength = _input.length;
bytes memory extendedInput = new bytes(_input.length + 64);

assembly {
// "Offset" for the calldata
mstore(add(extendedInput, 0x20), 0x20)
// "Length"
mstore(add(extendedInput, 0x40), _fullLength)

calldatacopy(add(extendedInput, 0x60), _input.offset, _fullLength)
}

return extendedInput;
}

/// @notice Deploy a certain bytecode on the address.
/// @param _newAddress The address of the contract to be deployed.
/// @param _aaVersion The version of the account abstraction protocol to use.
Expand Down Expand Up @@ -529,16 +504,13 @@ contract ContractDeployer is IContractDeployer, SystemContractBase {
mstore(paddedBytecode, sub(dataLen, 0x20))
}

// ToDO: use efficient call
bytes32 versionedCodeHash = Utils.hashEVMBytecode(paddedBytecode);

KNOWN_CODE_STORAGE_CONTRACT.publishEVMBytecode(paddedBytecode);
bytes32 versionedCodeHash = KNOWN_CODE_STORAGE_CONTRACT.publishEVMBytecode(paddedBytecode);
ACCOUNT_CODE_STORAGE_SYSTEM_CONTRACT.storeAccountConstructedCodeHash(_newAddress, versionedCodeHash);

bytes32 evmBytecodeHash;
bytes32 evmBytecodeHash;
assembly {
let bytecodeLen := mload(add(paddedBytecode, 0x20))
evmBytecodeHash := keccak256(add(paddedBytecode, 0x40), bytecodeLen)
let bytecodeLen := mload(add(paddedBytecode, 0x20))
evmBytecodeHash := keccak256(add(paddedBytecode, 0x40), bytecodeLen)
}

_setEvmCodeHash(_newAddress, evmBytecodeHash);
Expand Down
22 changes: 14 additions & 8 deletions system-contracts/contracts/KnownCodesStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,31 @@ contract KnownCodesStorage is IKnownCodesStorage, SystemContractBase {
}
}

function publishEVMBytecode(bytes calldata bytecode) external onlyCallFrom(address(DEPLOYER_SYSTEM_CONTRACT)) {
function publishEVMBytecode(
bytes calldata paddedBytecode
) external payable onlyCallFrom(address(DEPLOYER_SYSTEM_CONTRACT)) returns (bytes32) {
/*
TODO: ensure that it is properly padded, etc.
To preserve EVM compatibility, we can not emit any events here.
*/

bytes32 hash = Utils.hashEVMBytecode(bytecode);
// ToDO: use efficient call
bytes32 vesionedBytecodeHash = Utils.hashEVMBytecode(paddedBytecode);

bool isCodeUnknown = getMarker(hash) == 0;

if (isCodeUnknown) {
if (getMarker(vesionedBytecodeHash) == 0) {
// ToDO: use efficient call
L1_MESSENGER_CONTRACT.sendToL1(bytecode);
L1_MESSENGER_CONTRACT.sendToL1(paddedBytecode);

assembly {
sstore(hash, 1)
sstore(vesionedBytecodeHash, 1)
}

emit MarkedAsKnown(vesionedBytecodeHash, true);
}

emit MarkedAsKnown(hash, isCodeUnknown);
assembly {
mstore(0x0, vesionedBytecodeHash)
return(0x0, 0x20)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ interface IKnownCodesStorage {

function getMarker(bytes32 _hash) external view returns (uint256);

function publishEVMBytecode(bytes calldata bytecode) external;
function publishEVMBytecode(bytes calldata bytecode) external payable returns (bytes32);
}

0 comments on commit d9a1ca3

Please sign in to comment.