2018-03-30 10:43:38 -07:00
|
|
|
//! The `mint` module is a library for generating the chain's genesis block.
|
2018-03-03 13:41:36 -08:00
|
|
|
|
2018-05-11 11:38:52 -07:00
|
|
|
use entry::Entry;
|
2018-03-06 16:36:45 -08:00
|
|
|
use hash::{hash, Hash};
|
2018-03-03 18:57:22 -08:00
|
|
|
use ring::rand::SystemRandom;
|
2018-08-09 08:13:57 -07:00
|
|
|
use signature::{Keypair, KeypairUtil, Pubkey};
|
2018-09-26 08:51:51 -07:00
|
|
|
use transaction::{SystemTransaction, Transaction};
|
2018-03-03 13:41:36 -08:00
|
|
|
use untrusted::Input;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2018-03-07 16:08:12 -08:00
|
|
|
pub struct Mint {
|
2018-03-03 13:41:36 -08:00
|
|
|
pub pkcs8: Vec<u8>,
|
2018-08-09 08:13:57 -07:00
|
|
|
pubkey: Pubkey,
|
2018-03-05 16:29:32 -08:00
|
|
|
pub tokens: i64,
|
2018-03-03 13:41:36 -08:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:08:12 -08:00
|
|
|
impl Mint {
|
2018-07-12 14:42:01 -07:00
|
|
|
pub fn new_with_pkcs8(tokens: i64, pkcs8: Vec<u8>) -> Self {
|
2018-05-11 11:38:52 -07:00
|
|
|
let keypair =
|
2018-08-09 07:56:04 -07:00
|
|
|
Keypair::from_pkcs8(Input::from(&pkcs8)).expect("from_pkcs8 in mint pub fn new");
|
2018-03-08 07:32:57 -08:00
|
|
|
let pubkey = keypair.pubkey();
|
|
|
|
Mint {
|
|
|
|
pkcs8,
|
|
|
|
pubkey,
|
|
|
|
tokens,
|
|
|
|
}
|
2018-03-03 13:41:36 -08:00
|
|
|
}
|
2018-07-12 14:42:01 -07:00
|
|
|
|
|
|
|
pub fn new(tokens: i64) -> Self {
|
|
|
|
let rnd = SystemRandom::new();
|
2018-08-09 07:56:04 -07:00
|
|
|
let pkcs8 = Keypair::generate_pkcs8(&rnd)
|
2018-07-12 14:42:01 -07:00
|
|
|
.expect("generate_pkcs8 in mint pub fn new")
|
|
|
|
.to_vec();
|
|
|
|
Self::new_with_pkcs8(tokens, pkcs8)
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:32:22 -08:00
|
|
|
pub fn seed(&self) -> Hash {
|
2018-03-05 11:48:09 -08:00
|
|
|
hash(&self.pkcs8)
|
|
|
|
}
|
|
|
|
|
2018-04-02 14:02:23 -07:00
|
|
|
pub fn last_id(&self) -> Hash {
|
|
|
|
self.create_entries()[1].id
|
|
|
|
}
|
|
|
|
|
2018-08-09 07:56:04 -07:00
|
|
|
pub fn keypair(&self) -> Keypair {
|
|
|
|
Keypair::from_pkcs8(Input::from(&self.pkcs8)).expect("from_pkcs8 in mint pub fn keypair")
|
2018-03-03 23:13:40 -08:00
|
|
|
}
|
|
|
|
|
2018-08-09 08:13:57 -07:00
|
|
|
pub fn pubkey(&self) -> Pubkey {
|
2018-03-08 07:32:57 -08:00
|
|
|
self.pubkey
|
2018-03-03 23:13:40 -08:00
|
|
|
}
|
|
|
|
|
2018-05-25 14:51:41 -07:00
|
|
|
pub fn create_transactions(&self) -> Vec<Transaction> {
|
2018-03-08 09:05:00 -08:00
|
|
|
let keypair = self.keypair();
|
2018-09-17 15:38:10 -07:00
|
|
|
let tx = Transaction::system_move(&keypair, self.pubkey(), self.tokens, self.seed(), 0);
|
2018-05-25 15:05:37 -07:00
|
|
|
vec![tx]
|
2018-03-03 13:41:36 -08:00
|
|
|
}
|
2018-03-05 12:03:56 -08:00
|
|
|
|
2018-03-06 19:22:30 -08:00
|
|
|
pub fn create_entries(&self) -> Vec<Entry> {
|
2018-09-21 21:01:13 -07:00
|
|
|
let e0 = Entry::new(&self.seed(), 0, vec![]);
|
|
|
|
let e1 = Entry::new(&e0.id, 0, self.create_transactions());
|
2018-03-09 15:16:29 -08:00
|
|
|
vec![e0, e1]
|
2018-03-05 12:03:56 -08:00
|
|
|
}
|
2018-03-03 13:41:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-09-17 15:38:10 -07:00
|
|
|
use bincode::deserialize;
|
2018-04-02 10:36:51 -07:00
|
|
|
use ledger::Block;
|
2018-09-19 17:25:57 -07:00
|
|
|
use system_program::SystemProgram;
|
2018-03-03 13:41:36 -08:00
|
|
|
|
|
|
|
#[test]
|
2018-05-25 14:51:41 -07:00
|
|
|
fn test_create_transactions() {
|
|
|
|
let mut transactions = Mint::new(100).create_transactions().into_iter();
|
2018-05-25 15:05:37 -07:00
|
|
|
let tx = transactions.next().unwrap();
|
2018-09-19 17:25:57 -07:00
|
|
|
assert!(SystemProgram::check_id(&tx.program_id));
|
|
|
|
let instruction: SystemProgram = deserialize(&tx.userdata).unwrap();
|
|
|
|
if let SystemProgram::Move { tokens } = instruction {
|
2018-09-17 15:38:10 -07:00
|
|
|
assert_eq!(tokens, 100);
|
2018-03-03 23:13:40 -08:00
|
|
|
}
|
2018-05-25 14:51:41 -07:00
|
|
|
assert_eq!(transactions.next(), None);
|
2018-03-03 13:41:36 -08:00
|
|
|
}
|
|
|
|
|
2018-03-05 14:34:15 -08:00
|
|
|
#[test]
|
|
|
|
fn test_verify_entries() {
|
2018-03-07 16:08:12 -08:00
|
|
|
let entries = Mint::new(100).create_entries();
|
2018-04-02 10:36:51 -07:00
|
|
|
assert!(entries[..].verify(&entries[0].id));
|
2018-03-05 14:34:15 -08:00
|
|
|
}
|
2018-03-03 13:41:36 -08:00
|
|
|
}
|