Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Aug 3, 2024
1 parent 5340fa7 commit 2844988
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
38 changes: 28 additions & 10 deletions src/AccessLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,28 @@ library AccessLogs {
Memory.PhysicalAddress writeAddress,
uint64 newValue
) internal pure {
bytes32 writtenData = a.buffer.consumeBytes32();

(Memory.PhysicalAddress leafAddress, uint64 offset) =
writeAddress.truncateToLeaf();
uint64 expectedNewValue = machineWordToSolidityUint64(
getBytes8FromBytes32AtOffset(writtenData, offset)

Memory.Region memory region = Memory.regionFromStride(
Memory.strideFromLeafAddress(leafAddress),
Memory.alignedSizeFromLog2(0)
);

bytes32 oldLeaf = a.buffer.consumeBytes32();
(bytes32 rootHash,) =
a.buffer.peekRoot(region, keccak256(abi.encodePacked(oldLeaf)));

require(
newValue == expectedNewValue,
"Access log value does not contain the expected written value"
a.currentRootHash == rootHash, "Write region root doesn't match"
);

writeLeaf(
a,
Memory.strideFromLeafAddress(leafAddress),
keccak256(abi.encodePacked(writtenData))
bytes32 newLeaf = setBytes8ToBytes32AtOffset(
solidityUint64ToMachineWord(newValue), oldLeaf, offset
);

bytes32 newRootHash = a.buffer.getRoot(region, newLeaf);
a.currentRootHash = newRootHash;
}

function getBytes8FromBytes32AtOffset(bytes32 source, uint64 offset)
Expand All @@ -170,4 +174,18 @@ library AccessLogs {
{
return bytes8(source << (offset << Memory.LOG2_WORD));
}

function setBytes8ToBytes32AtOffset(
bytes8 word,
bytes32 leaf,
uint64 offset
) internal pure returns (bytes32) {
uint64 wordOffset = offset << Memory.LOG2_WORD;
bytes32 toWrite = bytes32(word) << wordOffset;

bytes32 wordMask = bytes32((1 << Memory.LOG2_WORD) - 1);
bytes32 mask = ~(wordMask << wordOffset);

return (leaf & mask) | toWrite;
}
}
42 changes: 32 additions & 10 deletions templates/AccessLogs.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,27 @@ library AccessLogs {
Memory.PhysicalAddress writeAddress,
uint64 newValue
) internal pure {
bytes32 writtenData = a.buffer.consumeBytes32();
(Memory.PhysicalAddress leafAddress, uint64 offset) =
writeAddress.truncateToLeaf();

(Memory.PhysicalAddress leafAddress, uint64 offset) = writeAddress.truncateToLeaf();
uint64 expectedNewValue =
machineWordToSolidityUint64(
getBytes8FromBytes32AtOffset(writtenData, offset));
Memory.Region memory region = Memory.regionFromStride(
Memory.strideFromLeafAddress(leafAddress),
Memory.alignedSizeFromLog2(0)
);

require(newValue == expectedNewValue, "Access log value does not contain the expected written value");
bytes32 oldLeaf = a.buffer.consumeBytes32();
(bytes32 rootHash,) = a.buffer.peekRoot(region, keccak256(abi.encodePacked(oldLeaf)));

writeLeaf(
a,
Memory.strideFromLeafAddress(leafAddress),
keccak256(abi.encodePacked(writtenData))
require(
a.currentRootHash == rootHash, "Write region root doesn't match"
);

bytes32 newLeaf = setBytes8ToBytes32AtOffset(
solidityUint64ToMachineWord(newValue), oldLeaf, offset
);

bytes32 newRootHash = a.buffer.getRoot(region, newLeaf);
a.currentRootHash = newRootHash;
}

function getBytes8FromBytes32AtOffset(bytes32 source, uint64 offset)
Expand All @@ -179,6 +186,21 @@ library AccessLogs {
return bytes8(source << (offset << Memory.LOG2_WORD));
}

function setBytes8ToBytes32AtOffset(bytes8 word, bytes32 leaf, uint64 offset)
internal
pure
returns (bytes32)
{
uint64 wordOffset = offset << Memory.LOG2_WORD;
bytes32 toWrite = bytes32(word) << wordOffset;

bytes32 wordMask = bytes32((1 << Memory.LOG2_WORD) - 1);
bytes32 mask = ~(wordMask << wordOffset);

return (leaf & mask) | toWrite;
}


//:#else

/// @dev This library mocks the `templates/AccessLogs.sol` yet with a very different implementation.
Expand Down

0 comments on commit 2844988

Please sign in to comment.