Skip to content

Commit

Permalink
save transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
promentol committed Apr 8, 2018
1 parent fb053e5 commit f1c91ea
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
.vscode
node_modules
data
asd
src/**/*.js
src/**/*.js.map
test/**/*.js
test/**/*.js.map
TestResults
dist
data
10 changes: 8 additions & 2 deletions src/core/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export namespace Actions {
}
export const processTransaction = (tx: Transaction) => {
//verify transaction
return Persistence.Instance.addTransactionToPool(tx)
Persistence.Instance.addTransactionToPool(tx)
return Persistence.Instance.saveTransaction(tx)
}

export const processBlock = (block: Block): Observable<Block> => {
Expand All @@ -102,7 +103,12 @@ export namespace Actions {
}).do(()=>{
const hashes = block.transactions.map((x)=>Crypto.hashTransaction(x))
return Persistence.Instance.eraseTransactionToPool(hashes);
})
}).switchMap((x)=>{
return Observable.from(block.transactions);
}).mergeMap((tx)=>{
console.log('tx', tx, Crypto.hashTransaction(tx))
return Persistence.Instance.saveTransaction(tx)
}).map(()=>block)
/*
return Persistence.Instance.lastBlock.map((lastBlock)=>{
if()
Expand Down
7 changes: 5 additions & 2 deletions src/core/Persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Crypto
} from './Crypto'

import _ from 'lodash'
import * as _ from 'lodash'

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
Expand Down Expand Up @@ -95,7 +95,10 @@ export class Persistence {
public saveTransaction: (tx: Transaction) => Observable<boolean> = Observable.bindNodeCallback((
tx: Transaction,
callback: (error: Error, value: boolean) => void
) => this._db.put(`t-${Crypto.hashTransaction(tx)}`, JSON.stringify(tx), (err, buffer) => callback(null, !err && !!buffer)))
) => this._db.put(`t-${Crypto.hashTransaction(tx)}`, JSON.stringify(tx), (err, buffer) => {
console.log('err', err)
callback(null, !err && !!buffer)
}))

public writeBlock: (block: Block) => Observable<boolean> = Observable.bindNodeCallback((
block: Block,
Expand Down
8 changes: 4 additions & 4 deletions src/network/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export class NCoinServerConnection extends NCoinConnection {
input.on("data", (data) => {
this.messages.next(data)
})
input.on("error", (e) => {
this.socket.on("error", (e) => {
console.log(e)
input.destroy()
this.messages.complete()
})
input.on("close", (data) => {
this.socket.on("close", (data) => {
this.messages.complete()
})

Expand Down Expand Up @@ -68,12 +68,12 @@ export class NCoinClientConnection extends NCoinConnection {
input.on("data", (data) => {
this.messages.next(data)
})
input.on("error", (e) => {
this.client.on("error", (e) => {
input.destroy()
console.log(e)
this.messages.complete()
})
input.on("close", () => {
this.client.on("close", () => {
this.messages.complete()
})
this.writeStream.pipe(this.client)
Expand Down
12 changes: 5 additions & 7 deletions src/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ export function initREST(port) {
})

app.get('/blocks/:hash', function (req, res) {
Actions.getBlock(req.params.hash).subscribe((blocksCount) => {
res.send({
blocksCount
})
})
Actions.getBlock(req.params.hash).toPromise().then((block) => {
res.send(block)
}).catch(e => res.status(400).send('NOT EXISTS'))
})

app.get('/transactions/:hash', function (req, res) {
Actions.getTransaction(req.params.hash).subscribe((tx) => {
Actions.getTransaction(req.params.hash).toPromise().then((tx) => {
res.send(tx)
})
}).catch(e => res.status(400).send('NOT EXISTS'))
})

app.get('/:address/nonce', function (req, res) {
Expand Down
12 changes: 8 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import {
Persistence,
BlockHeader,
Actions,
Crypto,
encodeBlockHeader,
encodeTransaction,
} from '../src/core'
/*

var levelup = require('levelup')
var leveldown = require('leveldown')

// 1) Create our store
var db = levelup(leveldown('./data'))


/*
Persistence.Instance.setDB(db).subscribe(() => {
Actions.getBlockUntill('6319dabd2040e9f24f7fb09876c5e4b9797ad96661f5b3be8275b8c2c38707dd').subscribe((x) => {
console.log(x)
Expand Down Expand Up @@ -89,7 +89,6 @@ var valid = validate({
]
});
if (!valid) console.log(validate.errors);
*/
import * as ndjson from 'ndjson'
Expand All @@ -105,4 +104,9 @@ ls.on('error', function (err) {
console.log(err);
});
ls.write('{ "foo" : "bar" }\n n { "foo" : "baz" }\n');
ls.write('{ "foo" : "bar" }\n n { "foo" : "baz" }\n');
console.log(Crypto.hashTransaction(
{ "data": { "to": "A99x/geiJvPrh1vp/kTIH9qdtTgGAIJXmOhF57zVWQuO", "type": 0, "amount": 100, "payload": "payload", "nonce": 0 } }
))*/

10 changes: 2 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist"
"watch": true
},
"exclude": [
"dist",
"node_modules"
],
"files": [
"src/index.ts",
]
}

0 comments on commit f1c91ea

Please sign in to comment.