Skip to content

Commit

Permalink
update contract method names
Browse files Browse the repository at this point in the history
  • Loading branch information
Ameen Soleimani committed Jun 18, 2017
1 parent 96acbd7 commit 2f2b4fa
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 87 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ enum ChannelState { Open, Checkpointing, Closing, Closed }
```

- `expiration` - block number after which the channel expires and can be closed by anyone (set in `openChannel`)
- `challengeTimeout` - block number after which a proposed checkpoint or valid challenge can be finalized (set in `proposeCheckpointChannel` and `challengeCheckpointChannel`)
- `challengeTimeout` - block number after which a proposed checkpoint or valid challenge can be finalized (set in `proposeCheckpoint` and `challengeCheckpoint`)
- `proposedRoot` - a placeholder root which is only stored and set after the challenge period is over

#### Checkpointing the Channel
Expand All @@ -178,7 +178,7 @@ The signature provided must be from the demand and is verified in the contract m
If `renew` is set to `true`, the channel will remain open once the checkpoint is completed.

```
function proposeCheckpointChannel(
function proposeCheckpoint(
bytes32 channelId,
bytes32 proposedRoot,
bytes signature,
Expand All @@ -191,7 +191,7 @@ function proposeCheckpointChannel(
Proposing a checkpoint triggers a challenge period during which either party can challenge the `proposedRoot` by submitting a signed state update -- the `challengeRoot` -- with a higher impression count (the impressions count is sequence number). The index of the impressions in the state array and the corresponding merkle proof are required to verify that the impressions value is included in the `challengeRoot`.

```
function challengeCheckpointChannel(
function challengeCheckpoint(
bytes32 channelId,
bytes32 challengeRoot,
uint256 impressions,
Expand All @@ -206,7 +206,7 @@ function challengeCheckpointChannel(
If a challenge was issued, the challenge period is reset, providing time to answer the challenge. To accept the challenge, the party must provide an impressions count which can be proven to be included in the original `proposedRoot`, and is higher than the impressions in the challenge. Should this be the case, the checkpointing immediately completes and the original `proposedRoot` is recorded.

```
function acceptChallengeCheckpointChannel(
function acceptChallenge(
bytes32 channelId,
uint256 impressions,
uint256 index,
Expand Down
58 changes: 28 additions & 30 deletions contracts/AdMarket.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
pragma solidity ^0.4.7;

// 5/6/2017 Cleanup
//
// Goal is to ship, can add features later
// - demo parallel impression tracking system
// - can inform clearing / settlement

// Refactor to optionally renew the channel. By default, checkpointing will close.
// Either party can checkpoint and opt to not renew, which closes the channel.

import "ECVerify.sol";

// Registers supply and demand, facilitates discovery, and manages the impression tracking state channels between them
Expand All @@ -18,11 +9,21 @@ contract AdMarket is ECVerify {

address owner;
string ownerUrl;

mapping (address => string) public registeredDemand;
// registeredDemand[0xabc...] => toyota.adserver.com

mapping (address => string) public registeredSupply;
// registeredSupply[0xdef...] => nyt.adserver.com

mapping (bytes32 => Channel) channels;
// channels[channelId] => channel metadata

mapping (bytes32 => Challenge) challenges;
mapping (address => address[]) channelPartners;
// channels[channelId] => channel challenge metadata

mapping (address => mapping (address => bool)) channelPartners;
// channelPartners[demand][supply] => true/false

uint256 public channelCount = 0;
uint256 public channelTimeout; // max lifetime of a channel in blocks
Expand All @@ -41,13 +42,13 @@ contract AdMarket is ECVerify {
// Metadata (not included in offchain state updates)
ChannelState state;
uint256 expiration; // block number after which the channel expires and can be closed by anyone (set in openChannel)
uint256 challengeTimeout; // block number after which the channel can be closed by its participants (set in proposeCloseChannel)
uint256 challengeTimeout; // block number after which the channel can be closed by its participants (set in proposeCheckpointChannel and challengeCheckpointChannel)
bytes32 proposedRoot; // a placeholder root which is only stored and set after the challenge period is over
}

// Note: Root is the merkle root of the previous root and the current state. The current state includes:
// - balance: demand -> supply
// - impressions (#)
// - impressions (#) (sequence number)
// - impressionId
// - impressionPrice
// In case of a dispute, the data can be made public and verified onchain using merkle proofs.
Expand Down Expand Up @@ -115,6 +116,7 @@ contract AdMarket is ECVerify {
}

function registerSupply(address supply, string url) only_owner {
if (isEmptyString(url)) throw; // must at least provide a non-empty string to update later
registeredSupply[supply] = url;
SupplyRegistered(supply);
}
Expand Down Expand Up @@ -151,12 +153,7 @@ contract AdMarket is ECVerify {
if (!isRegisteredSupply(supply)) throw;

// Check that we don't already have a channel open with the supply
// TODO with enough channel partners, this check will always run out of gas
// This will limit the number of channels any party can have
address[] storage partners = channelPartners[demand];
for (uint256 i = 0; i < partners.length; i++) {
if (partners[i] == supply) throw;
}
if (channelPartners[demand][supply]) throw;

bytes32 channelId = sha3(channelCount++);
uint256 expiration = block.number + channelTimeout;
Expand All @@ -173,15 +170,14 @@ contract AdMarket is ECVerify {
0 // proposed root
);

channelPartners[demand].push(supply);
channelPartners[demand][supply] = true;
}

// Either supply or demand can checkpoint a channel at any time
// We have to have a challenge period because we aren't tracking sequence number (impressions) onchain.
// Checkpointing gives us the ability to have long-lived channels without downtime.
// The channel participants can elect to renew the channel during a checkpoint.
// If a channel is open and also has a challengeTimeout, that challengeTimeout is interpreted as a checkpoint challenge period.
function proposeCheckpointChannel(
function proposeCheckpoint(
bytes32 channelId,
bytes32 proposedRoot,
bytes signature,
Expand Down Expand Up @@ -240,14 +236,14 @@ contract AdMarket is ECVerify {
// They also supply the proof for this impression count and the Demand's signature on it.
// This resets the challengeTimeout giving the counterparty a chance to accept this challenge.
// To accept the challenge, the counterparty must prove that the original checkpointed root has more impressions
function challengeCheckpointChannel(
bytes32 channelId,
bytes32 challengeRoot,
uint256 impressions,
uint256 index,
bytes merkleProof,
bytes signature
) {
function challengeCheckpoint(
bytes32 channelId,
bytes32 challengeRoot,
uint256 impressions,
uint256 index,
bytes merkleProof,
bytes signature
) {
Channel channel = channels[channelId];

// Check that msg.sender is either demand or supply
Expand Down Expand Up @@ -291,7 +287,7 @@ function challengeCheckpointChannel(
// and the state is finalized by the checkPointChannel function
// If the participants intend to renew, the channel will stay open and its expiration block will reset.
// Otherwise the channel will close.
function acceptChallengeCheckpointChannel(
function acceptChallenge(
bytes32 channelId,
uint256 impressions,
uint256 index,
Expand Down Expand Up @@ -327,6 +323,7 @@ function challengeCheckpointChannel(
// close channel
} else {
channel.state = ChannelState.Closed;
channelPartners[channel.demand][channel.supply] = false;
}

// even if the channel is closed, we want to record the final state root
Expand Down Expand Up @@ -372,6 +369,7 @@ function challengeCheckpointChannel(
// close channel
} else {
channel.state = ChannelState.Closed;
channelPartners[channel.demand][channel.supply] = false;
}

channel.proposedRoot = 0;
Expand Down
12 changes: 8 additions & 4 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

const request = require('request-promise')

const mode = process.env.mode

const wait = timeout => new Promise(resolve => setTimeout(resolve, timeout))

function generateImpressions (count, price, supplyId, demandId) {
Expand All @@ -21,14 +23,16 @@ const impressions = generateImpressions(2, 1, supplyId, demandId)
console.log(impressions)

async function openChannel () {
// Supply
await request('https://localhost:3000/open')
console.log('Opened Demand')

// Demand
await request('https://localhost:3001/open')
console.log('Opened Supply')

// AdMarket
await request('https://localhost:3002/open')
console.log('Opened AdMarket')

console.log('Connected to all adservers')
}

async function sendImpression (impression) {
Expand All @@ -54,7 +58,7 @@ async function sendImpression (impression) {
async function main () {
await openChannel()

for (const impression of impressions) {
for (let impression of impressions) {
await sendImpression(impression)
}

Expand Down
1 change: 0 additions & 1 deletion js/servers/admarket.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const p = Promise.promisify

const privKey = new Buffer(config.adMarket.privKey, 'hex')

// const rootReducer = combineReducers(admarketChannelsReducer)
const store = createStore(admarketChannelsReducer)
const dispatch = store.dispatch

Expand Down
3 changes: 0 additions & 3 deletions js/servers/demand.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import { channelsReducer } from '../reducers'
import { impressionDB, channelDB } from '../storage'
import { makeChannel, makeUpdate } from '../channel'

// TODO should be API client for supply

const web3 = new Web3()

const p = Promise.promisify

// const rootReducer = combineReducers(channelsReducer)
const store = createStore(channelsReducer)
const dispatch = store.dispatch

Expand Down
1 change: 0 additions & 1 deletion js/servers/supply.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const web3 = new Web3()

const p = Promise.promisify

// const rootReducer = combineReducers(supplyChannelsReducer)
const store = createStore(supplyChannelsReducer)
const dispatch = store.dispatch

Expand Down
6 changes: 3 additions & 3 deletions js/storage/A_DATA_CHANNEL
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"root":"0x21924a468ff0d684e04ad6256ad2a905899c59e7584868b1813edd242cef8eb7","impressionId":"1","demandId":"0x11111111111111111111","proposedRoot":0,"price":1,"supplyId":"0x22222222222222222222","impressions":1,"demand":"0x11111111111111111111","contractId":"0x12345123451234512345","time":1492462613.322,"supply":"0x22222222222222222222","expiration":100,"state":0,"pendingUpdates":[],"prevRoot":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","signature":"0x0147858e64f59b2c03fad54fc62e91fe8049ed2f810f18f555157ffe7d3398861e6734a1d9a1ecb6503df271fd345f3996ed6f760dd5ae54e6d71da659de1a921b","balance":1,"channelId":"0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d","challengeTimeout":100,"_id":"z1C16TSQXuulg7Bw"}
{"$$deleted":true,"_id":"z1C16TSQXuulg7Bw"}
{"contractId":"0x12345123451234512345","channelId":"0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d","demand":"0x11111111111111111111","supply":"0x22222222222222222222","impressionId":"foo","price":1,"impressions":0,"root":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","balance":0,"state":0,"expiration":100,"challengeTimeout":100,"proposedRoot":0,"pendingUpdates":{"size":0,"_origin":0,"_capacity":0,"_level":5,"__altered":false},"_id":"nxh7X7kCUaJ6kNmQ"}
{"root":"0x21924a468ff0d684e04ad6256ad2a905899c59e7584868b1813edd242cef8eb7","impressionId":"1","demandId":"0x11111111111111111111","proposedRoot":0,"price":1,"supplyId":"0x22222222222222222222","impressions":1,"demand":"0x11111111111111111111","contractId":"0x12345123451234512345","time":1492462635.747,"supply":"0x22222222222222222222","expiration":100,"state":0,"pendingUpdates":[],"prevRoot":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","signature":"0x0147858e64f59b2c03fad54fc62e91fe8049ed2f810f18f555157ffe7d3398861e6734a1d9a1ecb6503df271fd345f3996ed6f760dd5ae54e6d71da659de1a921b","balance":1,"channelId":"0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d","challengeTimeout":100,"_id":"nxh7X7kCUaJ6kNmQ"}
{"$$deleted":true,"_id":"nxh7X7kCUaJ6kNmQ"}
{"contractId":"0x12345123451234512345","channelId":"0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d","demand":"0x11111111111111111111","supply":"0x22222222222222222222","impressionId":"foo","price":1,"impressions":0,"root":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","balance":0,"state":0,"expiration":100,"challengeTimeout":100,"proposedRoot":0,"pendingUpdates":{"size":0,"_origin":0,"_capacity":0,"_level":5,"__altered":false},"_id":"60843GintTpSIjQI"}
{"root":"0x8e16c43f3559d28eee9725af2ae48f8846aefe522700ad3928abae3ab4d9e0b4","impressionId":"1","demandId":"0x11111111111111111111","proposedRoot":0,"price":1,"supplyId":"0x22222222222222222222","impressions":1,"demand":"0x11111111111111111111","contractId":"0x12345123451234512345","time":1497662040.593,"supply":"0x22222222222222222222","expiration":100,"state":0,"pendingUpdates":[],"prevRoot":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","signature":"0x8e6182c4610f4bc1f5512b771b32ebdced120a91011398cb283073a345a92fe67e9b42ed1c810a245aabf11d5de68868b3977e98825ffea8b34230d73c6fd7a81c","balance":1,"channelId":"0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d","challengeTimeout":100,"_id":"60843GintTpSIjQI"}
16 changes: 8 additions & 8 deletions js/storage/A_DATA_IMPRESSION
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"1","time":1492462613.322,"impressions":1,"balance":1,"root":"0x21924a468ff0d684e04ad6256ad2a905899c59e7584868b1813edd242cef8eb7","prevRoot":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","signature":"0x0147858e64f59b2c03fad54fc62e91fe8049ed2f810f18f555157ffe7d3398861e6734a1d9a1ecb6503df271fd345f3996ed6f760dd5ae54e6d71da659de1a921b","_id":"PEXkPkkcyFkL4ABu"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"1","time":1492462613.322,"_id":"nIrC1U9JPo1CWM69"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"2","time":1492462613.322,"_id":"wOfD4tIsad5GWgLx"}
{"$$deleted":true,"_id":"PEXkPkkcyFkL4ABu"}
{"$$deleted":true,"_id":"nIrC1U9JPo1CWM69"}
{"$$deleted":true,"_id":"wOfD4tIsad5GWgLx"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"1","time":1492462635.747,"_id":"eyg9Uhs2d3Sa9m7J"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"2","time":1492462635.747,"_id":"bo7QAw4umFVhgEP8"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"1","time":1492462635.747,"impressions":1,"balance":1,"root":"0x21924a468ff0d684e04ad6256ad2a905899c59e7584868b1813edd242cef8eb7","prevRoot":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","signature":"0x0147858e64f59b2c03fad54fc62e91fe8049ed2f810f18f555157ffe7d3398861e6734a1d9a1ecb6503df271fd345f3996ed6f760dd5ae54e6d71da659de1a921b","_id":"QZXJhDZVsohmFuVs"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"2","time":1492462635.747,"_id":"bo7QAw4umFVhgEP8"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"1","time":1492462635.747,"_id":"eyg9Uhs2d3Sa9m7J"}
{"$$deleted":true,"_id":"QZXJhDZVsohmFuVs"}
{"$$deleted":true,"_id":"bo7QAw4umFVhgEP8"}
{"$$deleted":true,"_id":"eyg9Uhs2d3Sa9m7J"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"1","time":1497662040.593,"impressions":1,"balance":1,"root":"0x8e16c43f3559d28eee9725af2ae48f8846aefe522700ad3928abae3ab4d9e0b4","prevRoot":"0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d","signature":"0x8e6182c4610f4bc1f5512b771b32ebdced120a91011398cb283073a345a92fe67e9b42ed1c810a245aabf11d5de68868b3977e98825ffea8b34230d73c6fd7a81c","_id":"cvLaW4Ef1XqHBj4y"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"1","time":1497662040.593,"_id":"kwVW7dbmDY2arVQA"}
{"price":1,"supplyId":"0x22222222222222222222","demandId":"0x11111111111111111111","impressionId":"2","time":1497662040.593,"_id":"r7y3S43tnV99p2C3"}
Loading

0 comments on commit 2f2b4fa

Please sign in to comment.