59 lines
2.1 KiB
Solidity
59 lines
2.1 KiB
Solidity
|
// SPDX-License-Identifier: MIT
|
||
|
|
||
|
pragma solidity ^0.6.0;
|
||
|
|
||
|
contract Pack {
|
||
|
|
||
|
uint256 public constant CHUNK_TREE_HEIGHT = 7;
|
||
|
uint256 public constant CHUNK_SIZE = 2**CHUNK_TREE_HEIGHT;
|
||
|
uint256 public constant ITEM_SIZE = 32 + 20 + 4;
|
||
|
uint256 public constant BYTES_SIZE = CHUNK_SIZE * ITEM_SIZE;
|
||
|
|
||
|
uint256 public gas1;
|
||
|
uint256 public gas2;
|
||
|
uint256 public gas3;
|
||
|
uint256 public gas4;
|
||
|
bytes32 public hash;
|
||
|
|
||
|
event DepositData(address instance, bytes32 indexed hash, uint256 block, uint256 index);
|
||
|
|
||
|
function pack2(bytes32[CHUNK_SIZE] memory hashes, address[CHUNK_SIZE] memory instances, uint32[CHUNK_SIZE] memory blocks) public {
|
||
|
uint256 gasBefore = gasleft();
|
||
|
bytes memory data = new bytes(BYTES_SIZE);
|
||
|
for(uint i = 0; i < CHUNK_SIZE; i++) {
|
||
|
(bytes32 hash, address instance, uint32 block) = (hashes[i], instances[i], blocks[i]);
|
||
|
assembly {
|
||
|
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x38), block)
|
||
|
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x34), instance)
|
||
|
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x20), hash)
|
||
|
}
|
||
|
}
|
||
|
uint256 gasHash = gasleft();
|
||
|
bytes32 hash1 = sha256(data);
|
||
|
uint256 gasEvents = gasleft();
|
||
|
for(uint i = 0; i < CHUNK_SIZE; i++) {
|
||
|
emit DepositData(instances[i], hashes[i], blocks[i], i);
|
||
|
}
|
||
|
gas1 = gasEvents - gasleft();
|
||
|
gas2 = gasHash - gasEvents;
|
||
|
gas3 = gasBefore - gasHash;
|
||
|
gas4 = gasBefore;
|
||
|
hash = hash1;
|
||
|
}
|
||
|
|
||
|
function pack3(bytes32[CHUNK_SIZE] memory hashes, address[CHUNK_SIZE] memory instances, uint32[CHUNK_SIZE] memory blocks) public view returns(uint256 gas1, uint256 gas2, bytes32 hash) {
|
||
|
uint256 gasBefore = gasleft();
|
||
|
bytes memory data = new bytes(BYTES_SIZE);
|
||
|
for(uint i = 0; i < CHUNK_SIZE; i++) {
|
||
|
(bytes32 hash, address instance, uint32 block) = (hashes[i], instances[i], blocks[i]);
|
||
|
assembly {
|
||
|
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x38), block)
|
||
|
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x34), instance)
|
||
|
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x20), hash)
|
||
|
}
|
||
|
}
|
||
|
uint256 gasHash = gasleft();
|
||
|
bytes32 hash = sha256(data);
|
||
|
return (gasleft() - gasHash, gasHash - gasBefore, hash);
|
||
|
}
|
||
|
}
|