Skip to content

Commit

Permalink
Merge pull request #254 from willmeister/linting_and_getting_tests_pa…
Browse files Browse the repository at this point in the history
…ssing

Linting and getting tests passing
  • Loading branch information
karlfloersch committed Jun 11, 2019
2 parents ce9e49a + fffdc25 commit 8327614
Show file tree
Hide file tree
Showing 30 changed files with 287 additions and 178 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Most of the projects in `@pigi` are [`Node.js`](https://nodejs.org/en/) projects
You'll need to install `Node.js` for your system before continuing.
We've provided a [detailed explanation of now to install `Node.js`](https://docs.plasma.group/en/latest/src/pigi/reference.html#installing-node-js) on Windows, Mac, and Linux.

**Note**: This is confirmed to work on `Node.js v11.6`, but there may be issues on other versions. If you have trouble, please peg your Node.js version to 11.6.

#### Yarn
We're using a package manager called [Yarn](https://yarnpkg.com/en/).
You'll need to [install Yarn](https://yarnpkg.com/en/docs/install) before continuing.
Expand Down
6 changes: 4 additions & 2 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"test": "waffle waffle-config.json && mocha --require ts-node/register 'test/*.spec.ts'",
"lint": "tslint --format stylish --project .",
"fix": "prettier --config ./.prettierrc.js --write 'index.ts' '{src,test}/**/*.ts'",
"build": "waffle waffle-config.json && tsc -p ."
"build": "waffle waffle-config.json && tsc -p .",
"clean": "rimraf build/"
},
"keywords": [
"plasma",
Expand Down Expand Up @@ -38,7 +39,8 @@
"chai": "^4.2.0",
"mocha": "^6.0.2",
"typescript": "^3.3.3333",
"web3": "^1.0.0-beta.48"
"web3": "^1.0.0-beta.48",
"rimraf": "^2.6.3"
},
"dependencies": {
"@types/sinon-chai": "^3.2.2",
Expand Down
52 changes: 28 additions & 24 deletions packages/contracts/test/BasicTokenMock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
import chai = require('chai')
import {createMockProvider, deployContract, getWallets, solidity} from 'ethereum-waffle';
import * as BasicTokenMock from '../build/BasicTokenMock.json';
import {
createMockProvider,
deployContract,
getWallets,
solidity,
} from 'ethereum-waffle'
import * as BasicTokenMock from '../build/BasicTokenMock.json'

chai.use(solidity);
const {expect} = chai;
chai.use(solidity)
const { expect } = chai

describe('INTEGRATION: Example', () => {
const provider = createMockProvider();
const [wallet, walletTo] = getWallets(provider);
let token;
const provider = createMockProvider()
const [wallet, walletTo] = getWallets(provider)
let token

beforeEach(async () => {
token = await deployContract(wallet, BasicTokenMock, [wallet.address, 1000]);
});
token = await deployContract(wallet, BasicTokenMock, [wallet.address, 1000])
})

it('Assigns initial balance', async () => {
expect(await token.balanceOf(wallet.address)).to.eq(1000);
});
expect(await token.balanceOf(wallet.address)).to.eq(1000)
})

it('Transfer adds amount to destination account', async () => {
await token.transfer(walletTo.address, 7);
expect(await token.balanceOf(walletTo.address)).to.eq(7);
});
await token.transfer(walletTo.address, 7)
expect(await token.balanceOf(walletTo.address)).to.eq(7)
})

it('Transfer emits event', async () => {
await expect(token.transfer(walletTo.address, 7))
.to.emit(token, 'Transfer')
.withArgs(wallet.address, walletTo.address, 7);
});
.withArgs(wallet.address, walletTo.address, 7)
})

it('Can not transfer above the amount', async () => {
await expect(token.transfer(walletTo.address, 1007)).to.be.reverted;
});
await expect(token.transfer(walletTo.address, 1007)).to.be.reverted
})

it('Can not transfer from empty account', async () => {
const tokenFromOtherWallet = token.connect(walletTo);
await expect(tokenFromOtherWallet.transfer(wallet.address, 1))
.to.be.reverted;
});

});
const tokenFromOtherWallet = token.connect(walletTo)
await expect(tokenFromOtherWallet.transfer(wallet.address, 1)).to.be
.reverted
})
})
12 changes: 9 additions & 3 deletions packages/operator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"lint": "tslint --format stylish --project .",
"test": "mocha --require ts-node/register 'test/**/*.spec.ts'",
"fix": "prettier --config ./.prettierrc.js --write 'index.ts' '{src,test}/**/*.ts'",
"build": "tsc -p ."
"build": "tsc -p .",
"clean": "rimraf build/"
},
"keywords": [
"plasma",
Expand All @@ -30,15 +31,20 @@
"@types/cors": "^2.8.4",
"@types/express": "^4.16.1",
"body-parser": "^1.18.3",
"cors": "^2.8.5"
"cors": "^2.8.5",
"rimraf": "^2.6.3",
"chai": "^4.2.0"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@types/node": "^11.13.7",
"@pigi/core": "^0.0.1-alpha.1",
"debug": "^4.1.1",
"express": "^4.16.4",
"level": "^5.0.1"
"level": "^5.0.1",
"bn.js": "^4.11.8",
"abstract-leveldown": "^6.0.3"
}
}
68 changes: 45 additions & 23 deletions packages/operator/src/app/db/range-db.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* External Imports */
import level from 'level'
import BigNum = require('bn.js')
import {
Batch
} from '@pigi/core'
import { Batch } from '@pigi/core'

/* Internal Imports */
import { itNext, itEnd, bufferUtils } from '../utils'
import { RangeStore, RangeEntry, Endianness } from '../../interfaces/db/range-db.interface'
import {
RangeStore,
RangeEntry,
Endianness,
} from '../../interfaces/db/range-db.interface'

/* Logging */
import debug from 'debug'
Expand All @@ -17,19 +19,18 @@ const log = debug('info:range-db')
* A RangeStore which uses Level as a backend.
*/
export class LevelRangeStore implements RangeStore {

/**
* Creates the LevelRangeStore.
* @param db Pointer to the Level instance to be used.
* @param prefix A Buffer which is prepended to each range key.
* @param keyLength The number of bytes which should be used for the range keys.
* @param endianness The endianness of the range keys.
*/
constructor (
constructor(
readonly db: level,
readonly prefix: Buffer,
readonly keyLength: number=16,
readonly endianness: Endianness='be'
readonly keyLength: number = 16,
readonly endianness: Endianness = 'be'
) {}

/**
Expand All @@ -50,7 +51,10 @@ export class LevelRangeStore implements RangeStore {
* @returns resulting concatenation of the start key & input value
*/
private addStartToValue(start: BigNum, value: Buffer): Buffer {
return Buffer.concat([start.toBuffer(this.endianness, this.keyLength), value])
return Buffer.concat([
start.toBuffer(this.endianness, this.keyLength),
value,
])
}

/**
Expand Down Expand Up @@ -121,7 +125,12 @@ export class LevelRangeStore implements RangeStore {
* @param end2 The end of the second range.
* @returns true if max(start1, start2) < min(end1, end2), false otherwise.
*/
private intersects(start1: Buffer, end1: Buffer, start2: Buffer, end2: Buffer): boolean {
private intersects(
start1: Buffer,
end1: Buffer,
start2: Buffer,
end2: Buffer
): boolean {
const maxStart = bufferUtils.max(start1, start2)
const minEnd = bufferUtils.min(end1, end2)
return bufferUtils.lt(maxStart, minEnd)
Expand All @@ -137,7 +146,7 @@ export class LevelRangeStore implements RangeStore {
return {
start: new BigNum(this.getStartFromValue(result.value)),
end: new BigNum(result.key.slice(this.prefix.length)),
value: this.getDataFromValue(result.value)
value: this.getDataFromValue(result.value),
}
}

Expand All @@ -148,17 +157,20 @@ export class LevelRangeStore implements RangeStore {
* @param end The end of the range which we want deletion batch operations for.
* @returns an object which contains both the ranges we queried & the batch deletion operations.
*/
private async delBatchOps(start: BigNum, end: BigNum): Promise<{ranges: RangeEntry[]; batchOps: Batch[]}> {
private async delBatchOps(
start: BigNum,
end: BigNum
): Promise<{ ranges: RangeEntry[]; batchOps: Batch[] }> {
this.validateRange(start, end)
const ranges = await this.get(start, end)
const batchOps = []
for (const range of ranges) {
batchOps.push({
type: 'del',
key: this.bnToKey(range.end)
key: this.bnToKey(range.end),
})
}
return {ranges, batchOps}
return { ranges, batchOps }
}

/**
Expand All @@ -171,11 +183,18 @@ export class LevelRangeStore implements RangeStore {
*/
public async put(start: BigNum, end: BigNum, value: Buffer): Promise<void> {
this.validateRange(start, end)
log('Putting range: [', start.toString(16), ',', end.toString(16), ') with value:', value)
log(
'Putting range: [',
start.toString(16),
',',
end.toString(16),
') with value:',
value
)

// Step #1: get all overlapping ranges and queue them for deletion
//
const {ranges, batchOps} = await this.delBatchOps(start, end)
const { ranges, batchOps } = await this.delBatchOps(start, end)

// Step #2: Add back ranges which are split
//
Expand All @@ -185,15 +204,15 @@ export class LevelRangeStore implements RangeStore {
batchOps.push({
type: 'put',
key: this.bnToKey(start),
value: this.addStartToValue(ranges[0].start, ranges[0].value)
value: this.addStartToValue(ranges[0].start, ranges[0].value),
})
}
// If the end position less than the last range's end...
if (ranges.length > 0 && ranges[ranges.length - 1].end.gt(end)) {
batchOps.push({
type: 'put',
key: this.bnToKey(ranges[ranges.length - 1].end),
value: this.addStartToValue(end, ranges[ranges.length - 1].value)
value: this.addStartToValue(end, ranges[ranges.length - 1].value),
})
}

Expand All @@ -202,7 +221,7 @@ export class LevelRangeStore implements RangeStore {
batchOps.push({
type: 'put',
key: this.bnToKey(end),
value: this.addStartToValue(start, value)
value: this.addStartToValue(start, value),
})

// Step #4: Execute the batch!
Expand All @@ -218,7 +237,7 @@ export class LevelRangeStore implements RangeStore {
*/
public async del(start: BigNum, end: BigNum): Promise<RangeEntry[]> {
// Delete all overlapping ranges and return the values which have been deleted
const {ranges, batchOps} = await this.delBatchOps(start, end)
const { ranges, batchOps } = await this.delBatchOps(start, end)
await this.db.batch(batchOps)
return ranges
}
Expand All @@ -235,7 +254,7 @@ export class LevelRangeStore implements RangeStore {
// Seek to the beginning
const it = this.db.iterator({
gt: this.bnToKey(start),
keyAsBuffer: true
keyAsBuffer: true,
})
const ranges = []
let result = await itNext(it)
Expand All @@ -249,13 +268,16 @@ export class LevelRangeStore implements RangeStore {
const queryEnd = this.bnToKey(end)
let resultStart = this.addPrefix(this.getStartFromValue(result.value))
let resultEnd = result.key
while (this.intersects(queryStart, queryEnd, resultStart, resultEnd) && this.isCorrectPrefix(result.key)) {
while (
this.intersects(queryStart, queryEnd, resultStart, resultEnd) &&
this.isCorrectPrefix(result.key)
) {
// If the query & result intersect, add it to our ranges array
ranges.push(this.resultToRange(result))
// Get the next result
result = await itNext(it)
// Make sure the result returned a value
if (typeof(result.key) === 'undefined') {
if (typeof result.key === 'undefined') {
break
}
// Format the result start & end as buffers with the correct prefix
Expand Down
3 changes: 1 addition & 2 deletions packages/operator/src/app/state-manager/ownership-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import debug from 'debug'
const log = debug('test:info:state-ownership')

export class OwnershipState {
constructor (readonly db: level) {
}
constructor(readonly db: level) {}

public applyTransaction(transaction: Buffer) {
log('Applying transaction:', transaction)
Expand Down
13 changes: 9 additions & 4 deletions packages/operator/src/app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Promisify the it.next(cb) function
export const itNext = (it): Promise<{ key: Buffer, value: Buffer }> => {
export const itNext = (it): Promise<{ key: Buffer; value: Buffer }> => {
return new Promise((resolve, reject) => {
it.next((err, key, value) => {
if (err) {
Expand Down Expand Up @@ -69,7 +69,7 @@ const gte = (buf1: Buffer, buf2: Buffer): boolean => {
* @returns Buffer the larger Buffer
*/
const max = (buf1: Buffer, buf2: Buffer): Buffer => {
return (gte(buf1, buf2)) ? buf1 : buf2
return gte(buf1, buf2) ? buf1 : buf2
}

/**
Expand All @@ -79,12 +79,17 @@ const max = (buf1: Buffer, buf2: Buffer): Buffer => {
* @returns Buffer the smaller Buffer
*/
const min = (buf1: Buffer, buf2: Buffer): Buffer => {
return (lte(buf1, buf2)) ? buf1 : buf2
return lte(buf1, buf2) ? buf1 : buf2
}

/**
* A collection of useful utilities for comparing buffers.
*/
export const bufferUtils = {
lt, lte, gt, gte, max, min
lt,
lte,
gt,
gte,
max,
min,
}
9 changes: 3 additions & 6 deletions packages/operator/src/interfaces/db/range-db.interface.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/* External Imports */
import { AbstractOpenOptions } from 'abstract-leveldown'
import BigNum = require('bn.js')
import {
KeyValueStore,
V
} from '@pigi/core'
import { KeyValueStore, V } from '@pigi/core'
import level from 'level'

/**
* Represents a range of values. Note start & end are big numbers!
*/
export interface RangeEntry {
start: BigNum,
end: BigNum,
start: BigNum
end: BigNum
value: V
}

Expand Down

0 comments on commit 8327614

Please sign in to comment.