Skip to content

Commit

Permalink
One BigNumber to Rule Them All (#357)
Browse files Browse the repository at this point in the history
Adding BigNumber to wrap big number implementation and updating ALL references
  • Loading branch information
willmeister committed Jul 24, 2019
1 parent 071efde commit 56a933a
Show file tree
Hide file tree
Showing 42 changed files with 1,014 additions and 318 deletions.
2 changes: 0 additions & 2 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
},
"devDependencies": {
"@pigi/prettier-config": "^0.0.2-alpha.2",
"@types/bn.js": "^4.11.4",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"@types/node": "^11.11.3",
Expand All @@ -45,7 +44,6 @@
"dependencies": {
"@pigi/core": "^0.0.1-alpha.1",
"@types/sinon-chai": "^3.2.2",
"bn.js": "^4.11.8",
"chai": "^4.2.0",
"debug": "^4.1.1",
"ethereum-waffle": "^2.0.12",
Expand Down
42 changes: 21 additions & 21 deletions packages/contracts/test/Deposit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
hexStringify,
AbiOwnershipBody,
AbiOwnershipTransaction,
BigNumber,
} from '@pigi/core'
import BigNum = require('bn.js')
/* Contract Imports */
import { createMockProvider, deployContract, getWallets } from 'ethereum-waffle'
import * as BasicTokenMock from '../build/BasicTokenMock.json'
Expand Down Expand Up @@ -91,8 +91,8 @@ describe('Deposit Contract with Ownership', () => {
await depositContract.deposit(100, depositStateObject)
// Attempt to start an exit on this deposit
const depositRange = {
start: hexStringify(new BigNum(0)),
end: hexStringify(new BigNum(100)),
start: hexStringify(new BigNumber(0)),
end: hexStringify(new BigNumber(100)),
}
await ownershipPredicate.startExitByOwner({
stateUpdate: {
Expand Down Expand Up @@ -135,8 +135,8 @@ describe('Deposit Contract with Ownership', () => {
await depositContract.deposit(100, depositStateObject)
// Attempt to start a checkpoint on a stateUpdate
const stateUpdateRange = {
start: hexStringify(new BigNum(10)),
end: hexStringify(new BigNum(20)),
start: hexStringify(new BigNumber(10)),
end: hexStringify(new BigNumber(20)),
}
const checkpoint = {
stateUpdate: {
Expand Down Expand Up @@ -165,8 +165,8 @@ describe('Deposit Contract with Ownership', () => {
await depositContract.deposit(100, depositStateObject)
// Attempt to start an exit on this deposit
const depositRange = {
start: hexStringify(new BigNum(0)),
end: hexStringify(new BigNum(100)),
start: hexStringify(new BigNumber(0)),
end: hexStringify(new BigNumber(100)),
}
const checkpoint = {
stateUpdate: {
Expand All @@ -181,11 +181,11 @@ describe('Deposit Contract with Ownership', () => {
// Now deprecate the exit
const txBody = new AbiOwnershipBody(
depositStateObject,
new BigNum(0),
new BigNum(9)
new BigNumber(0),
new BigNumber(9)
)
const txDepositContract = depositContract.address
const txRange = new AbiRange(new BigNum(10), new BigNum(30))
const txRange = new AbiRange(new BigNumber(10), new BigNumber(30))
const transaction = new AbiOwnershipTransaction(
txDepositContract,
txRange,
Expand Down Expand Up @@ -214,8 +214,8 @@ describe('Deposit Contract with Ownership', () => {
await depositContract.deposit(100, depositStateObject)
// Add a later checkpoint
const stateUpdateRange = {
start: hexStringify(new BigNum(10)),
end: hexStringify(new BigNum(20)),
start: hexStringify(new BigNumber(10)),
end: hexStringify(new BigNumber(20)),
}
const checkpoint = {
stateUpdate: {
Expand All @@ -234,8 +234,8 @@ describe('Deposit Contract with Ownership', () => {
await mineBlocks(provider, challengePeroid + 1)
// Now that we have a finalized checkpoint, attempt an exit on the original deposit
const depositRange = {
start: hexStringify(new BigNum(0)),
end: hexStringify(new BigNum(100)),
start: hexStringify(new BigNumber(0)),
end: hexStringify(new BigNumber(100)),
}
const depositCheckpoint = {
stateUpdate: {
Expand Down Expand Up @@ -264,8 +264,8 @@ describe('Deposit Contract with Ownership', () => {
await depositContract.deposit(100, depositStateObject)
// Add a later checkpoint
const stateUpdateRange = {
start: hexStringify(new BigNum(10)),
end: hexStringify(new BigNum(20)),
start: hexStringify(new BigNumber(10)),
end: hexStringify(new BigNumber(20)),
}
const checkpoint = {
stateUpdate: {
Expand All @@ -280,8 +280,8 @@ describe('Deposit Contract with Ownership', () => {
await ownershipPredicate.startExitByOwner(checkpoint)
// Now we use the deposit to challenge this exit
const depositRange = {
start: hexStringify(new BigNum(0)),
end: hexStringify(new BigNum(100)),
start: hexStringify(new BigNumber(0)),
end: hexStringify(new BigNumber(100)),
}
const depositCheckpoint = {
stateUpdate: {
Expand All @@ -302,11 +302,11 @@ describe('Deposit Contract with Ownership', () => {
// Deprecate the exit so we can remove the challenge
const txBody = new AbiOwnershipBody(
checkpoint.stateUpdate.stateObject,
new BigNum(0),
new BigNum(10)
new BigNumber(0),
new BigNumber(10)
)
const txDepositContract = depositContract.address
const txRange = new AbiRange(new BigNum(10), new BigNum(30))
const txRange = new AbiRange(new BigNumber(10), new BigNumber(30))
const transaction = new AbiOwnershipTransaction(
txDepositContract,
txRange,
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/app/aggregator/aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import BigNum = require('bn.js')

import { Aggregator } from '../../types/aggregator'
import { StateManager } from '../../types/ovm'
import {
Expand All @@ -8,7 +6,7 @@ import {
Transaction,
TransactionResult,
} from '../../types/serialization'
import { doRangesSpanRange, sign } from '../utils'
import { BigNumber, doRangesSpanRange, sign } from '../utils'
import { BlockManager } from '../../types/block-production'

export class DefaultAggregator implements Aggregator {
Expand All @@ -25,7 +23,7 @@ export class DefaultAggregator implements Aggregator {
public async ingestTransaction(
transaction: Transaction
): Promise<BlockTransactionCommitment> {
const blockNumber: BigNum = await this.blockManager.getNextBlockNumber()
const blockNumber: BigNumber = await this.blockManager.getNextBlockNumber()

const {
stateUpdate,
Expand Down
23 changes: 13 additions & 10 deletions packages/core/src/app/block-production/block-db.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* External Imports */
import BigNum = require('bn.js')
import { Mutex } from 'async-mutex'

import { BaseKey, BaseRangeBucket } from '../db'
import { BlockDB } from '../../types/block-production'
import { KeyValueStore, RangeStore } from '../../types/db'
import { StateUpdate } from '../../types/serialization'
import { MAX_BIG_NUM, ONE, ZERO } from '../utils'
import { BIG_ENDIAN, BigNumber, MAX_BIG_NUM, ONE, ZERO } from '../utils'
import { GenericMerkleIntervalTree } from './merkle-interval-tree'
import { deserializeStateUpdate, serializeStateUpdate } from '../serialization'

Expand Down Expand Up @@ -36,10 +35,10 @@ export class DefaultBlockDB implements BlockDB {
/**
* @returns the next plasma block number.
*/
public async getNextBlockNumber(): Promise<BigNum> {
public async getNextBlockNumber(): Promise<BigNumber> {
// TODO: Cache this when it makes sense
const buf = await this.vars.get(KEYS.NEXT_BLOCK)
return !buf ? ONE : new BigNum(buf, 'be')
return !buf ? ONE : new BigNumber(buf, 'hex', BIG_ENDIAN)
}

/**
Expand Down Expand Up @@ -79,7 +78,7 @@ export class DefaultBlockDB implements BlockDB {
* @param blockNumber Block to compute a root for.
* @returns the root of the block.
*/
public async getMerkleRoot(blockNumber: BigNum): Promise<Buffer> {
public async getMerkleRoot(blockNumber: BigNumber): Promise<Buffer> {
const stateUpdates = await this.getStateUpdates(blockNumber)

const leaves = stateUpdates.map((stateUpdate) => {
Expand All @@ -104,8 +103,10 @@ export class DefaultBlockDB implements BlockDB {
*/
public async finalizeNextBlock(): Promise<void> {
await this.blockMutex.runExclusive(async () => {
const prevBlockNumber: BigNum = await this.getNextBlockNumber()
const nextBlockNumber: Buffer = prevBlockNumber.add(ONE).toBuffer('be')
const prevBlockNumber: BigNumber = await this.getNextBlockNumber()
const nextBlockNumber: Buffer = prevBlockNumber
.add(ONE)
.toBuffer(BIG_ENDIAN)

await this.vars.put(KEYS.NEXT_BLOCK, nextBlockNumber)
})
Expand All @@ -116,8 +117,8 @@ export class DefaultBlockDB implements BlockDB {
* @param blockNumber Block to open the RangeDB for.
* @returns the RangeDB instance for the given block.
*/
private async getBlockStore(blockNumber: BigNum): Promise<RangeStore> {
const key = KEYS.BLOCK.encode([blockNumber.toBuffer('be')])
private async getBlockStore(blockNumber: BigNumber): Promise<RangeStore> {
const key = KEYS.BLOCK.encode([blockNumber.toBuffer(BIG_ENDIAN)])
const bucket = this.blocks.bucket(key)
return new BaseRangeBucket(bucket.db, bucket.prefix)
}
Expand All @@ -141,7 +142,9 @@ export class DefaultBlockDB implements BlockDB {
* @param blockNumber Block to query state updates for.
* @returns the list of state updates for that block.
*/
private async getStateUpdates(blockNumber: BigNum): Promise<StateUpdate[]> {
private async getStateUpdates(
blockNumber: BigNumber
): Promise<StateUpdate[]> {
const block = await this.getBlockStore(blockNumber)
const values = await block.get(ZERO, MAX_BIG_NUM)
return values.map((value) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/app/block-production/block-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import BigNum = require('bn.js')
import { Mutex } from 'async-mutex'

import {
Expand All @@ -7,6 +6,7 @@ import {
CommitmentContract,
} from '../../types/block-production'
import { StateUpdate } from '../../types/serialization'
import { BigNumber } from '../utils'

/**
* Simple BlockManager implementation.
Expand All @@ -29,7 +29,7 @@ export class DefaultBlockManager implements BlockManager {
/**
* @returns the next plasma block number.
*/
public async getNextBlockNumber(): Promise<BigNum> {
public async getNextBlockNumber(): Promise<BigNumber> {
return this.blockdb.getNextBlockNumber()
}

Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/app/block-production/merkle-interval-tree.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* External Imports */
import BigNum = require('bn.js')
import debug from 'debug'
const log = debug('info:merkle-interval-tree')

/* Internal Imports */
import { reverse, keccak256 } from '../'
import { reverse, keccak256, BigNumber, ZERO } from '../'
import {
MerkleIntervalTreeNode,
MerkleIntervalTree,
Expand Down Expand Up @@ -183,7 +182,7 @@ export class GenericMerkleIntervalTree implements MerkleIntervalTree {
}
return {
siblings: inclusionProof,
leafPosition: new BigNum(leafPosition),
leafPosition: new BigNumber(leafPosition),
}
}

Expand Down Expand Up @@ -216,13 +215,13 @@ export class GenericMerkleIntervalTree implements MerkleIntervalTree {
leafNode: GenericMerkleIntervalTreeNode,
inclusionProof: MerkleIntervalInclusionProof
): MerkleIntervalProofOutput {
if (inclusionProof.leafPosition.lt(new BigNum(0))) {
if (inclusionProof.leafPosition.lt(ZERO)) {
throw new Error('Invalid leaf position.')
}

// Compute the path based on the leaf index.
const path = reverse(
new BigNum(inclusionProof.leafPosition).toString(
new BigNumber(inclusionProof.leafPosition).toString(
2,
inclusionProof.siblings.length
)
Expand Down Expand Up @@ -271,7 +270,7 @@ export class GenericMerkleIntervalTree implements MerkleIntervalTree {
.lowerBound // messy way to get the max index, TODO clean
return {
root: computed,
upperBound: new BigNum(implicitEnd),
upperBound: new BigNumber(implicitEnd),
}
}
}
4 changes: 2 additions & 2 deletions packages/core/src/app/block-production/state-interval-tree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GenericMerkleIntervalTree, GenericMerkleIntervalTreeNode } from './'
import { AbiStateUpdate, STATE_ID_LENGTH } from '../'
import { AbiStateUpdate, BIG_ENDIAN, STATE_ID_LENGTH } from '../'
import {
MerkleIntervalTreeNode,
MerkleIntervalInclusionProof,
Expand All @@ -20,7 +20,7 @@ export class MerkleStateIntervalTree extends GenericMerkleIntervalTree {
const hash = GenericMerkleIntervalTree.hash(
Buffer.from(stateUpdate.encoded)
)
const index = stateUpdate.range.start.toBuffer('be', STATE_ID_LENGTH)
const index = stateUpdate.range.start.toBuffer(BIG_ENDIAN, STATE_ID_LENGTH)
return new GenericMerkleIntervalTreeNode(hash, index)
}

Expand Down

0 comments on commit 56a933a

Please sign in to comment.