Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Merge branch 'judd/persistence' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
keppel committed Jan 3, 2019
2 parents 4dbfb42 + 15ad14f commit 1ed107a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
42 changes: 37 additions & 5 deletions src/abci-server.ts
Expand Up @@ -2,17 +2,43 @@ import djson = require('deterministic-json')
import vstruct = require('varstruct')

let createServer = require('abci')
let { createHash } = require('crypto')
let fs = require('fs-extra')
let { join } = require('path')

export interface ABCIServer {
listen(port)
}

export default function createABCIServer(stateMachine, initialState): any {
export default function createABCIServer(
stateMachine,
initialState,
lotionAppHome
): any {
let stateFilePath = join(lotionAppHome, 'state.json')
let height = 0
let abciServer = createServer({
info(request) {
return {}
return new Promise(async (resolve, reject) => {
await fs.ensureFile(stateFilePath)
try {
let stateFile = await fs.readJson(stateFilePath, 'utf8')
let rootHash = createHash('sha256')
.update(djson.stringify(stateFile.state))
.digest()

stateMachine.initialize(stateFile.state, stateFile.context)
height = stateFile.height
resolve({
lastBlockAppHash: rootHash,
lastBlockHeight: stateFile.height
})
} catch (e) {
resolve({})
}
})
},

deliverTx(request) {
try {
let tx = decodeTx(request.tx)
Expand Down Expand Up @@ -41,7 +67,6 @@ export default function createABCIServer(stateMachine, initialState): any {
},
beginBlock(request) {
let time = request.header.time.seconds.toNumber()

stateMachine.transition({ type: 'begin-block', data: { time } })
return {}
},
Expand All @@ -62,8 +87,15 @@ export default function createABCIServer(stateMachine, initialState): any {
}
},
commit() {
let data = stateMachine.commit()
return { data: Buffer.from(data, 'hex') }
return new Promise(async (resolve, reject) => {
let data = stateMachine.commit()
await fs.writeJson(stateFilePath, {
state: stateMachine.query(),
height: height,
context: stateMachine.context()
})
resolve({ data: Buffer.from(data, 'hex') })
})
},
initChain(request) {
/**
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Expand Up @@ -4,7 +4,7 @@ import buildApplication, {
Application
} from 'lotion-state-machine'

import { join, resolve } from 'path'
import { join } from 'path'
import { homedir } from 'os'
import createABCIServer, { ABCIServer } from './abci-server'
import createTendermintProcess from './tendermint'
Expand Down Expand Up @@ -109,8 +109,8 @@ class LotionApp implements Application {
this.home = join(
this.lotionHome,
createHash('sha256')
.update(resolve(this.config.genesisPath))
.update(resolve(this.config.keyPath))
.update(fs.readFileSync(this.config.genesisPath))
.update(fs.readFileSync(this.config.keyPath))
.digest('hex')
)
} else {
Expand All @@ -125,7 +125,11 @@ class LotionApp implements Application {
// start state machine
this.stateMachine = this.application.compile()

this.abciServer = createABCIServer(this.stateMachine, this.initialState)
this.abciServer = createABCIServer(
this.stateMachine,
this.initialState,
this.home
)
this.abciServer.listen(this.ports.abci)

// start tendermint process
Expand Down

0 comments on commit 1ed107a

Please sign in to comment.