Skip to content

Instantly share code, notes, and snippets.

@chriseth
chriseth / Purchase.sol
Last active May 2, 2021 13:27
Purchase
// This contract can be used to hold money while an item is in transit
// during purchase.
// This protocol is a variation of a protocol by Oleg Andreev which is
// described at
// https://gatecoin.com/blog/2015/10/blockchain2-disrupting-disrutors/
//
// Assume Bob wants to buy an item worth x Ether from Alice.
// Alice creates this contract and and sends 2x Ether to the contract
// together with its creation transaction.
// If Bob does not react, Alice can get her money back.
#!/bin/bash
# TODO add opcodes that are not allowed
# and translate them to "invalid"
# and then check that it is not present
# in the optimized code.
# "ovmCREATE(bytes)": "14aa2ff7",
# "ovmCREATE2(bytes,bytes32)": "99ccd98b",
# "ovmCREATEEOA(bytes32,uint8,bytes32,bytes32)": "741a33eb",
contract PatriciaTree {
// List of all keys, not necessarily sorted.
bytes[] keys;
// Mapping of hash of key to value
mapping (bytes32 => bytes) values;
struct Label {
bytes32 data;
uint length;
}
@chriseth
chriseth / Oracle.sol
Last active July 29, 2020 11:37
Oracle
struct Query
{
uint id;
function(uint, bytes memory) external callback;
}
contract Oracle
{
event Queried(bytes,function(uint, bytes memory) external);
@chriseth
chriseth / debugdata.json
Created July 2, 2020 17:37
Function entry point information
[
{
"entryPoint": 399,
"id": 42,
"paramStackSlots": 2,
"returnStackSlots": 0
},
{
"entryPoint": 408,
"id": 35,
@chriseth
chriseth / analysis.cpp
Last active May 18, 2020 23:55
runtime jump validation
#include <iostream>
#include <vector>
#include <optional>
#include <assert.h>
using namespace std;
uint8_t constexpr PUSH = 0x60;
uint8_t constexpr JUMPDEST = 0x5b;
uint8_t constexpr BEGINSUB = 0xb5;
@chriseth
chriseth / gist:c95c20393e0dd1063d4366983b4bc4c9
Created May 29, 2019 21:23
Solidity to EWasm code generation
Solidity code:
contract c {
function f(uint x) internal returns (uint) {
while (x > 0)
return 2 * f(x - 1);
}
}
Yul IR:
@chriseth
chriseth / input.sol
Created April 17, 2019 14:27
Sol to Yul compiler
contract C {
function f() public pure returns (uint x) { x = 7; }
}
@chriseth
chriseth / Automated Source Code Repository.md
Last active April 2, 2019 20:55
Automated Source Code Repository

To increase decentralization, it would be great to have a verified repository of smart contract source codes. Due to the metadata being part of the bytecode, we should have everything that is needed to actually perform the verification. This repository could be an actual repository on github, where people can create pull requests to have their smart contract code compiled and verified. There could be a CI job that does the verification as follows:

  • fetch the bytecode from the chain via infura or another service
  • install the correct compiler version using npm
  • only flag the PR green, if the compilation output matches the bytecode

If the directory name is the address of the contract, the source code should be easy to find, even if multiple

uint salt = 0x123;
bool success;
// order matters!
bytes memory p = abi.encode(param1, ..., paramn);
bytes memory c = type(C).creationCode;
assembly {
function memCopyPadded(from, to, length) {
for { let i := 0 } lt(i, length) { i := add(i, 0x20) } {
mstore(to, mload(from))
to := add(to, 0x20)