Skip to content

Instantly share code, notes, and snippets.

@cag
Last active December 13, 2020 09:35
Show Gist options
  • Save cag/ca2b2046c75bd1b001e45c16e3890226 to your computer and use it in GitHub Desktop.
Save cag/ca2b2046c75bd1b001e45c16e3890226 to your computer and use it in GitHub Desktop.
Gas tests for oracle metadata
pragma solidity ^0.4.24;
contract SingleWordConsumer {
event LogWord(bytes32 w);
function receiveResult(bytes) external {
bytes32 w;
assembly {
w := calldataload(0x44) // 4 byte func selector +
// word of 1st arg offset +
// word of bytes length
// to get to content
}
emit LogWord(w);
}
}
contract SingleWordMetaconsumer {
event LogWord(bytes32 w);
function receiveResult(bytes, bytes) external {
bytes32 w;
assembly {
w := calldataload(0x64) // 4 byte func selector +
// word of 1st arg offset +
// word of 2nd arg offset +
// word of bytes length
// to get to content
}
emit LogWord(w);
}
}
contract SingleWordSWMetaConsumer {
event LogWords(bytes32 w, bytes32 m);
function receiveResult(bytes) external {
bytes32 w;
bytes32 m;
assembly {
w := calldataload(0x44) // 4 byte func selector +
// word of 1st arg offset +
// word of bytes length (should be 0x40)
// to get to contents of bytes
m := calldataload(0x64)
}
emit LogWords(w, m);
}
}
contract SingleWordSWMetaMetaconsumer {
event LogWords(bytes32 w, bytes32 m);
function receiveResult(bytes, bytes) external {
bytes32 w;
bytes32 m;
assembly {
w := calldataload(0x64) // 4 byte func selector +
// word of 1st arg offset +
// word of 2nt arg offset +
// word of 1st arg bytes length (should be 0x20)
// to get to contents of 1st arg
m := calldataload(0xa4) // then skip word of 2nd arg bytes length
// to get to contents of 2nd arg
}
emit LogWords(w, m);
}
}
contract SingleWordDynMetaConsumer {
event LogStuff(bytes32 w, bytes m);
function receiveResult(bytes) external {
bytes memory resCopy = res;
bytes32 w;
bytes memory m;
assembly {
w := calldataload(0x44) // 4 byte func selector +
// word of 1st arg offset +
// word of bytes length
// to get to contents of bytes
// then assume first word is data word
m := add(resCopy, 0x40)
}
emit LogStuff(w, m);
}
}
contract SingleWordDynMetaMetaconsumer {
event LogStuff(bytes32 w, bytes m);
function receiveResult(bytes, bytes m) external {
bytes32 w;
assembly {
w := calldataload(0x64) // 4 byte func selector +
// word of 1st arg offset +
// word of 2nd arg offset +
// word of 1st arg bytes length (should be 0x20)
// to get to contents of 1st arg
}
emit LogStuff(w, m);
}
}
contract DynamicConsumer {
event LogBytes(bytes b);
function receiveResult(bytes b) external {
emit LogBytes(b);
}
}
contract DynamicMetaconsumer {
event LogBytes(bytes b);
function receiveResult(bytes b, bytes) external {
emit LogBytes(b);
}
}
contract DynamicSWMetaConsumer {
event LogStuff(bytes b, bytes32 m);
function receiveResult(bytes res) external {
bytes memory resCopy = res;
bytes memory b;
bytes32 m;
assembly {
b := add(resCopy, 0x20)
m := mload(add(add(b, 0x20), mload(b)))
}
emit LogStuff(b, m);
}
}
contract DynamicSWMetaMetaconsumer {
event LogStuff(bytes b, bytes32 m);
function receiveResult(bytes b, bytes) external {
bytes32 m;
assembly {
m := calldataload(add(calldataload(0x24), 0x24))
}
emit LogStuff(b, m);
}
}
contract DynamicDynMetaConsumer {
event LogStuff(bytes b, bytes m);
function receiveResult(bytes res) external {
bytes memory resCopy = res;
bytes memory b;
bytes memory m;
assembly {
b := add(resCopy, 0x20)
m := add(add(b, 0x20), mload(b))
}
emit LogStuff(b, m);
}
}
contract DynamicDynMetaMetaconsumer {
event LogStuff(bytes b, bytes m);
function receiveResult(bytes b, bytes m) external {
emit LogStuff(b, m);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment