Wikipedia

Search results

08 February 2023

Ethereum: hex-encoding function ABI calls

Delegatecall is a feature in Solidity, the programming language used to write smart contracts on the Ethereum blockchain, that allows a contract to call another contract's code and use its storage. Essentially, delegatecall enables one contract to reuse code from another contract, and the storage of the calling contract will be used as if it was the storage of the called contract.

Delegatecall was hacked before in an exploit known as the "re-entrancy attack." In this type of attack, a malicious contract could call another contract's code multiple times in a single transaction, effectively re-entering the code and taking advantage of the storage of the called contract. This could lead to the malicious contract being able to steal funds from the called contract's storage before the called contract had a chance to update its storage to reflect the intended outcome of the transaction.

To mitigate these types of attacks, it is important to properly handle re-entrancy in smart contract code and to follow best practices for secure contract development. 

In Ethereum, a block of memory (also known as "storage") is allocated as an array of bytes. The size of each element in the array is 1 byte. So, 64 bytes of memory would be 64 elements in the array.

The speed at which 64 bytes of memory can be used depends on the operations being performed on it and the state of the Ethereum network. If the operations are simple, such as read or write operations, they can be performed relatively quickly. However, if the operations are complex, such as computations, they may take longer to complete, particularly if the network is congested.

It's also important to keep in mind that every operation that accesses or modifies memory in a smart contract incurs a gas cost. This cost is based on the amount of memory used, so operations that use a large amount of memory can be expensive in terms of gas.




If the fallback function in contract B has a delegatecall to contract A, then executing a sendTransaction to contract B with a specified data value equal to the hex representation of the function signature in contract A, it would execute the functio in contract A. 

For example, the function is named "foo":



The data needs to be hex-encoded and would be accomplished with: web3.utils.soliditySha3('foo()').substring(0,10)

The first 4 bytes of this value would represent the function signature, and the remaining bytes could be padded with zeros. 

The sendTransaction would be used or a contract method of the delegator, in the former case it would look like: 

sendTransaction({
    from: address1,
    to: address2,
    data: web3.utils.soliditySha3('foo()').substring(0,10)
})

No comments:

Post a Comment