Skip to content

Commit

Permalink
Gregarious Goose
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenDelahoy committed Aug 3, 2018
1 parent 24b0ea5 commit c154628
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 425 deletions.
7 changes: 4 additions & 3 deletions src/consensus/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DelayQueue } from "../common/delayQueue"
import { ITxPool } from "../common/itxPool"
import { SignedTx } from "../common/txSigned"
import { globalOptions } from "../main"
import { MAX_HEADER_SIZE } from "../network/rabbit/networkConstants"
import { Hash } from "../util/hash"
import { Account } from "./database/account"
import { Database } from "./database/database"
Expand All @@ -21,7 +22,7 @@ import { TxDatabase } from "./database/txDatabase"
import { TxValidity, WorldState } from "./database/worldState"
import { DifficultyAdjuster } from "./difficultyAdjuster"
import { IConsensus, IStatusChange } from "./iconsensus"
import { BlockStatus, MAX_HEADER_SIZE } from "./sync"
import { BlockStatus } from "./sync"
import { Verify } from "./verify"
const logger = getLogger("Consensus")

Expand Down Expand Up @@ -318,7 +319,7 @@ export class Consensus extends EventEmitter implements IConsensus {
+ ` ${hash}(${dbBlock.height}, ${dbBlock.totalWork.toExponential()}),`
+ ` BTip(${this.blockTip.height}, ${this.blockTip.totalWork.toExponential()}),`
+ ` HTip(${this.headerTip.height}, ${this.headerTip.totalWork.toExponential()})`)
return { oldStatus, status }
return { oldStatus, status, height: dbBlock.height }
}

if (block !== undefined && (this.blockTip === undefined || this.forkChoice(dbBlock, this.blockTip))) {
Expand All @@ -332,7 +333,7 @@ export class Consensus extends EventEmitter implements IConsensus {
+ ` BTip(${this.blockTip.height}, ${this.blockTip.totalWork.toExponential()}),`
+ ` HTip(${this.headerTip.height}, ${this.headerTip.totalWork.toExponential()})`)

return { oldStatus, status }
return { oldStatus, status, height: dbBlock.height }
})
}
private async process(hash: Hash, header: BlockHeader, block?: Block): Promise<IPutResult> {
Expand Down
14 changes: 0 additions & 14 deletions src/consensus/database/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import levelup = require("levelup")
import { getLogger } from "log4js"
import rocksdb = require("rocksdb")
import { AsyncLock } from "../../common/asyncLock"
import { AnyBlock, Block } from "../../common/block"
import { GenesisBlock } from "../../common/blockGenesis"
import { Hash } from "../../util/hash"
Expand All @@ -11,15 +10,6 @@ import { DBBlock } from "./dbblock"

const logger = getLogger("Database")

function uint8ArrayEqual(first: Uint8Array, second: Uint8Array): boolean {
if (first.length !== second.length) { return false }
for (let i = 0; i < second.length; i++) {
if (first[i] !== second[i]) {
return false
}
}
return true
}
export class DecodeError extends Error {
public hash: Hash
}
Expand All @@ -29,16 +19,12 @@ export class DecodeError extends Error {
export class Database {
private database: levelup.LevelUp
private blockFile: BlockFile
private headerLock: AsyncLock
private blockLock: AsyncLock
private fileNumber: number

constructor(dbPath: string, filePath: string) {

const rocks: any = rocksdb(dbPath)
this.database = levelup(rocks)
this.headerLock = new AsyncLock()
this.blockLock = new AsyncLock()
this.blockFile = new BlockFile(filePath)
}

Expand Down
3 changes: 3 additions & 0 deletions src/consensus/database/worldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export class WorldState {
}
}
public async validateTx(stateRoot: Hash, tx: SignedTx): Promise<TxValidity> {
if (tx.from.equals(tx.to)) {
return TxValidity.Invalid
}
const fromAccount = await this.getAccount(stateRoot, tx.from)
if (fromAccount === undefined || fromAccount.nonce >= tx.nonce) {
return TxValidity.Invalid
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/iconsensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DBMined } from "./database/dbMined"
import { DBTx } from "./database/dbtx"
import { TxValidity } from "./database/worldState"

export interface IStatusChange { oldStatus?: BlockStatus, status?: BlockStatus }
export interface IStatusChange { oldStatus?: BlockStatus, status?: BlockStatus, height?: number }

export type AnySignedTx = (GenesisSignedTx | SignedTx)

Expand Down
Loading

0 comments on commit c154628

Please sign in to comment.