2016-12-21 04:29:35 +03:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
//! Logging, as well as various low-level utilities that factor Rust
|
2017-10-12 19:56:44 +03:00
|
|
|
//! patterns that are frequent within the grin codebase.
|
2016-12-21 04:29:35 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
#![deny(non_upper_case_globals)]
|
|
|
|
#![deny(non_camel_case_types)]
|
|
|
|
#![deny(non_snake_case)]
|
|
|
|
#![deny(unused_mut)]
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate slog;
|
|
|
|
extern crate slog_async;
|
2017-11-01 02:32:33 +03:00
|
|
|
extern crate slog_term;
|
2018-01-10 22:36:27 +03:00
|
|
|
extern crate byteorder;
|
2017-11-07 19:48:37 +03:00
|
|
|
extern crate rand;
|
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
2017-11-01 02:20:55 +03:00
|
|
|
// Re-export so only has to be included once
|
|
|
|
pub extern crate secp256k1zkp as secp_;
|
|
|
|
pub use secp_ as secp;
|
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
// Logging related
|
|
|
|
pub mod logger;
|
2017-11-01 02:32:33 +03:00
|
|
|
pub use logger::{init_logger, init_test_logger, LOGGER};
|
2017-10-12 19:56:44 +03:00
|
|
|
|
2017-11-07 19:48:37 +03:00
|
|
|
// Static secp instance
|
|
|
|
pub mod secp_static;
|
|
|
|
pub use secp_static::static_secp_instance;
|
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
pub mod types;
|
2018-01-10 22:36:27 +03:00
|
|
|
pub use types::{LoggingConfig, LogLevel};
|
2017-10-12 19:56:44 +03:00
|
|
|
|
|
|
|
// other utils
|
2017-11-01 02:32:33 +03:00
|
|
|
use std::cell::{Ref, RefCell};
|
2016-12-21 04:29:35 +03:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::ops::Deref;
|
2018-01-10 22:36:27 +03:00
|
|
|
use byteorder::{BigEndian, ByteOrder};
|
2016-12-21 04:29:35 +03:00
|
|
|
|
2017-05-25 02:08:39 +03:00
|
|
|
mod hex;
|
|
|
|
pub use hex::*;
|
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
/// Encapsulation of a RefCell<Option<T>> for one-time initialization after
|
|
|
|
/// construction. This implementation will purposefully fail hard if not used
|
|
|
|
/// properly, for example if it's not initialized before being first used
|
|
|
|
/// (borrowed).
|
2017-07-04 02:46:25 +03:00
|
|
|
#[derive(Clone)]
|
2016-12-21 04:29:35 +03:00
|
|
|
pub struct OneTime<T> {
|
2017-10-12 19:56:44 +03:00
|
|
|
/// inner
|
2017-09-29 21:44:25 +03:00
|
|
|
inner: RefCell<Option<T>>,
|
2016-12-21 04:29:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T> Sync for OneTime<T> {}
|
|
|
|
unsafe impl<T> Send for OneTime<T> {}
|
|
|
|
|
|
|
|
impl<T> OneTime<T> {
|
2017-09-29 21:44:25 +03:00
|
|
|
/// Builds a new uninitialized OneTime.
|
|
|
|
pub fn new() -> OneTime<T> {
|
2017-11-01 02:32:33 +03:00
|
|
|
OneTime {
|
|
|
|
inner: RefCell::new(None),
|
|
|
|
}
|
2017-09-29 21:44:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes the OneTime, should only be called once after construction.
|
|
|
|
pub fn init(&self, value: T) {
|
|
|
|
let mut inner_mut = self.inner.borrow_mut();
|
|
|
|
*inner_mut = Some(value);
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:56:47 +03:00
|
|
|
/// Whether the OneTime has been initialized
|
2017-10-17 16:51:02 +03:00
|
|
|
pub fn is_initialized(&self) -> bool {
|
2017-12-14 00:52:21 +03:00
|
|
|
match self.inner.try_borrow() {
|
|
|
|
Ok(inner) => inner.is_some(),
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
2017-10-17 16:51:02 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
/// Borrows the OneTime, should only be called after initialization.
|
|
|
|
pub fn borrow(&self) -> Ref<T> {
|
|
|
|
Ref::map(self.inner.borrow(), |o| o.as_ref().unwrap())
|
|
|
|
}
|
2016-12-21 04:29:35 +03:00
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
/// Construct msg bytes from tx fee and lock_height
|
|
|
|
pub fn kernel_sig_msg(fee: u64, lock_height: u64) -> [u8; 32] {
|
|
|
|
let mut bytes = [0; 32];
|
|
|
|
BigEndian::write_u64(&mut bytes[16..24], fee);
|
|
|
|
BigEndian::write_u64(&mut bytes[24..], lock_height);
|
|
|
|
bytes
|
|
|
|
}
|