-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataStore-backed Deciders in place of witnesses (#384)
DB Changes: - Created HashPreimageDB for use in HashPreimageExistsDecider. Updating the HashPreimageExistsDecider and PreimageExistenceOnMultipleHashes tests to account for DB update - Added SignedByDB to store signatures for messages for use in SignedByDecider and SignedByQuantifier - Removed deprecated KeyValueStoreDecider - Made StateChannelMessageDBInterface extend MessageDB and SignedByDBInterface so that the StateChannelClient, SignedByQuantifier, SignedByDecider, etc. all use the same, consistent datastore Other Additions: - Added MessageSubscriber interface to subscribe to messages received Improvements: - Moving state-db.ts into db directory - Moving the keccak256 function into the crypto.ts file - Moving the HashFunction type into the crypto.ts file
- Loading branch information
1 parent
2dae7ed
commit 29a16e6
Showing
33 changed files
with
634 additions
and
667 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { HashPreimageDbInterface } from '../../../types/ovm/db' | ||
import { HashAlgorithm } from '../../../types/utils' | ||
import { DB } from '../../../types/db' | ||
import { hashFunctionFor } from '../../utils' | ||
import { Message } from '../../../types/serialization' | ||
|
||
interface Record { | ||
preimage: Buffer | ||
hashAlgorithm: HashAlgorithm | ||
hash: Buffer | ||
} | ||
|
||
/** | ||
* DB to store and access hashes and their associated preimages. | ||
*/ | ||
export class HashPreimageDb implements HashPreimageDbInterface { | ||
public constructor(private readonly db: DB) {} | ||
|
||
public async handleMessage(message: Message): Promise<void> { | ||
// TODO: handle each specific type of message when we formally define different messages. | ||
if (message.data && 'preimage' in message.data) { | ||
await this.storePreimage( | ||
Buffer.from(message.data['preimage']), | ||
HashAlgorithm.KECCAK256 | ||
) | ||
} | ||
} | ||
|
||
public async storePreimage( | ||
preimage: Buffer, | ||
hashAlgorithm: HashAlgorithm | ||
): Promise<void> { | ||
const hash: Buffer = hashFunctionFor(hashAlgorithm)(preimage) | ||
|
||
const serialized: Buffer = HashPreimageDb.serializeRecord({ | ||
preimage, | ||
hashAlgorithm, | ||
hash, | ||
}) | ||
|
||
await this.db.bucket(Buffer.from(hashAlgorithm)).put(hash, serialized) | ||
} | ||
|
||
public async getPreimage( | ||
hash: Buffer, | ||
hashAlgorithm: HashAlgorithm | ||
): Promise<Buffer | undefined> { | ||
const recordBuffer: Buffer = await this.db | ||
.bucket(Buffer.from(hashAlgorithm)) | ||
.get(hash) | ||
|
||
if (!recordBuffer) { | ||
return undefined | ||
} | ||
|
||
return HashPreimageDb.deserializeRecord(recordBuffer).preimage | ||
} | ||
|
||
private static serializeRecord(record: Record): Buffer { | ||
return Buffer.from( | ||
JSON.stringify({ | ||
preimage: record.preimage.toString(), | ||
hashAlgorithm: record.hashAlgorithm, | ||
hash: record.hash.toString(), | ||
}) | ||
) | ||
} | ||
|
||
private static deserializeRecord(serialized: Buffer): Record { | ||
const obj: {} = JSON.parse(serialized.toString()) | ||
return { | ||
preimage: Buffer.from(obj['preimage']), | ||
hashAlgorithm: obj['hashAlgorithm'], | ||
hash: Buffer.from(obj['hash']), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './hash-preimage-db' | ||
export * from './signed-by-db' | ||
export * from './state-db' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { SignedByDBInterface } from '../../../types/ovm/db/signed-by-db.interface' | ||
import { Message, SignedMessage } from '../../../types/serialization' | ||
import { DB } from '../../../types/db' | ||
import { decryptWithPublicKey, Md5Hash } from '../../utils' | ||
|
||
interface Record { | ||
signerPublicKey: Buffer | ||
signature: Buffer | ||
message: Buffer | ||
} | ||
|
||
/** | ||
* DB to store and access message signatures. | ||
*/ | ||
export class SignedByDB implements SignedByDBInterface { | ||
public constructor(private readonly db: DB) {} | ||
|
||
public async handleMessage( | ||
message: Message, | ||
signedMessage?: SignedMessage | ||
): Promise<void> { | ||
if (!!signedMessage) { | ||
await this.storeSignedMessage( | ||
signedMessage.signedMessage, | ||
signedMessage.sender | ||
) | ||
} | ||
} | ||
|
||
public async storeSignedMessage( | ||
signature: Buffer, | ||
signerPublicKey: Buffer | ||
): Promise<void> { | ||
const message: Buffer = decryptWithPublicKey( | ||
signerPublicKey, | ||
signature | ||
) as Buffer | ||
const serialized: Buffer = SignedByDB.serializeRecord({ | ||
signerPublicKey, | ||
signature, | ||
message, | ||
}) | ||
|
||
await this.db | ||
.bucket(signerPublicKey) | ||
.put(SignedByDB.getKey(message), serialized) | ||
} | ||
|
||
public async getMessageSignature( | ||
message: Buffer, | ||
signerPublicKey | ||
): Promise<Buffer | undefined> { | ||
const recordBuffer: Buffer = await this.db | ||
.bucket(signerPublicKey) | ||
.get(SignedByDB.getKey(message)) | ||
|
||
if (!recordBuffer) { | ||
return undefined | ||
} | ||
|
||
return SignedByDB.deserializeRecord(recordBuffer).signature | ||
} | ||
|
||
public async getAllSignedBy(signerPublicKey: Buffer): Promise<Buffer[]> { | ||
const signed: Buffer[] = await this.db | ||
.bucket(signerPublicKey) | ||
.iterator() | ||
.values() | ||
|
||
return signed.map((m) => SignedByDB.deserializeRecord(m).message) | ||
} | ||
|
||
private static getKey(message: Buffer): Buffer { | ||
return Md5Hash(message) | ||
} | ||
|
||
private static serializeRecord(record: Record): Buffer { | ||
return Buffer.from( | ||
JSON.stringify({ | ||
signerPublicKey: record.signerPublicKey.toString(), | ||
signature: record.signature.toString(), | ||
message: record.message.toString(), | ||
}) | ||
) | ||
} | ||
|
||
private static deserializeRecord(serialized: Buffer): Record { | ||
const obj: {} = JSON.parse(serialized.toString()) | ||
return { | ||
signerPublicKey: Buffer.from(obj['signerPublicKey']), | ||
signature: Buffer.from(obj['signature']), | ||
message: Buffer.from(obj['message']), | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/core/src/app/ovm/state-db.ts → packages/core/src/app/ovm/db/state-db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.