Skip to content

Commit

Permalink
blockchain: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Oct 23, 2020
1 parent 74ea480 commit 70f5df7
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 132 deletions.
232 changes: 116 additions & 116 deletions packages/blockchain/src/db/databaseOperation.ts
Original file line number Diff line number Diff line change
@@ -1,143 +1,143 @@
import BN = require('bn.js')
import {
HEADS_KEY,
HEAD_HEADER_KEY,
HEAD_BLOCK_KEY,
tdKey,
headerKey,
bodyKey,
numberToHashKey,
hashToNumberKey,
HEADS_KEY,
HEAD_HEADER_KEY,
HEAD_BLOCK_KEY,
tdKey,
headerKey,
bodyKey,
numberToHashKey,
hashToNumberKey,
} from './dbConstants'

import { CacheMap } from './dbManager'

export enum DatabaseOperationTarget {
Heads,
HeadHeader,
HeadBlock,
HashToNumber,
NumberToHash,
TotalDifficulty,
Body,
Header
}
/**
* @hidden
*/
Heads,
HeadHeader,
HeadBlock,
HashToNumber,
NumberToHash,
TotalDifficulty,
Body,
Header,
}

/**
* @hidden
*/
export interface DBOp {
type?: String
key: Buffer | string
keyEncoding: String
valueEncoding?: String
value?: Buffer | object
type?: String
key: Buffer | string
keyEncoding: String
valueEncoding?: String
value?: Buffer | object
}

// a Database Key is identified by a block hash, a block number, or both
export type DatabaseKey = {
blockNumber?: BN,
blockHash?: Buffer
blockNumber?: BN
blockHash?: Buffer
}





export class DatabaseOperation {
public operationTarget: DatabaseOperationTarget
public baseDBOp: DBOp
public cacheString: string | undefined

public operationTarget: DatabaseOperationTarget
public baseDBOp: DBOp
public cacheString: string | undefined

private constructor(operationTarget: DatabaseOperationTarget, key?: DatabaseKey) {

this.operationTarget = operationTarget
private constructor(operationTarget: DatabaseOperationTarget, key?: DatabaseKey) {
this.operationTarget = operationTarget

this.baseDBOp = {
key: '',
keyEncoding: 'binary',
}

switch(operationTarget) {
case DatabaseOperationTarget.Heads: {
this.baseDBOp.key = HEADS_KEY
break
}
case DatabaseOperationTarget.HeadHeader: {
this.baseDBOp.key = HEAD_HEADER_KEY
break
}
case DatabaseOperationTarget.HeadBlock: {
this.baseDBOp.key = HEAD_BLOCK_KEY
break
}
case DatabaseOperationTarget.HashToNumber: {
this.baseDBOp.key = hashToNumberKey(key!.blockHash!)
this.cacheString = "hashToNumber"
break
}
case DatabaseOperationTarget.NumberToHash: {
this.baseDBOp.key = numberToHashKey(key!.blockNumber!)
this.cacheString = "numberToHash"
break
}
case DatabaseOperationTarget.TotalDifficulty: {
this.baseDBOp.key = tdKey(key!.blockNumber!, key!.blockHash!)
this.cacheString = "td"
break
}
case DatabaseOperationTarget.Body: {
this.baseDBOp.key = bodyKey(key!.blockNumber!, key!.blockHash!)
this.cacheString = "body"
break
}
case DatabaseOperationTarget.Header: {
this.baseDBOp.key = headerKey(key!.blockNumber!, key!.blockHash!)
this.cacheString = "header"
break
}
}
this.baseDBOp = {
key: '',
keyEncoding: 'binary',
}

public static get(operationTarget: DatabaseOperationTarget, key?: DatabaseKey): DatabaseOperation {
return new DatabaseOperation(operationTarget, key)
switch (operationTarget) {
case DatabaseOperationTarget.Heads: {
this.baseDBOp.key = HEADS_KEY
break
}
case DatabaseOperationTarget.HeadHeader: {
this.baseDBOp.key = HEAD_HEADER_KEY
break
}
case DatabaseOperationTarget.HeadBlock: {
this.baseDBOp.key = HEAD_BLOCK_KEY
break
}
case DatabaseOperationTarget.HashToNumber: {
this.baseDBOp.key = hashToNumberKey(key!.blockHash!)
this.cacheString = 'hashToNumber'
break
}
case DatabaseOperationTarget.NumberToHash: {
this.baseDBOp.key = numberToHashKey(key!.blockNumber!)
this.cacheString = 'numberToHash'
break
}
case DatabaseOperationTarget.TotalDifficulty: {
this.baseDBOp.key = tdKey(key!.blockNumber!, key!.blockHash!)
this.cacheString = 'td'
break
}
case DatabaseOperationTarget.Body: {
this.baseDBOp.key = bodyKey(key!.blockNumber!, key!.blockHash!)
this.cacheString = 'body'
break
}
case DatabaseOperationTarget.Header: {
this.baseDBOp.key = headerKey(key!.blockNumber!, key!.blockHash!)
this.cacheString = 'header'
break
}
}
}

// set operation: note: value/key is not in default order
public static set(operationTarget: DatabaseOperationTarget, value: Buffer | object, key?: DatabaseKey): DatabaseOperation {
const databaseOperation = new DatabaseOperation(operationTarget, key)
databaseOperation.baseDBOp.value = value
databaseOperation.baseDBOp.type = 'put'

if (operationTarget == DatabaseOperationTarget.Heads) {
databaseOperation.baseDBOp.valueEncoding = 'json'
} else {
databaseOperation.baseDBOp.valueEncoding = 'binary'
}
public static get(
operationTarget: DatabaseOperationTarget,
key?: DatabaseKey
): DatabaseOperation {
return new DatabaseOperation(operationTarget, key)
}

return databaseOperation
// set operation: note: value/key is not in default order
public static set(
operationTarget: DatabaseOperationTarget,
value: Buffer | object,
key?: DatabaseKey
): DatabaseOperation {
const databaseOperation = new DatabaseOperation(operationTarget, key)
databaseOperation.baseDBOp.value = value
databaseOperation.baseDBOp.type = 'put'

if (operationTarget == DatabaseOperationTarget.Heads) {
databaseOperation.baseDBOp.valueEncoding = 'json'
} else {
databaseOperation.baseDBOp.valueEncoding = 'binary'
}

public static del(operationTarget: DatabaseOperationTarget, key?: DatabaseKey): DatabaseOperation {
const databaseOperation = new DatabaseOperation(operationTarget, key)
databaseOperation.baseDBOp.type = 'del'
return databaseOperation
}
return databaseOperation
}

public static del(
operationTarget: DatabaseOperationTarget,
key?: DatabaseKey
): DatabaseOperation {
const databaseOperation = new DatabaseOperation(operationTarget, key)
databaseOperation.baseDBOp.type = 'del'
return databaseOperation
}

public updateCache(cacheMap: CacheMap) {
if (this.cacheString && cacheMap[this.cacheString] && Buffer.isBuffer(this.baseDBOp.value)) {
if (this.baseDBOp.type == 'put') {
cacheMap[this.cacheString].set(this.baseDBOp.key, this.baseDBOp.value)
} else if (this.baseDBOp.type == 'del') {
cacheMap[this.cacheString].del(this.baseDBOp.key)
} else {
throw(new Error("unsupported db operation on cache"))
}
}
public updateCache(cacheMap: CacheMap) {
if (this.cacheString && cacheMap[this.cacheString] && Buffer.isBuffer(this.baseDBOp.value)) {
if (this.baseDBOp.type == 'put') {
cacheMap[this.cacheString].set(this.baseDBOp.key, this.baseDBOp.value)
} else if (this.baseDBOp.type == 'del') {
cacheMap[this.cacheString].del(this.baseDBOp.key)
} else {
throw new Error('unsupported db operation on cache')
}
}



}
}
}
1 change: 0 additions & 1 deletion packages/blockchain/src/db/dbManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export class DBManager {
async get(databaseOperationTarget: DatabaseOperationTarget, key?: DatabaseKey): Promise<any> {
const databaseGetOperation = DatabaseOperation.get(databaseOperationTarget, key)


const cacheString = databaseGetOperation.cacheString
const dbKey = databaseGetOperation.baseDBOp.key
const dbOpts = databaseGetOperation.baseDBOp
Expand Down
53 changes: 38 additions & 15 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DBManager } from './db/dbManager'
import { DatabaseOperationTarget, DatabaseOperation } from './db/databaseOperation'
import { bufBE8 } from './db/dbConstants'


import type { LevelUp } from 'levelup'

const level = require('level-mem')
Expand Down Expand Up @@ -385,23 +384,30 @@ export default class Blockchain implements BlockchainInterface {
}

const rebuildInfo = async () => {
const type = 'put'
const keyEncoding = 'binary'
const valueEncoding = 'binary'

// save block and total difficulty to the database
const TDValue = rlp.encode(td)
dbOps.push(DatabaseOperation.set(DatabaseOperationTarget.TotalDifficulty, TDValue, { blockNumber, blockHash }))
dbOps.push(
DatabaseOperation.set(DatabaseOperationTarget.TotalDifficulty, TDValue, {
blockNumber,
blockHash,
})
)

// save header

const headerValue = header.serialize()
dbOps.push(DatabaseOperation.set(DatabaseOperationTarget.Header, headerValue, { blockNumber, blockHash }))
dbOps.push(
DatabaseOperation.set(DatabaseOperationTarget.Header, headerValue, {
blockNumber,
blockHash,
})
)

// store body if it exists
if (isGenesis || block.transactions.length || block.uncleHeaders.length) {
const bodyValue = rlp.encode(block.raw().slice(1))
dbOps.push(DatabaseOperation.set(DatabaseOperationTarget.Body, bodyValue, { blockNumber, blockHash }))
dbOps.push(
DatabaseOperation.set(DatabaseOperationTarget.Body, bodyValue, { blockNumber, blockHash })
)
}

// if total difficulty is higher than current, add it to canonical chain
Expand All @@ -423,7 +429,11 @@ export default class Blockchain implements BlockchainInterface {
}
// save hash to number lookup info even if rebuild not needed
const blockNumber8Byte = bufBE8(blockNumber)
dbOps.push(DatabaseOperation.set(DatabaseOperationTarget.HashToNumber, blockNumber8Byte, { blockHash }))
dbOps.push(
DatabaseOperation.set(DatabaseOperationTarget.HashToNumber, blockNumber8Byte, {
blockHash,
})
)
}
}

Expand Down Expand Up @@ -534,7 +544,7 @@ export default class Blockchain implements BlockchainInterface {
return [
DatabaseOperation.set(DatabaseOperationTarget.Heads, this._heads),
DatabaseOperation.set(DatabaseOperationTarget.HeadHeader, this._headHeader!),
DatabaseOperation.set(DatabaseOperationTarget.HeadBlock, this._headBlock!)
DatabaseOperation.set(DatabaseOperationTarget.HeadBlock, this._headBlock!),
]
}

Expand Down Expand Up @@ -588,10 +598,16 @@ export default class Blockchain implements BlockchainInterface {
const blockNumber = header.number

const saveLookups = async (hash: Buffer, number: BN) => {
ops.push(DatabaseOperation.set(DatabaseOperationTarget.NumberToHash, blockHash, { blockNumber }))
ops.push(
DatabaseOperation.set(DatabaseOperationTarget.NumberToHash, blockHash, { blockNumber })
)

const blockNumber8Bytes = bufBE8(number)
ops.push(DatabaseOperation.set(DatabaseOperationTarget.HashToNumber, blockNumber8Bytes, { blockHash }))
ops.push(
DatabaseOperation.set(DatabaseOperationTarget.HashToNumber, blockNumber8Bytes, {
blockHash,
})
)
}

// handle genesis block
Expand Down Expand Up @@ -706,12 +722,19 @@ export default class Blockchain implements BlockchainInterface {
/**
* @hidden
*/
async _delChild(blockHash: Buffer, blockNumber: BN, headHash: Buffer | null, ops: DatabaseOperation[]) {
async _delChild(
blockHash: Buffer,
blockNumber: BN,
headHash: Buffer | null,
ops: DatabaseOperation[]
) {
// delete header, body, hash to number mapping and td
ops.push(DatabaseOperation.del(DatabaseOperationTarget.Header, { blockHash, blockNumber }))
ops.push(DatabaseOperation.del(DatabaseOperationTarget.Body, { blockHash, blockNumber }))
ops.push(DatabaseOperation.del(DatabaseOperationTarget.HashToNumber, { blockHash }))
ops.push(DatabaseOperation.del(DatabaseOperationTarget.TotalDifficulty, { blockHash, blockNumber }))
ops.push(
DatabaseOperation.del(DatabaseOperationTarget.TotalDifficulty, { blockHash, blockNumber })
)

if (!headHash) {
return
Expand Down

0 comments on commit 70f5df7

Please sign in to comment.