Skip to content

Commit

Permalink
Feat/399/fix state channel exit claim (#401)
Browse files Browse the repository at this point in the history
- Adding optional ChannelID filter to SignedByQuantifier to make state channel tests work properly
- Changing channelId to channelID everywhere because it makes more sense
  • Loading branch information
willmeister committed Aug 20, 2019
1 parent 29a16e6 commit c4cc067
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/app/ovm/deciders/examples/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Utils {
return (
!!message &&
!!other &&
message.message.channelId.equals(other.message.channelId) &&
message.message.channelID.equals(other.message.channelID) &&
message.message.nonce.equals(other.message.nonce) &&
(message.sender.equals(other.sender) ||
message.sender.equals(other.recipient)) &&
Expand Down
36 changes: 18 additions & 18 deletions packages/core/src/app/ovm/examples/state-channel-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ export class StateChannelClient {
addressBalance: AddressBalance,
recipient: Buffer
): Promise<SignedMessage> {
let channelId: Buffer = await this.messageDB.getChannelForCounterparty(
let channelID: Buffer = await this.messageDB.getChannelForCounterparty(
recipient
)
let nonce: BigNumber

if (!!channelId) {
if (!!channelID) {
const [lastValid, lastSigned, exited]: [
ParsedMessage,
ParsedMessage,
boolean
] = await Promise.all([
this.messageDB.getMostRecentValidStateChannelMessage(channelId),
this.messageDB.getMostRecentMessageSignedBy(channelId, this.myAddress),
this.messageDB.isChannelExited(channelId),
this.messageDB.getMostRecentValidStateChannelMessage(channelID),
this.messageDB.getMostRecentMessageSignedBy(channelID, this.myAddress),
this.messageDB.isChannelExited(channelID),
])

if (
Expand All @@ -89,15 +89,15 @@ export class StateChannelClient {

nonce = lastValid.message.nonce.add(ONE)
} else {
channelId = Buffer.from(uuid.v4())
channelID = Buffer.from(uuid.v4())
nonce = ONE
}

return this.signAndSaveMessage({
sender: this.myAddress,
recipient,
message: {
channelId,
channelID,
nonce,
data: { addressBalance },
},
Expand All @@ -114,19 +114,19 @@ export class StateChannelClient {
public async exitChannel(
counterparty: Buffer
): Promise<StateChannelExitClaim> {
const channelId: Buffer = await this.messageDB.getChannelForCounterparty(
const channelID: Buffer = await this.messageDB.getChannelForCounterparty(
counterparty
)

if (!channelId) {
if (!channelID) {
throw Error('Cannot exit a channel that does not exist.')
}

const mostRecent: ParsedMessage = await this.messageDB.getMostRecentValidStateChannelMessage(
channelId
channelID
)

await this.messageDB.markChannelExited(channelId)
await this.messageDB.markChannelExited(channelID)

return {
decider: AndDecider.instance(),
Expand Down Expand Up @@ -172,12 +172,12 @@ export class StateChannelClient {
* counter-claim that disproves it.
*
* TODO: Improve this signature so that channel ID doesn't need to be passed
* @param channelId the ChannelID in question
* @param channelID the ChannelID in question
* @param exitClaim the Exit claim in question
* @returns the counter-claim that the original claim is invalid
*/
public async handleChannelExit(
channelId: Buffer,
channelID: Buffer,
exitClaim: StateChannelExitClaim
): Promise<ImplicationProofItem[]> {
let decision: Decision
Expand All @@ -190,7 +190,7 @@ export class StateChannelClient {
}

if (!decision || decision.outcome) {
await this.messageDB.markChannelExited(channelId)
await this.messageDB.markChannelExited(channelID)
return undefined
}

Expand All @@ -210,7 +210,7 @@ export class StateChannelClient {
)

const existingMessage: ParsedMessage = await this.messageDB.getMessageByChannelIdAndNonce(
parsedMessage.message.channelId,
parsedMessage.message.channelID,
parsedMessage.message.nonce
)

Expand Down Expand Up @@ -268,10 +268,10 @@ export class StateChannelClient {
ParsedMessage,
ParsedMessage
] = await Promise.all([
this.messageDB.isChannelExited(message.message.channelId),
this.messageDB.isChannelExited(message.message.channelID),
this.messageDB.conflictsWithAnotherMessage(message),
this.messageDB.getMostRecentValidStateChannelMessage(
message.message.channelId
message.message.channelID
),
])
if (!!conflicts || exited) {
Expand Down Expand Up @@ -329,7 +329,7 @@ export class StateChannelClient {
return {
sender: this.myAddress,
signedMessage: objectToBuffer({
channelId: message.message.channelId.toString(),
channelID: message.message.channelID.toString(),
nonce: message.message.nonce,
data: stateChannelMessageToString(message.message
.data as StateChannelMessage),
Expand Down
18 changes: 15 additions & 3 deletions packages/core/src/app/ovm/quantifiers/signed-by-quantifier.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Quantifier, QuantifierResult } from '../../../types/ovm'
import { SignedByDBInterface } from '../../../types/ovm/db/signed-by-db.interface'
import { deserializeBuffer, deserializeMessage } from '../../serialization'
import { Message } from '../../../types/serialization'

interface SignedByQuantifierParameters {
address: Buffer
channelID?: Buffer
}

/*
Expand All @@ -22,9 +25,18 @@ export class SignedByQuantifier implements Quantifier {
public async getAllQuantified(
signerParams: SignedByQuantifierParameters
): Promise<QuantifierResult> {
const messages: Buffer[] = await this.db.getAllSignedBy(
signerParams.address
)
let messages: Buffer[] = await this.db.getAllSignedBy(signerParams.address)

if ('channelID' in signerParams && signerParams['channelID']) {
messages = messages.filter((m) => {
try {
const message: Message = deserializeBuffer(m, deserializeMessage)
return signerParams.channelID.equals(message.channelID)
} catch (e) {
return false
}
})
}

return {
results: messages,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/app/serialization/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export const stateChannelMessageToString = (
*/
export const messageToBuffer = (
message: Message,
messageSerializer: ({}) => string
messageSerializer: ({}) => string = (_) => '{}'
): Buffer => {
return objectToBuffer({
channelId: message.channelId.toString(),
channelID: message.channelID.toString(),
nonce: message.nonce,
data: messageSerializer(message.data),
})
Expand Down Expand Up @@ -87,11 +87,11 @@ export const deserializeBuffer = (
*/
export const deserializeMessage = (
message: string,
dataDeserializer: (string) => {}
dataDeserializer: (string) => {} = (d) => d
): Message => {
const parsedObject = deserializeObject(message)
return {
channelId: Buffer.from(parsedObject['channelId']),
channelID: Buffer.from(parsedObject['channelID']),
nonce:
'nonce' in parsedObject
? new BigNumber(parsedObject['nonce'])
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/types/ovm/db/message-db.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,66 @@ export interface MessageDB {
* Gets the ParsedMessage sent by the counterparty that has the same channelID and nonce
* but different data than the one that created locally and stored, if one exists.
*
* @param channelId the channel ID in question
* @param channelID the channel ID in question
* @param nonce the nonce in question
* @returns The message, if there is one
*/
getConflictingCounterpartyMessage(
channelId: Buffer,
channelID: Buffer,
nonce: BigNumber
): Promise<ParsedMessage>

/**
* Gets a specific message by the provided channel ID and nonce.
*
* @param channelId the channel ID in question
* @param channelID the channel ID in question
* @param nonce the nonce in question
* @returns The message, if there is one
*/
getMessageByChannelIdAndNonce(
channelId: Buffer,
channelID: Buffer,
nonce: BigNumber
): Promise<ParsedMessage>

/**
* Gets all messages signed by the provided signer address.
*
* @param signer the signer address to filter by
* @param channelId an optional channelId to filter by
* @param channelID an optional channelID to filter by
* @param nonce an optional nonce to filter by
* @returns the list of ParsedMessages that match the provided filters
*/
getMessagesSignedBy(
signer: Buffer,
channelId?: Buffer,
channelID?: Buffer,
nonce?: BigNumber
): Promise<ParsedMessage[]>

/**
* Gets all messages by the provided sender address.
*
* @param sender the sender address to filter by
* @param channelId an optional channelId to filter by
* @param channelID an optional channelID to filter by
* @param nonce an optional nonce to filter by
* @returns the list of ParsedMessages that match the provided filters
*/
getMessagesBySender(
sender: Buffer,
channelId?: Buffer,
channelID?: Buffer,
nonce?: BigNumber
): Promise<ParsedMessage[]>

/**
* Gets all messages by the provided recipient address.
*
* @param recipient the recipient address to filter by
* @param channelId an optional channelId to filter by
* @param channelID an optional channelID to filter by
* @param nonce an optional nonce to filter by
* @returns the list of ParsedMessages that match the provided filters
*/
getMessagesByRecipient(
recipient: Buffer,
channelId?: Buffer,
channelID?: Buffer,
nonce?: BigNumber
): Promise<ParsedMessage[]>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ export interface StateChannelMessageDBInterface
* Gets the ParsedMessage of the StateChannelMessage with the provided
* ChannelID that is signed by both parties and has the highest nonce.
*
* @param channelId The channel ID
* @param channelID The channel ID
* @returns The ParsedMessage, if one exits
*/
getMostRecentValidStateChannelMessage(
channelId: Buffer
channelID: Buffer
): Promise<ParsedMessage>

/**
* Gets the ParsedMessage of the StateChannelMessage with the provided
* ChannelID that is signed by the provided address and has the highest nonce.
*
* @param channelId The channel ID
* @param channelID The channel ID
* @param address The signer's address
* @returns The ParsedMessage, if one exits
*/
getMostRecentMessageSignedBy(
channelId: Buffer,
channelID: Buffer,
address: Buffer
): Promise<ParsedMessage>

Expand All @@ -47,17 +47,17 @@ export interface StateChannelMessageDBInterface
* Determines whether the channel with the provided ChannelID has been
* exited or has an active exit attempt by either party.
*
* @param channelId The Channel ID in question
* @param channelID The Channel ID in question
* @returns True if exited, false otherwise.
*/
isChannelExited(channelId: Buffer): Promise<boolean>
isChannelExited(channelID: Buffer): Promise<boolean>

/**
* Marks the provided Channel ID as exited.
*
* @param channelId The Channel ID in question
* @param channelID The Channel ID in question
*/
markChannelExited(channelId: Buffer): Promise<void>
markChannelExited(channelID: Buffer): Promise<void>

/**
* Gets the ChannelID associated with the provided counterparty address.
Expand All @@ -71,8 +71,8 @@ export interface StateChannelMessageDBInterface
* Determines whether or not the provided Channel ID represents a
* State Channel that we are a party of.
*
* @param channelId The Channel ID in question
* @param channelID The Channel ID in question
* @returns True if so, false otherwise
*/
channelIdExists(channelId: Buffer): Promise<boolean>
channelIDExists(channelID: Buffer): Promise<boolean>
}
2 changes: 1 addition & 1 deletion packages/core/src/types/serialization/message.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface SignedMessage {
}

export interface Message {
channelId: Buffer
channelID: Buffer
nonce?: BigNumber
data: {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('HashPreimageExistenceDecider', () => {

it('should decide true for valid preimage from Message', async () => {
await preimageDB.handleMessage({
channelId: Buffer.from('chan'),
channelID: Buffer.from('chan'),
data: { preimage },
})
const decision: Decision = await decider.decide({ hash })
Expand Down

0 comments on commit c4cc067

Please sign in to comment.