ChainBridge has a piece of solidity code:
A:
assembly {
amount := calldataload(0xC4)
recipientAddress := mload(0x40)
lenRecipientAddress := calldataload(0xE4) // offset 0x20 beside amount is the length variable, I know that.
mstore(0x40, add(0x20, add(recipientAddress, lenRecipientAddress))) // load address to the free memory
calldatacopy(
recipientAddress, // copy to destinationRecipientAddress
0xE4, // copy from calldata @ 0x104 **Why copy from 0xE4?
sub(calldatasize(), 0xE) // copy size (calldatasize - 0x104)
//** Why the length is calldatasize()-0xE? Is the original comment by the author correct?
)
}
and another piece of code
B:
assembly {
tokenID := calldataload(0x64)
destinationRecipientAddress := mload(0x40) // load free memory pointer
let lenDestinationRecipientAddress := calldataload(0x84)
let lenMeta := calldataload(add(0xA4, lenDestinationRecipientAddress))
mstore(0x40, add(0x40, add(destinationRecipientAddress, lenDestinationRecipientAddress))) // shift free memory pointer
calldatacopy(
destinationRecipientAddress, // copy to destinationRecipientAddress
0x84, // copy from calldata after destinationRecipientAddress length declaration @0x84
sub(calldatasize(), add(0x84, add(0x20, lenMeta))) // copy size (calldatasize - (0xC4 + the space metaData takes up))
)
// .....
}
The code mstore(0x40, add(0x40, add(destinationRecipientAddress, lenDestinationRecipientAddress)))
in B, why is add(0x40, ...)
, but in the code of A is add(0x20, add(recipientAddress, lenRecipientAddress)
. 0x40 or 0x20 ?
Why is 0x40
? If I change to mstore(0x40, add(0x20, add(destinationRecipientAddress, lenDestinationRecipientAddress)))
, it's also success with the test scripts.
And in GenericHandler.sol, Line 200, it's mstore(0x40, add(0x60, add(metaData, lenMeta)))
. Why is 0x60
?