Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel tx serializations #284

Merged
merged 34 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
077a6ff
Improve channel rpc usage
mpowaga Mar 5, 2019
6e4b3b4
Fix lint error
mpowaga Mar 5, 2019
528b6c3
Remove unreachable code
mpowaga Mar 5, 2019
cb53c32
Make sure that sign function is correctly called
mpowaga Mar 5, 2019
28d3423
Merge branch 'develop' into feature/improve-channel-tests
nduchak Mar 6, 2019
228f65d
Merge branch 'develop' into feature/channel-rpc-improvement
nduchak Mar 6, 2019
8184360
Improve error handling for update method
mpowaga Mar 6, 2019
81d4f43
Add missing channel tx serializations
mpowaga Mar 7, 2019
dbb8fa5
Add channel close solo and settle tx serialization
mpowaga Mar 12, 2019
ec9a12b
Add channel slash tx serialization
mpowaga Mar 26, 2019
e3bc811
Add proof of inclusion tx serialization
mpowaga Mar 27, 2019
27b94ba
Merge branch 'feature/channel-rpc-improvement' into feature/channel-t…
mpowaga Mar 27, 2019
83b2495
Add basic merkle patricia tree implementation
mpowaga Mar 28, 2019
6dab142
Add merkle patricia tree serialization and verify function
mpowaga Mar 29, 2019
415751e
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Mar 30, 2019
dec757b
fix(schema.js): Fix linter error
Mar 30, 2019
cf9bcc5
Improve channel tests and error handling (#276)
mpowaga Apr 1, 2019
1d2e3a6
Improve state channel params handling. Fixes #299 (#300)
mpowaga Apr 1, 2019
8e01600
Compiler improvements (#303)
nduchak Apr 1, 2019
988f089
Channel contracts (#279)
mpowaga Apr 2, 2019
28ba57e
Merge branch 'develop' into feature/channel-tx-serializations
mpowaga Apr 2, 2019
3924b8f
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 5, 2019
c58af67
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 9, 2019
a25de21
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 9, 2019
504f03b
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 9, 2019
69bcf3c
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 10, 2019
01b004a
Fix channel tests
mpowaga Apr 10, 2019
a97e30f
Add contract call tx serialization
mpowaga Apr 12, 2019
6822692
Add channel tx serialization
mpowaga Apr 12, 2019
fb70fb1
Add missing tree tx serializations
mpowaga Apr 12, 2019
c86bd4b
Add channel snapshot solo tx serialization
mpowaga Apr 12, 2019
182d459
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 12, 2019
01f7ef7
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 16, 2019
e843b35
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion es/channel/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
changeStatus,
changeState,
send,
emit
emit,
channelId
} from './internal'
import { unpackTx } from '../tx/builder'

Expand Down Expand Up @@ -86,6 +87,7 @@ export function awaitingBlockInclusion (channel, message, state) {

export function awaitingOpenConfirmation (channel, message, state) {
if (message.method === 'channels.info' && message.params.data.event === 'open') {
channelId.set(channel, message.params.channel_id)
return { handler: awaitingInitialState }
}
}
Expand Down
11 changes: 11 additions & 0 deletions es/channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
initialize,
enqueueAction,
send,
channelId,
call
} from './internal'
import * as R from 'ramda'
Expand Down Expand Up @@ -70,6 +71,15 @@ async function state () {
return snakeToPascalObjKeys(await call(this, 'channels.get.offchain_state', {}))
}

/**
* Get channel id
*
* @return {string}
*/
function id () {
return channelId.get(this)
}

/**
* Trigger an update
*
Expand Down Expand Up @@ -546,6 +556,7 @@ const Channel = AsyncInit.compose({
on,
status,
state,
id,
update,
poi,
balances,
Expand Down
2 changes: 2 additions & 0 deletions es/channel/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const messageQueueLocked = new WeakMap()
const actionQueue = new WeakMap()
const actionQueueLocked = new WeakMap()
const sequence = new WeakMap()
const channelId = new WeakMap()
const rpcCallbacks = new WeakMap()

function channelURL (url, params, endpoint = 'channel') {
Expand Down Expand Up @@ -196,5 +197,6 @@ export {
changeState,
send,
enqueueAction,
channelId,
call
}
2 changes: 1 addition & 1 deletion es/tx/builder/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { BigNumber } from 'bignumber.js'

export const createSalt = salt

const base64Types = ['tx', 'st', 'ss', 'pi', 'ov', 'or', 'cb']
const base64Types = ['tx', 'st', 'ss', 'pi', 'ov', 'or', 'cb', 'cs']

/**
* Build a contract public key
Expand Down
40 changes: 38 additions & 2 deletions es/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './schema'
import { readInt, readId, readPointers, writeId, writeInt, buildPointers, encode, decode } from './helpers'
import { toBytes } from '../../utils/bytes'
import * as mpt from '../../utils/mptree'

/**
* JavaScript-based Transaction builder
Expand All @@ -37,6 +38,10 @@ function deserializeField (value, type, prefix) {
return readInt(value)
case FIELD_TYPES.id:
return readId(value)
case FIELD_TYPES.ids:
return value.map(readId)
case FIELD_TYPES.bool:
return value[0] === 1
case FIELD_TYPES.binary:
return encode(value, prefix)
case FIELD_TYPES.string:
Expand All @@ -45,11 +50,26 @@ function deserializeField (value, type, prefix) {
return readPointers(value)
case FIELD_TYPES.rlpBinary:
return unpackTx(value, true)
case FIELD_TYPES.rlpBinaries:
return value.map(v => unpackTx(v, true))
case FIELD_TYPES.rawBinary:
return value
case FIELD_TYPES.hex:
return value.toString('hex')
case FIELD_TYPES.offChainUpdates:
return value.map(v => unpackTx(v, true))
case FIELD_TYPES.callStack:
// TODO: fix this
return [readInt(value)]
case FIELD_TYPES.mptree:
return value.map(mpt.deserialize)
case FIELD_TYPES.callReturnType:
switch (readInt(value)) {
case '0': return 'ok'
case '1': return 'error'
case '2': return 'revert'
default: return value
}
default:
return value
}
Expand All @@ -61,14 +81,29 @@ function serializeField (value, type, prefix) {
return writeInt(value)
case FIELD_TYPES.id:
return writeId(value)
case FIELD_TYPES.ids:
return value.map(writeId)
case FIELD_TYPES.bool:
return Buffer.from([value ? 1 : 0])
case FIELD_TYPES.binary:
return decode(value, prefix)
case FIELD_TYPES.hex:
return Buffer.from(value, 'hex')
case FIELD_TYPES.signatures:
return value.map(Buffer.from)
case FIELD_TYPES.string:
return toBytes(value)
case FIELD_TYPES.pointers:
return buildPointers(value)
case FIELD_TYPES.mptree:
return value.map(mpt.serialize)
case FIELD_TYPES.callReturnType:
switch (value) {
case 'ok': return writeInt(0)
case 'error': return writeInt(1)
case 'revert': return writeInt(2)
default: return value
}
default:
return value
}
Expand Down Expand Up @@ -250,18 +285,19 @@ export function unpackRawTx (binary, schema) {
* @param {String} type Transaction type
* @param {Object} [options={}] options
* @param {Object} [options.excludeKeys] excludeKeys Array of keys to exclude for validation and build
* @param {String} [options.prefix] Prefix of transaction
* @throws {Error} Validation error
* @return {Object} { tx, rlpEncoded, binary } Object with tx -> Base64Check transaction hash with 'tx_' prefix, rlp encoded transaction and binary transaction
*/
export function buildTx (params, type, { excludeKeys = [] } = {}) {
export function buildTx (params, type, { excludeKeys = [], prefix = 'tx' } = {}) {
if (!TX_SERIALIZATION_SCHEMA[type]) {
throw new Error('Transaction serialization not implemented for ' + type)
}
const [schema, tag] = TX_SERIALIZATION_SCHEMA[type]
const binary = buildRawTx({ ...params, VSN, tag }, schema, { excludeKeys }).filter(e => e !== undefined)

const rlpEncoded = rlp.encode(binary)
const tx = encode(rlpEncoded, 'tx')
const tx = encode(rlpEncoded, prefix)

return { tx, rlpEncoded, binary, txObject: unpackRawTx(binary, schema) }
}
Expand Down
Loading