2018-11-03 09:46:12 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use super::utils::w;
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::core::core::hash::Hashed;
|
|
|
|
use crate::core::core::Transaction;
|
2019-06-27 19:19:41 +03:00
|
|
|
use crate::core::ser::{self, ProtocolVersion};
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::pool;
|
|
|
|
use crate::rest::*;
|
|
|
|
use crate::router::{Handler, ResponseFuture};
|
|
|
|
use crate::types::*;
|
|
|
|
use crate::util;
|
|
|
|
use crate::util::RwLock;
|
|
|
|
use crate::web::*;
|
2019-02-16 00:17:00 +03:00
|
|
|
use failure::ResultExt;
|
2019-03-18 21:34:35 +03:00
|
|
|
use futures::future::{err, ok};
|
2018-11-03 09:46:12 +03:00
|
|
|
use futures::Future;
|
|
|
|
use hyper::{Body, Request, StatusCode};
|
|
|
|
use std::sync::Weak;
|
|
|
|
|
|
|
|
/// Get basic information about the transaction pool.
|
|
|
|
/// GET /v1/pool
|
|
|
|
pub struct PoolInfoHandler {
|
|
|
|
pub tx_pool: Weak<RwLock<pool::TransactionPool>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler for PoolInfoHandler {
|
|
|
|
fn get(&self, _req: Request<Body>) -> ResponseFuture {
|
2019-03-18 21:34:35 +03:00
|
|
|
let pool_arc = w_fut!(&self.tx_pool);
|
2018-11-03 09:46:12 +03:00
|
|
|
let pool = pool_arc.read();
|
|
|
|
|
|
|
|
json_response(&PoolInfo {
|
|
|
|
pool_size: pool.total_size(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dummy wrapper for the hex-encoded serialized transaction.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct TxWrapper {
|
|
|
|
tx_hex: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Push new transaction to our local transaction pool.
|
2019-06-15 11:54:45 +03:00
|
|
|
/// POST /v1/pool/push_tx
|
2018-11-03 09:46:12 +03:00
|
|
|
pub struct PoolPushHandler {
|
|
|
|
pub tx_pool: Weak<RwLock<pool::TransactionPool>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PoolPushHandler {
|
2018-12-08 02:59:40 +03:00
|
|
|
fn update_pool(&self, req: Request<Body>) -> Box<dyn Future<Item = (), Error = Error> + Send> {
|
2019-02-16 00:17:00 +03:00
|
|
|
let params = QueryParams::from(req.uri().query());
|
2018-11-03 09:46:12 +03:00
|
|
|
|
|
|
|
let fluff = params.get("fluff").is_some();
|
2019-03-18 21:34:35 +03:00
|
|
|
let pool_arc = match w(&self.tx_pool) {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(e) => return Box::new(err(e)),
|
|
|
|
};
|
2018-11-03 09:46:12 +03:00
|
|
|
|
|
|
|
Box::new(
|
|
|
|
parse_body(req)
|
|
|
|
.and_then(move |wrapper: TxWrapper| {
|
|
|
|
util::from_hex(wrapper.tx_hex)
|
|
|
|
.map_err(|e| ErrorKind::RequestError(format!("Bad request: {}", e)).into())
|
|
|
|
})
|
|
|
|
.and_then(move |tx_bin| {
|
2019-06-27 19:19:41 +03:00
|
|
|
// TODO - pass protocol version in via the api call?
|
2019-07-06 17:51:03 +03:00
|
|
|
let version = ProtocolVersion::local();
|
2019-06-27 19:19:41 +03:00
|
|
|
|
|
|
|
ser::deserialize(&mut &tx_bin[..], version)
|
2018-11-03 09:46:12 +03:00
|
|
|
.map_err(|e| ErrorKind::RequestError(format!("Bad request: {}", e)).into())
|
|
|
|
})
|
|
|
|
.and_then(move |tx: Transaction| {
|
2019-07-04 13:56:42 +03:00
|
|
|
let source = pool::TxSource::PushApi;
|
2018-11-03 09:46:12 +03:00
|
|
|
info!(
|
|
|
|
"Pushing transaction {} to pool (inputs: {}, outputs: {}, kernels: {})",
|
|
|
|
tx.hash(),
|
|
|
|
tx.inputs().len(),
|
|
|
|
tx.outputs().len(),
|
|
|
|
tx.kernels().len(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Push to tx pool.
|
|
|
|
let mut tx_pool = pool_arc.write();
|
2019-02-16 00:17:00 +03:00
|
|
|
let header = tx_pool
|
|
|
|
.blockchain
|
|
|
|
.chain_head()
|
|
|
|
.context(ErrorKind::Internal("Failed to get chain head".to_owned()))?;
|
|
|
|
let res = tx_pool
|
2018-11-03 09:46:12 +03:00
|
|
|
.add_to_pool(source, tx, !fluff, &header)
|
2019-02-16 00:17:00 +03:00
|
|
|
.context(ErrorKind::Internal("Failed to update pool".to_owned()))?;
|
|
|
|
Ok(res)
|
2018-11-03 09:46:12 +03:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler for PoolPushHandler {
|
|
|
|
fn post(&self, req: Request<Body>) -> ResponseFuture {
|
|
|
|
Box::new(
|
|
|
|
self.update_pool(req)
|
|
|
|
.and_then(|_| ok(just_response(StatusCode::OK, "")))
|
|
|
|
.or_else(|e| {
|
|
|
|
ok(just_response(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
format!("failed: {}", e),
|
|
|
|
))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|