Skip to content

Instantly share code, notes, and snippets.

@crackcomm
Last active June 26, 2017 22:10
Show Gist options
  • Save crackcomm/47634997a865e2e5edd5e91661beb139 to your computer and use it in GitHub Desktop.
Save crackcomm/47634997a865e2e5edd5e91661beb139 to your computer and use it in GitHub Desktop.

IPFN Contracts

Blockchain

IPFN Blockchain is a contract itself, running on infinite amount of nodes with a budget that is slightly limited.

Contract operations

Neurons in the contract can be called publicly or by the owner if contract is private.

Operations can be called on the entire network to create blockchain applications.

IPFN Blockchain examples

Example contract

{
  "id": "QmekgFZREtKWPoU8SBLReDbjEYj1vHM12h65weE1pd2LAB", // unique contract ID
  "owner": "QmZ5JxYN3BBaijWjs3quqN3XxQQuKZYu1YNwtp8QSjdLRh", // owner gets refunds (optional)
  "state": "QmRZ5TL18MXcZm7GysG8JBVkkpguHwDyRybS1hoWgHRo7i", // eq. ID of the last block in blockchain (optional)
  "neuron": "/ipfs/QmYJaJa4N4tosk4JWtZidM5Ech6DPzpAS3ajZyhSuN8ZSM", // neuron is the function to execute
  "previous": "QmNkbyVE72GKDmP6V5bm5b8BwvSmT3rsmebZ6KEHQCvPQR", // previous execution (optional)
  "created_date": 1498141924, // date of creation (unix timestamp)
  "enforce_date": 1498833155, // date of execution (11520 minutes later) (optional)
  "exec_time": 60, // execution time in seconds (optional)
  "exec_interval": 60, // execution every 60 seconds (optional)
  "charge_limit": 1, // 1e-6 coins per second of execution (optional)
  "parallel_run": 1, // one execution (default)
  "max_failures": 12, // max verification failures to abandon operation (default is 0)
  "verify_times": 1, // one verification only (default is 1 if `verify_neuron` is present)
  "verify_state": "/ipfs/QmR1WuiVbGZVH1wzPxbLwJq8z5Cg3bzuB8G8mKiDZQ5k62", // neuron that verifies new and previous state
  "verify_neuron": "/ipfs/QmakKWzpKArjNc5puKuwuCNqCnpF3HmK11xzREhiqz7gVV", // neuron that verifies execution
  "verify_node": ["/ipns/…", "/ipns/…"], // nodes to verify (optional)
  "execute_node": ["/ipns/…", "/ipns/…"], // nodes to execute (optional)
  "node_capital": 1e9, // minimum node locked capital to be able to participate in running and verifying
  "locked": true, // contract cannot be removed (optional)
  "locked_coins": 1192, // 1e-6 coins per 60m on queue + exec costs
  "signature": "…" // signed contract
}

Blockchain-hosting contract

IPFN Blockchain is contract itself and is close to following:

{
  "id": "QmUyERaqHBehKc6tjHTN1LFyNNWBTHvkBwoccBPNiNfX4m", // unique contract ID
  "locked": true, // contract cannot be removed
  "previous": "QmVgQm6nWWBZwnSUKa4KH14WNg33iCTxASM4QbaepmVatH",
  "state": "QmUydmxwbBCeAhBPcKqj9ghEWvY8FC5sdwy4SMfrZYEsRs",
  "neuron": "/ipfs/…", // blockchain neuron
  "created_date": 1498141924, // date of creation (unix timestamp)
  "charge_node": 1, // 1e-6 coins per second per node
  "exec_time": 60, // run for 60 seconds
  "exec_interval": 60, // collect state every 60 seconds
  "charge_limit": 60, // 6e-5 coins per second of execution
  "parallel_run": -1, // Infinite parallel executions
  "verify_times": -1, // Infinite verifications (whats left after execution)
  "verify_node": ["/ipns/…", "/ipns/…"], // nodes that need to verify the results
  "verify_state": "/ipfs/QmQXvQ4GcxZtXnL9TfyhBEkyp7i1GeDQKQsV5DY5Z75fKz", // neuron verifying states
  "verify_neuron": "/ipfs/QmakKWzpKArjNc5puKuwuCNqCnpF3HmK11xzREhiqz7gVV", // neuron verifying results
  "node_capital": 1e9, // minimum node locked capital
  "locked_coins": 1e9, // funds for running blockchain 
  "signature": "…" // signed contract
}

IPFN Contract execution

Contract execution

Execution of the contract results in a new contract being stored on the blockchain.

Created contract if not stateless will have a new state value.

Execution interval

Contracts can execute with intervals. It can be used like CRON,

Blockchain applications on top of IPFN benefit from it by collecting transactions and squashing into blocks in constant time intervals.

Contracts state

State of the contract is collected verified result generated during execution of the contract and can be stored on IPFS to ensure continuum of execution.

On top of operations verification state can be verified using additional verify_state neuron.

Example execution results

{
  "id": "Qmcx7FBSe4PzuunfdvUjBJ84C8uMmsqRzuNttNUXG95M1X",
  "state": "QmQJgv2FjxQPi6QRi7vWJCgQfBHgmMbKUjHhu9toLTcV6D",
  "contract": "QmekgFZREtKWPoU8SBLReDbjEYj1vHM12h65weE1pd2LAB",
  "total_cost": 1314,
  "created_date": 1498833155,
  "failures": 11,
  "executions": [
    {
      "coins": 293,
      "usage": "",
      "node": "QmS34LpsUDzNhkMuQyoggdsqZ9iRHd33ZroWqABEVGwndC",
      "sess": "QmYo6EuJD5YtMBrHgxu7J5U3ZMKw5FdXufzJwMbWWbsfEe",
      "state": "QmfDQCxU4orURQ9fCchvxv7CGeEcN2CPRa2MKtSzNvt2gR",
      "sign": "QmRcNUrnrJCny1V54a6eUcqrMoqKSXsKofsFGKCW9FtZQm",
      "calls": 1300
    },
    {
      "coins": 342,
      "usage": "",
      "node": "QmRXHi5buQiTH5c8MgLkUToBNADPLr9C3mRmsX57EFAJay",
      "sess": "QmQJgv2FjxQPi6QRi7vWJCgQfBHgmMbKUjHhu9toLTcV6D",
      "state": "QmWNqZWDfsCvky21ggwFziakgnsLCrmSbPtC19YXVJZRV7",
      "sign": "Qmf4DTb5cXzWCyfLkyHGj43KbP7gkHxAmF4n3oHE6hhiUX",
      "calls": 1311
    },
    {
      "coins": 321,
      "usage": "",
      "node": "QmRXHi5buQiTH5c8MgLkUToBNADPLr9C3mRmsX57EFAJay",
      "sess": "QmQJgv2FjxQPi6QRi7vWJCgQfBHgmMbKUjHhu9toLTcV6D",
      "state": "QmWNqZWDfsCvky21ggwFziakgnsLCrmSbPtC19YXVJZRV7",
      "sign": "QmcRU4bHYRq82j3JjLVd2PeRfHA4Rs5eNfwdU6pv56LvUJ",
      "calls": 1311
    },
    {
      "coins": 284,
      "usage": "",
      "node": "QmfBKZwefZiA8qEsgxQ5XMCjjWvg26qoQvAfFhWLvaV4DM",
      "sess": "QmQJgv2FjxQPi6QRi7vWJCgQfBHgmMbKUjHhu9toLTcV6D",
      "state": "QmYtKwZKhzd33if4YS9zXhp522wjmTcb53bXTuitor7tqF",
      "sign": "QmbEforGJJTAnZJdkCuTCr69SEpJK7fVT1zo3wk9jAiMdC",
      "calls": 1311
    }
  ],
  "verifications": [
    {
      "coins": 42,
      "usage": "",
      "node": "QmXpgPrQBB4gMfn6hacq3GyyUuF4Az6JjB31JxvvJCr6LN",
      "sess": "QmbUZjQFtFFuJyQUiZHmcvojChLDXTM3NewKphpuNye6tA",
      "state": "QmCtKwZKhzd33if4YS9zXhp522wjmTcb53bXTuitor7tqD",
      "sign": "QmRcNUrnrJCny1V54a6eUcqrMoqKSXsKofsFGKCW9FtZQm",
      "calls": 5233
    },
    {
      "coins": 32,
      "usage": "",
      "node": "QmQJvpVa1ziEce7fdAocRQUrfBAwVPMTKd6HSr6QW72NUL",
      "sess": "QmYRqbTdmKZcRx6QwqFCLxbBVeGGc3dxz8NWykepxcmFvd",
      "state": "QmCtKwZKhzd33if4YS9zXhp522wjmTcb53bXTuitor7tqD",
      "sign": "QmViRZnf11kpjVdpZKFdk1jfnPKyM6JKgyqfcSFdWNyQDs",
      "calls": 5233
    }
  ]
}

IPFN Blockchain features

Parallel runs

Contract can have -1 or Infinite parallel executions. Every node running the contract will be rewarded for running it.

Charge limits

Limits on charges can be set per second so you can make sure funds won't be spent all at once and ensuring long time of contract life.

Contract funding

Funding can be created for the contract by specifying it as a previous when creating a new one.

{
  "id": "QmZ5JxYN3BBaijWjs3quqN3XxQQuKZYu1YNwtp8QSjdLRh",
  "locked": true,
  "previous": "QmUyERaqHBehKc6tjHTN1LFyNNWBTHvkBwoccBPNiNfX4m", // funded contract
  "locked_coins": 10000000,
  "created_date": 1498141924,
  "signature": "…"
}

Funds from funding contracts are spent only when contract funds are insufficient or when adds additional executions.

You can also specify more requirements about how to spend funds and to add up executions:

  • charge_node – how much can node charge per second of execution
  • charge_limit – limit of total execution cost per second
  • parallel_run – adds up to previous (-1 is Infinite, default is 0)
  • verify_times – adds up to previous (-1 is Infinite, default is 0)
  • node_capital – funds node needs requires to participate in execution

If above parameters are not changed and contract is locked funds are squashed into previous contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment