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

Commit

Permalink
Improve handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
chritchens committed Nov 15, 2018
1 parent e4c5eb5 commit ca15aef
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
67 changes: 63 additions & 4 deletions src/io/network/server/handler/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use mitrid_core::base::Serializable;
use mitrid_core::io::Storable;

use crypto::Hasher;
use model::{UTxO, Transaction, Block, BlockNode, BlockChain};
use io::Store;
use io::network::{Message, Request, Response};
use io::network::message::request::transaction::*;
Expand All @@ -18,14 +19,72 @@ pub fn create(store: &mut Store,

if TransactionRequest::verify_create(request)? {
let transaction = TransactionRequest::parse_create(request)?;
transaction.check()?;

if Transaction::store_lookup(store, &transaction.id)? {
return error(store, request, "already found");
}

let mut new_utxos: Vec<UTxO> = Vec::new();

for ref input in transaction.inputs.iter() {
let coin = &input.coin;
if !UTxO::store_lookup(store, &coin.id)? {
if Transaction::store_lookup(store, &coin.tx_id)? {
return error(store, request, &format!("unknown transaction: {:?}", coin.tx_id));
}
new_utxos.push(coin.to_owned());
}
}

for utxo in new_utxos {
utxo.store_create(store)?;
}

transaction.store_create(store)?;
// TODO: store input coins per transaction
} else if BlockRequest::verify_create(request)? {
let block = BlockRequest::parse_create(request)?;
block.check()?;

if Block::store_lookup(store, &block.id)? {
return error(store, request, "already found");
}

for tx in block.transactions.iter() {
if !Transaction::store_lookup(store, &tx.id)? {
return error(store, request, &format!("transaction not found: {:?}", tx.id));
}
}

let blockchains = BlockChain::store_list(store, None, None, None, 0)?;
if blockchains.len() > 1 {
return error(store, request, "internal server error");
}

let mut blockchain = blockchains[0].to_owned();

if blockchain.tip_idx != Some(0) {
return error(store, request, "internal server error");
}

if blockchain.frontier_len != 1 {
return error(store, request, "internal server error");
}

let mut hasher = Hasher{};

if blockchain.height < block.height {
blockchain.height = block.height;
let blocknode = BlockNode::new()
.meta(&block.meta)?
.block_data(&block.id, block.height)?
.finalize(&mut hasher)?;

blockchain.frontier = vec![blocknode];
blockchain = blockchain.finalize(&mut hasher)?;
blockchain.store_create(store)?;
}

// TODO: check if txs exist
// check if tx input coins are in store
// whatever: make a list
block.store_create(store)?;
} else {
return error(store, request, "invalid resource");
Expand Down
7 changes: 7 additions & 0 deletions src/io/network/server/handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl HandlerBase<Store, (), (), Digest, Vec<u8>> for Handler {
-> Result<Response>
{
ping(store, request)
.or_else(|e| error(store, request, &format!("{}", e)))
}

fn handle_session(&mut self,
Expand All @@ -24,6 +25,7 @@ impl HandlerBase<Store, (), (), Digest, Vec<u8>> for Handler {
-> Result<Response>
{
session(store, request)
.or_else(|e| error(store, request, &format!("{}", e)))
}

fn handle_count(&mut self,
Expand All @@ -32,6 +34,7 @@ impl HandlerBase<Store, (), (), Digest, Vec<u8>> for Handler {
-> Result<Response>
{
count(store, request)
.or_else(|e| error(store, request, &format!("{}", e)))
}

fn handle_list(&mut self,
Expand All @@ -40,6 +43,7 @@ impl HandlerBase<Store, (), (), Digest, Vec<u8>> for Handler {
-> Result<Response>
{
list(store, request)
.or_else(|e| error(store, request, &format!("{}", e)))
}

fn handle_lookup(&mut self,
Expand All @@ -48,6 +52,7 @@ impl HandlerBase<Store, (), (), Digest, Vec<u8>> for Handler {
-> Result<Response>
{
lookup(store, request)
.or_else(|e| error(store, request, &format!("{}", e)))
}

fn handle_get(&mut self,
Expand All @@ -56,6 +61,7 @@ impl HandlerBase<Store, (), (), Digest, Vec<u8>> for Handler {
-> Result<Response>
{
get(store, request)
.or_else(|e| error(store, request, &format!("{}", e)))
}

fn handle_create(&mut self,
Expand All @@ -64,6 +70,7 @@ impl HandlerBase<Store, (), (), Digest, Vec<u8>> for Handler {
-> Result<Response>
{
create(store, request)
.or_else(|e| error(store, request, &format!("{}", e)))
}

fn handle_update(&mut self,
Expand Down

0 comments on commit ca15aef

Please sign in to comment.