Skip to content

Commit

Permalink
Fixes for PR 52
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Oct 23, 2024
1 parent febc0cb commit a69646e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 153 deletions.
68 changes: 36 additions & 32 deletions src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use bitcoin::script::{Instruction, Instructions};
use bitcoin::{hashes::Hash, Amount, Opcode, Script, ScriptBuf, Transaction, TxOut};
use bitcoin::{
hashes::Hash,
opcodes::{
all::{OP_NOP5, OP_PUSHBYTES_1, OP_RETURN},
OP_TRUE,
},
script::PushBytesBuf,
script::{Instruction, Instructions, PushBytesBuf},
Amount, Opcode, Script, ScriptBuf, Transaction, TxOut,
};
use byteorder::{ByteOrder, LittleEndian};
use nom::branch::alt;
use nom::bytes::complete::{tag, take};
use nom::combinator::fail;
use nom::combinator::rest;
use nom::multi::many0;
use nom::IResult;
use sha2::{Digest, Sha256};
use nom::{
branch::alt,
bytes::complete::{tag, take},
combinator::{fail, rest},
multi::many0,
IResult,
};

use crate::types::SidechainNumber;

Expand Down Expand Up @@ -160,27 +160,36 @@ impl M4AckBundles {
}

pub fn parse_coinbase_script(script: &Script) -> IResult<&[u8], CoinbaseMessage> {
let mut instructions = script.instructions();

// Return a nom parsing failure. Would be nice to include a message
// about what went wrong? Is that possible?
fn instruction_failure(instructions: Instructions) -> nom::Err<nom::error::Error<&[u8]>> {
nom::Err::Failure(nom::error::Error {
input: instructions.as_script().as_bytes(),
fn instruction_failure<'a>(
err_msg: Option<&'static str>,
instructions: Instructions<'a>,
) -> nom::Err<nom::error::Error<&'a [u8]>> {
use nom::error::ContextError as _;
let input = instructions.as_script().as_bytes();
let err = nom::error::Error {
input,
code: nom::error::ErrorKind::Fail,
})
}

if instructions.next() != Some(Ok(Instruction::Op(OP_RETURN))) {
return Err(instruction_failure(instructions));
};
let err = match err_msg {
Some(err_msg) => nom::error::Error::add_context(input, err_msg, err),
None => err,
};
nom::Err::Failure(err)
}

let mut instructions = script.instructions();
let Some(Ok(Instruction::Op(OP_RETURN))) = instructions.next() else {
return Err(instruction_failure(
Some("expected OP_RETURN instruction"),
instructions,
));
};
let Some(Ok(Instruction::PushBytes(data))) = instructions.next() else {
return Err(instruction_failure(instructions));
return Err(instruction_failure(
Some("expected PushBytes instruction"),
instructions,
));
};

let input = data.as_bytes();

let (input, message_tag) = alt((
tag(M1_PROPOSE_SIDECHAIN_TAG),
tag(M2_ACK_SIDECHAIN_TAG),
Expand Down Expand Up @@ -379,12 +388,7 @@ impl TryFrom<CoinbaseMessage> for ScriptBuf {
}

pub fn sha256d(data: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(data);
let data_hash: [u8; 32] = hasher.finalize_reset().into();
hasher.update(data_hash);
let data_hash: [u8; 32] = hasher.finalize().into();
data_hash
bitcoin::hashes::sha256d::Hash::hash(data).to_byte_array()
}

pub fn m6_to_id(m6: &Transaction, previous_treasury_utxo_total: u64) -> [u8; 32] {
Expand Down
33 changes: 33 additions & 0 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ pub mod mainchain {
let hex = bitcoin::consensus::encode::serialize_hex(value);
Self { hex: Some(hex) }
}

pub fn encode_hex<T>(value: &T) -> Self
where
T: hex::ToHex,
{
let hex = value.encode_hex();
Self { hex: Some(hex) }
}
}

impl ReverseHex {
Expand Down Expand Up @@ -166,6 +174,31 @@ pub mod mainchain {
}
}

impl From<SidechainDeclarationV0> for SidechainDeclaration {
fn from(declaration: SidechainDeclarationV0) -> Self {
Self {
version: Some(sidechain_declaration::Version::V0(declaration)),
}
}
}

impl From<crate::types::SidechainDeclaration> for SidechainDeclarationV0 {
fn from(declaration: crate::types::SidechainDeclaration) -> Self {
Self {
title: Some(declaration.title),
description: Some(declaration.description),
hash_id_1: Some(ConsensusHex::encode(&declaration.hash_id_1)),
hash_id_2: Some(ConsensusHex::encode_hex(&declaration.hash_id_2)),
}
}
}

impl From<crate::types::SidechainDeclaration> for SidechainDeclaration {
fn from(declaration: crate::types::SidechainDeclaration) -> Self {
SidechainDeclarationV0::from(declaration).into()
}
}

impl From<bitcoin::Network> for Network {
fn from(network: bitcoin::Network) -> Self {
match network {
Expand Down
17 changes: 4 additions & 13 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::sync::Arc;

use crate::proto::mainchain::{
sidechain_declaration, SidechainDeclaration, SidechainDeclarationV0,
};
use crate::proto::mainchain::{
wallet_service_server::WalletService, BroadcastWithdrawalBundleRequest,
BroadcastWithdrawalBundleResponse, CreateBmmCriticalDataTransactionRequest,
Expand Down Expand Up @@ -350,16 +347,10 @@ impl ValidatorService for Validator {
.map(|(data_hash, proposal)| SidechainProposal {
sidechain_number: u8::from(proposal.sidechain_number) as u32,
data: Some(proposal.data.clone()),
declaration: proposal.try_deserialize().ok().map(|(_, deserialized)| {
SidechainDeclaration {
version: Some(sidechain_declaration::Version::V0(SidechainDeclarationV0 {
title: Some(deserialized.title),
description: Some(deserialized.description),
hash_id_1: Some(ConsensusHex::encode(&deserialized.hash_id_1)),
hash_id_2: Some(ConsensusHex::encode(&deserialized.hash_id_2.to_vec())),
})),
}
}),
declaration: (&proposal)
.try_into()
.ok()
.map(|(_, declaration)| declaration.into()),
data_hash: Some(ConsensusHex::encode(&data_hash)),
vote_count: proposal.vote_count as u32,
proposal_height: proposal.proposal_height,
Expand Down
Loading

0 comments on commit a69646e

Please sign in to comment.