Skip to content

Commit

Permalink
Merge pull request #108 from plasma-group/dev-state-update
Browse files Browse the repository at this point in the history
Update how we represent state to match spec
  • Loading branch information
smartcontracts committed Mar 26, 2019
2 parents 1516c50 + 6adb6c2 commit 9fc272f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 174 deletions.
3 changes: 1 addition & 2 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './src/constants'
export * from './src/state-object'
export * from './src/sum-tree'
export * from './src/transaction'
export * from './src/utils'
export * from './src/state'
134 changes: 0 additions & 134 deletions packages/utils/src/state-object.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/utils/src/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './state-object'
export * from './state-update'
export * from './transaction'
31 changes: 31 additions & 0 deletions packages/utils/src/state/state-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Internal Imports */
import { abi } from '../utils'

const STATE_OBJECT_ABI_TYPES = ['address', 'bytes']

export interface StateObjectData {
predicate: string
parameters: string
}

/**
* Class that represents a simple state object.
* State objects are the fundamental building
* blocks of our plasma chain design.
*/
export class StateObject {
public predicate: string
public parameters: string

constructor(args: StateObjectData) {
this.predicate = args.predicate
this.parameters = args.parameters
}

/**
* @returns the encoded state object.
*/
get encoded(): string {
return abi.encode(STATE_OBJECT_ABI_TYPES, [this.predicate, this.parameters])
}
}
55 changes: 55 additions & 0 deletions packages/utils/src/state/state-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* External Imports */
import BigNum = require('bn.js')

/* Internal Imports */
import { abi } from '../utils'
import { StateObject } from './state-object'

const STATE_OBJECT_ABI_TYPES = [
'uint256',
'uint256',
'uint256',
'bytes',
'bytes',
]

interface StateUpdateArgs {
start: number | BigNum
end: number | BigNum
block: number | BigNum
plasmaContract: string
newState: StateObject
}

/**
* Represents a StateUpdate, which wraps each state
* update but doesn't have a witness.
*/
export class StateUpdate {
public start: BigNum
public end: BigNum
public block: BigNum
public plasmaContract: string
public newState: StateObject

constructor(args: StateUpdateArgs) {
this.start = new BigNum(args.start, 'hex')
this.end = new BigNum(args.end, 'hex')
this.block = new BigNum(args.block, 'hex')
this.plasmaContract = args.plasmaContract
this.newState = args.newState
}

/**
* @returns the encoded state update.
*/
get encoded(): string {
return abi.encode(STATE_OBJECT_ABI_TYPES, [
this.start,
this.end,
this.block,
this.plasmaContract,
this.newState.encoded,
])
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import BigNum = require('bn.js')
import { abi, keccak256 } from './utils'
import { StateObject } from './state-object'
import { MerkleTreeNode } from './sum-tree'
/* Internal Imports */
import { abi, keccak256 } from '../utils'
import { StateUpdate } from './state-update'

const TRANSACTION_ABI_TYPES = ['uint256', 'bytes[]', 'bytes', 'bytes']
const TRANSACTION_ABI_TYPES = ['bytes', 'bytes']

interface TransactionArgs {
block: number | BigNum
inclusionProof: MerkleTreeNode[]
witness: string
newState: StateObject
stateUpdate: StateUpdate
transactionWitness: string
}

/**
Expand All @@ -20,40 +17,21 @@ interface TransactionArgs {
const fromEncoded = (encoded: string): Transaction => {
const decoded = abi.decode(TRANSACTION_ABI_TYPES, encoded)
return new Transaction({
block: decoded[0],
inclusionProof: decoded[1],
witness: decoded[2],
newState: decoded[3],
stateUpdate: decoded[0],
transactionWitness: decoded[1],
})
}

/**
* Represents a basic plasma chain transaction.
*/
export class Transaction {
/**
* Casts a value to a Transaction.
* @param value Thing to cast to a Transaction.
* @returns the Transaction.
*/
public static from(value: string): Transaction {
if (typeof value === 'string') {
return fromEncoded(value)
}

throw new Error('Got invalid argument type when casting to Transaction.')
}

public block: BigNum
public inclusionProof: MerkleTreeNode[]
public witness: string
public newState: StateObject
public stateUpdate: StateUpdate
public transactionWitness: string

constructor(args: TransactionArgs) {
this.block = new BigNum(args.block, 'hex')
this.inclusionProof = args.inclusionProof
this.witness = args.witness
this.newState = args.newState
this.stateUpdate = args.stateUpdate
this.transactionWitness = args.transactionWitness
}

/**
Expand All @@ -68,10 +46,21 @@ export class Transaction {
*/
get encoded(): string {
return abi.encode(TRANSACTION_ABI_TYPES, [
this.block,
this.inclusionProof,
this.witness,
this.newState.encoded,
this.stateUpdate,
this.transactionWitness,
])
}

/**
* Casts a value to a Transaction.
* @param value Thing to cast to a Transaction.
* @returns the Transaction.
*/
public static from(value: string): Transaction {
if (typeof value === 'string') {
return fromEncoded(value)
}

throw new Error('Got invalid argument type when casting to Transaction.')
}
}

0 comments on commit 9fc272f

Please sign in to comment.