Skip to content

Instantly share code, notes, and snippets.

// in application module
module Payments {
import 0x30.Adjudicator;
public validTransition(s1, s2) {
  // implement app-specific transition logic
  }
public respond(R#Adjudicator.T adjudicator, V#State s1, V#State s2) {
assert(validTransition(s1, s2));
  Adjudicator.respond(move(adjudicator), s1, s2);
module Adjudicator {
// the adjudicator explicitly declares the apps it supports
import 0x32.Payments;
import 0x34.Poker;
// etc.
public respond(adj: &mut R#Self.T, s1: &V#State, s2: &V#State) {
  // framework checks 
  // … (not shown) …
// in the adjudicator contract:
function respond(State memory s1, State memory s2) public {
// check that the first state matches the stored challenge
assertHashMatches(s1, stored_challenge_hash);
// framework checks
  assertValidSignature(s2);
  assertSameAppDefinition(s1, s2);
  assertTurnNumIncrements(s1, s2);
@tomclose
tomclose / nitro-state-format.csv
Last active July 4, 2019 00:30
Nitro State Format
Field Data type Definition / Explanation
ChainID unit256 e.g. ropsten / mainnet
Participants address[] participant addresses in signing order
ChannelNonce uint256 chosen by the participants to uniquely identify the channel
TurnNum uint256 turn number
DefaultOutcome [address, uint256][] payouts if the channel is finalized in this state
isFinal boolean used to allow instant withdrawals when channel is closed collaboratively
AppDefinition address on-chain address of library defining custom application rules
AppData bytes application-specific data
Signature [uint8, bytes32, bytes32] ECDSA signature of commitment (v, r, s)
@tomclose
tomclose / state-action-style-game.sol
Last active May 22, 2019 15:10
state-action style for force-move
pragma solidity ^0.5.2;
pragma experimental ABIEncoderV2;
import "fmg-core/contracts/Commitment.sol";
library StateActionStyle {
using Commitment for Commitment.CommitmentStruct;
struct StateActionAttributes {
address stateActionLibrary;
*.sol linguist-language=Solidity
@tomclose
tomclose / benchmark.rb
Created April 8, 2016 10:14
Benchmarking functions vs procs
require 'benchmark'
def f_func(x); 2*x; end
def f_proc_func; proc {|x| 2*x}; end
f_proc_var = proc {|x| 2*x }
n = 1000000
Benchmark.bm(7) do |x|
x.report("using function: ") { n.times { |i| f_func(i) } }

Keybase proof

I hereby claim:

  • I am tomclose on github.
  • I am tomclose (https://keybase.io/tomclose) on keybase.
  • I have a public key ASCUBtXcn50zeVGwvfdpKEdIvjbco6XVEGYNMcRitbrZ_go

To claim this, I am signing this object:

Simple timing

You can investigate how long something is taking using the Benchmark library.

require 'benchmark'

Benchmark.measure { some_code_I_want_to_profile }
@tomclose
tomclose / example.rb
Last active January 2, 2016 23:29
A module that can be used to transform the keys and values for a subset of pairs in a hash.
class FruitImporter
include HashRemapper
remaps 'fruit_name', to: :name
remaps 'id', to: :fruit_store_id
remaps 'coordinates', to: :coordinates_x, value: ->(c) { c[0].to_f if c}
remaps 'coordinates', to: :coordinates_y, value: ->(c) { c[1].to_f if c}
def import(fruit_hash)
Fruit.create(remap(fruit_hash))