2018-05-30 19:48:32 +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.
|
|
|
|
|
|
|
|
//! Builds the blinded output and related signature proof for the block
|
|
|
|
//! reward.
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::consensus::reward;
|
|
|
|
use crate::core::transaction::kernel_sig_msg;
|
|
|
|
use crate::core::{KernelFeatures, Output, OutputFeatures, TxKernel};
|
|
|
|
use crate::keychain::{Identifier, Keychain};
|
|
|
|
use crate::libtx::error::Error;
|
2019-06-27 11:19:17 +03:00
|
|
|
use crate::libtx::{
|
|
|
|
aggsig,
|
|
|
|
proof::{self, ProofBuild},
|
|
|
|
};
|
2019-03-26 15:26:03 +03:00
|
|
|
use crate::util::{secp, static_secp_instance};
|
2019-06-27 11:19:17 +03:00
|
|
|
use grin_keychain::SwitchCommitmentType;
|
2018-05-30 19:48:32 +03:00
|
|
|
|
|
|
|
/// output a reward output
|
2019-06-27 11:19:17 +03:00
|
|
|
pub fn output<K, B>(
|
2019-03-26 15:26:03 +03:00
|
|
|
keychain: &K,
|
2019-06-27 11:19:17 +03:00
|
|
|
builder: &B,
|
2019-03-26 15:26:03 +03:00
|
|
|
key_id: &Identifier,
|
|
|
|
fees: u64,
|
|
|
|
test_mode: bool,
|
|
|
|
) -> Result<(Output, TxKernel), Error>
|
2018-06-08 08:21:54 +03:00
|
|
|
where
|
|
|
|
K: Keychain,
|
2019-06-27 11:19:17 +03:00
|
|
|
B: ProofBuild,
|
2018-06-08 08:21:54 +03:00
|
|
|
{
|
2018-05-30 19:48:32 +03:00
|
|
|
let value = reward(fees);
|
2019-06-27 11:19:17 +03:00
|
|
|
// TODO: proper support for different switch commitment schemes
|
|
|
|
let switch = &SwitchCommitmentType::Regular;
|
|
|
|
let commit = keychain.commit(value, key_id, switch)?;
|
2018-05-30 19:48:32 +03:00
|
|
|
|
2018-10-21 23:30:56 +03:00
|
|
|
trace!("Block reward - Pedersen Commit is: {:?}", commit,);
|
2018-05-30 19:48:32 +03:00
|
|
|
|
2019-06-27 11:19:17 +03:00
|
|
|
let rproof = proof::create(keychain, builder, value, key_id, switch, commit, None)?;
|
2018-05-30 19:48:32 +03:00
|
|
|
|
|
|
|
let output = Output {
|
2019-01-08 19:07:38 +03:00
|
|
|
features: OutputFeatures::Coinbase,
|
2018-05-30 19:48:32 +03:00
|
|
|
commit: commit,
|
|
|
|
proof: rproof,
|
|
|
|
};
|
|
|
|
|
|
|
|
let secp = static_secp_instance();
|
2018-10-20 03:13:07 +03:00
|
|
|
let secp = secp.lock();
|
2018-05-30 19:48:32 +03:00
|
|
|
let over_commit = secp.commit_value(reward(fees))?;
|
|
|
|
let out_commit = output.commitment();
|
|
|
|
let excess = secp.commit_sum(vec![out_commit], vec![over_commit])?;
|
2018-10-10 12:11:01 +03:00
|
|
|
let pubkey = excess.to_pubkey(&secp)?;
|
2018-05-30 19:48:32 +03:00
|
|
|
|
|
|
|
// NOTE: Remember we sign the fee *and* the lock_height.
|
2018-12-18 21:26:34 +03:00
|
|
|
// For a coinbase output the fee is 0 and the lock_height is 0
|
2019-01-08 19:07:38 +03:00
|
|
|
let msg = kernel_sig_msg(0, 0, KernelFeatures::Coinbase)?;
|
2019-03-26 15:26:03 +03:00
|
|
|
let sig = match test_mode {
|
|
|
|
true => {
|
2019-03-26 18:32:54 +03:00
|
|
|
let test_nonce = secp::key::SecretKey::from_slice(&secp, &[1; 32])?;
|
2019-03-26 15:26:03 +03:00
|
|
|
aggsig::sign_from_key_id(
|
|
|
|
&secp,
|
|
|
|
keychain,
|
|
|
|
&msg,
|
|
|
|
value,
|
|
|
|
&key_id,
|
|
|
|
Some(&test_nonce),
|
|
|
|
Some(&pubkey),
|
|
|
|
)?
|
|
|
|
}
|
|
|
|
false => {
|
|
|
|
aggsig::sign_from_key_id(&secp, keychain, &msg, value, &key_id, None, Some(&pubkey))?
|
|
|
|
}
|
|
|
|
};
|
2018-05-30 19:48:32 +03:00
|
|
|
|
|
|
|
let proof = TxKernel {
|
2019-01-08 19:07:38 +03:00
|
|
|
features: KernelFeatures::Coinbase,
|
2018-05-30 19:48:32 +03:00
|
|
|
excess: excess,
|
|
|
|
excess_sig: sig,
|
|
|
|
fee: 0,
|
2018-12-18 21:26:34 +03:00
|
|
|
// lock_height here is 0
|
|
|
|
// *not* the maturity of the coinbase output (only spendable 1,440 blocks later)
|
|
|
|
lock_height: 0,
|
2018-05-30 19:48:32 +03:00
|
|
|
};
|
|
|
|
Ok((output, proof))
|
|
|
|
}
|