mirror of
https://github.com/serai-dex/serai.git
synced 2025-03-23 07:38:46 +00:00
* Add v1 ring sig verifying * allow calculating signature hash for v1 txs * add unreduced scalar type with recovery I have added this type for borromen sigs, the ee field can be a normal scalar as in the verify function the ee field is checked against a reduced scalar mean for it to verify as correct ee must be reduced * change block major/ minor versions to u8 this matches Monero I have also changed a couple varint functions to accept the `VarInt` trait * expose `serialize_hashable` on `Block` * add back MLSAG verifying functions I still need to revert the commit removing support for >1 input MLSAG FULL This adds a new rct type to separate Full and simple rct * add back support for multiple inputs for RCT FULL * comment `non_adjacent_form` function also added `#[allow(clippy::needless_range_loop)]` around a loop as without a re-write satisfying clippy without it will make the function worse. * Improve Mlsag verifying API * fix rebase errors * revert the changes on `reserialize_chain` plus other misc changes * fix no-std * Reduce the amount of rpc calls needed for `get_block_by_number`. This function was causing me problems, every now and then a node would return a block with a different number than requested. * change `serialize_hashable` to give the POW hashing blob. Monero calculates the POW hash and the block hash using *slightly* different blobs :/ * make ring_signatures public and add length check when verifying. * Misc improvements and bug fixes --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
123 lines
3.4 KiB
Rust
123 lines
3.4 KiB
Rust
use std_shims::{
|
|
vec::Vec,
|
|
io::{self, Read, Write},
|
|
};
|
|
|
|
use crate::{
|
|
hash,
|
|
merkle::merkle_root,
|
|
serialize::*,
|
|
transaction::{Input, Transaction},
|
|
};
|
|
|
|
const CORRECT_BLOCK_HASH_202612: [u8; 32] =
|
|
hex_literal::hex!("426d16cff04c71f8b16340b722dc4010a2dd3831c22041431f772547ba6e331a");
|
|
const EXISTING_BLOCK_HASH_202612: [u8; 32] =
|
|
hex_literal::hex!("bbd604d2ba11ba27935e006ed39c9bfdd99b76bf4a50654bc1e1e61217962698");
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
pub struct BlockHeader {
|
|
pub major_version: u8,
|
|
pub minor_version: u8,
|
|
pub timestamp: u64,
|
|
pub previous: [u8; 32],
|
|
pub nonce: u32,
|
|
}
|
|
|
|
impl BlockHeader {
|
|
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
write_varint(&self.major_version, w)?;
|
|
write_varint(&self.minor_version, w)?;
|
|
write_varint(&self.timestamp, w)?;
|
|
w.write_all(&self.previous)?;
|
|
w.write_all(&self.nonce.to_le_bytes())
|
|
}
|
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
let mut serialized = vec![];
|
|
self.write(&mut serialized).unwrap();
|
|
serialized
|
|
}
|
|
|
|
pub fn read<R: Read>(r: &mut R) -> io::Result<BlockHeader> {
|
|
Ok(BlockHeader {
|
|
major_version: read_varint(r)?,
|
|
minor_version: read_varint(r)?,
|
|
timestamp: read_varint(r)?,
|
|
previous: read_bytes(r)?,
|
|
nonce: read_bytes(r).map(u32::from_le_bytes)?,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
pub struct Block {
|
|
pub header: BlockHeader,
|
|
pub miner_tx: Transaction,
|
|
pub txs: Vec<[u8; 32]>,
|
|
}
|
|
|
|
impl Block {
|
|
pub fn number(&self) -> usize {
|
|
match self.miner_tx.prefix.inputs.first() {
|
|
Some(Input::Gen(number)) => (*number).try_into().unwrap(),
|
|
_ => panic!("invalid block, miner TX didn't have a Input::Gen"),
|
|
}
|
|
}
|
|
|
|
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
self.header.write(w)?;
|
|
self.miner_tx.write(w)?;
|
|
write_varint(&self.txs.len(), w)?;
|
|
for tx in &self.txs {
|
|
w.write_all(tx)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn tx_merkle_root(&self) -> [u8; 32] {
|
|
merkle_root(self.miner_tx.hash(), &self.txs)
|
|
}
|
|
|
|
/// Serialize the block as required for the proof of work hash.
|
|
///
|
|
/// This is distinct from the serialization required for the block hash. To get the block hash,
|
|
/// use the [`Block::hash`] function.
|
|
pub fn serialize_hashable(&self) -> Vec<u8> {
|
|
let mut blob = self.header.serialize();
|
|
blob.extend_from_slice(&self.tx_merkle_root());
|
|
write_varint(&(1 + u64::try_from(self.txs.len()).unwrap()), &mut blob).unwrap();
|
|
|
|
blob
|
|
}
|
|
|
|
pub fn hash(&self) -> [u8; 32] {
|
|
let mut hashable = self.serialize_hashable();
|
|
// Monero pre-appends a VarInt of the block hashing blobs length before getting the block hash
|
|
// but doesn't do this when getting the proof of work hash :)
|
|
let mut hashing_blob = Vec::with_capacity(8 + hashable.len());
|
|
write_varint(&u64::try_from(hashable.len()).unwrap(), &mut hashing_blob).unwrap();
|
|
hashing_blob.append(&mut hashable);
|
|
|
|
let hash = hash(&hashing_blob);
|
|
if hash == CORRECT_BLOCK_HASH_202612 {
|
|
return EXISTING_BLOCK_HASH_202612;
|
|
};
|
|
|
|
hash
|
|
}
|
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
let mut serialized = vec![];
|
|
self.write(&mut serialized).unwrap();
|
|
serialized
|
|
}
|
|
|
|
pub fn read<R: Read>(r: &mut R) -> io::Result<Block> {
|
|
Ok(Block {
|
|
header: BlockHeader::read(r)?,
|
|
miner_tx: Transaction::read(r)?,
|
|
txs: (0_usize .. read_varint(r)?).map(|_| read_bytes(r)).collect::<Result<_, _>>()?,
|
|
})
|
|
}
|
|
}
|