Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction validity and to-plain conversion for PoW transactions #3187

Merged
merged 14 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions blockchain-interface/src/abstract_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub trait AbstractBlockchain {
/// Obtains the hash associated with the genesis block.
fn get_genesis_hash(&self) -> Blake2bHash;

/// Obtains the genesis block.
fn get_genesis_block(&self) -> Block;

/// Fetches a given block, by its hash.
fn get_block(&self, hash: &Blake2bHash, include_body: bool) -> Result<Block, BlockchainError>;

Expand Down
4 changes: 4 additions & 0 deletions blockchain-proxy/src/blockchain_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ impl AbstractBlockchain for BlockchainReadProxy<'_> {
gen_blockchain_match!(self, BlockchainReadProxy, get_genesis_hash)
}

fn get_genesis_block(&self) -> Block {
gen_blockchain_match!(self, BlockchainReadProxy, get_genesis_block)
}

fn get_block(&self, hash: &Blake2bHash, include_body: bool) -> Result<Block, BlockchainError> {
gen_blockchain_match!(self, BlockchainReadProxy, get_block, hash, include_body)
}
Expand Down
5 changes: 5 additions & 0 deletions blockchain/src/blockchain/abstract_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl AbstractBlockchain for Blockchain {
self.genesis_hash.clone()
}

fn get_genesis_block(&self) -> Block {
self.get_block_at(self.genesis_block_number, true, None)
.unwrap()
}

fn get_block(&self, hash: &Blake2bHash, include_body: bool) -> Result<Block, BlockchainError> {
self.get_block(hash, include_body, None)
}
Expand Down
4 changes: 1 addition & 3 deletions consensus/src/consensus/consensus_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ impl<N: Network> ConsensusProxy<N> {
log::warn!(peer = %peer_id, "The genesis hash from the peer does not match our own");
continue;
}
}

if election_head.hash() == block_hash
} else if election_head.hash() == block_hash
|| election_head.header.parent_election_hash == block_hash
{
already_proven = true;
Expand Down
4 changes: 4 additions & 0 deletions light-blockchain/src/abstract_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl AbstractBlockchain for LightBlockchain {
self.genesis_block.hash()
}

fn get_genesis_block(&self) -> Block {
self.genesis_block.clone()
}

fn get_blocks(
&self,
start_block_hash: &Blake2bHash,
Expand Down
4 changes: 2 additions & 2 deletions mempool/tests/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn it_can_blacklist_transactions() {
Coin::try_from(100).unwrap(),
Coin::try_from(1).unwrap(),
123,
NetworkId::Main,
NetworkId::MainAlbatross,
);

let hash: Blake2bHash = tx.hash();
Expand All @@ -40,7 +40,7 @@ fn it_accepts_and_rejects_transactions() {
Coin::try_from(0).unwrap(),
Coin::try_from(0).unwrap(),
0,
NetworkId::Main,
NetworkId::MainAlbatross,
);

assert!(!f.accepts_transaction(&tx));
Expand Down
1 change: 1 addition & 0 deletions pow-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ build-data = "0.2"
serde_json = "1.0"

nimiq-test-log = { workspace = true }
nimiq-web-client = { workspace = true, features = ["primitives"] }

[features]
pow-migration-tests = []
110 changes: 108 additions & 2 deletions pow-migration/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ pub async fn get_history_store_height(env: MdbxDatabase, network_id: NetworkId)
#[cfg(test)]
mod test {

use nimiq_web_client::common::transaction::Transaction as WebTransaction;

use super::*;

static TRANSACTIONS: &str = r#"
[
{
{
"hash": "b216c4d1b655ebc918fcd25212f1f5abb1ed82a45a2114ee7109f92dba955f5c",
"blockHash": "cbca3812447d51983a55beb2d16464b45dba19db29abdbbb7b9be8cead4a66a2",
"blockNumber": 2815085,
Expand Down Expand Up @@ -552,6 +554,106 @@ mod test {
"flags": 0,
"validityStartHeight": 2815092,
"networkId": 42
},
{
"hash": "1ae3f9d84e1951863b34f9de0f30e6d4a59619cd0cb4dd70fbd75c330bc14cbc",
"blockHash": "0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": 3381335,
"timestamp": 1727529953,
"confirmations": 1818875,
"from": "0000000000000000000000000000000000000000",
"fromAddress": "NQ12 0B4A X8HK CDYF LVLR RVVT DKB3 FS1G Y2DS",
"fromType": 0,
"to": "0000000000000000000000000000000000000000",
"toAddress": "NQ14 PC35 FMEJ X6PT J74X 8CHK FN66 RN68 Y2AC",
"toType": 1,
"value": 20000000,
"fee": 0,
"data": "5fb123ae5c64698b85e03e06a8259c4fd9e8152500339858000000020000000000989680",
"flags": 1,
"validityStartHeight": 3381332,
"proof": "26dcb47806d6d1a0f51ab470d518799dfc29236c79dad4c9c1ce4d09c64651af00b7f06e9118254767372c8d07f3f5cff6d753097129624b8f83a16652d518233bcb5d35e136cc50d0fed7aea67b5548c62017c1719cba80c0d943495a862f4b0b",
"networkId": 42
},
{
"hash": "d98625628971998dd9f5b29fc0f95615254cfd60b342fe729620889d7b3b15ac",
"blockHash": "0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": 3454647,
"timestamp": 1731952574,
"confirmations": 1745013,
"from": "0000000000000000000000000000000000000000",
"fromAddress": "NQ19 LYQQ BL3N E4K6 AXCN CFB4 TDD4 LAN0 BYK1",
"fromType": 0,
"to": "0000000000000000000000000000000000000000",
"toAddress": "NQ72 C5TJ 9RU2 HM64 SH8P D222 GCCE 41N3 HXC1",
"toType": 2,
"value": 515101564,
"fee": 0,
"data": "a7f185d076712665799663d64db5a4a2ac05fe61eb933bf41fdcc9bc2ebf439305a0b2c64d5aea34033913543fa4e5b6c41176ee552d314db28d786bd87f103ee25f49f4e2555e51d1010034b71d",
"flags": 1,
"validityStartHeight": 3454645,
"proof": "05cd72f08df886acf3f7ae7b5abe7aca89c2191f092a2c121b1d7b0d26d2244500b2fba4e35847d6e3e4b4292e797cb256ec8f5f42ce9f347bbfc9c0ff19deaa3c9b5171ca98347ff1b2f3d48918059efcc4662f98eb4a53702942be7174430a0e",
"networkId": 42
},
{
"hash": "9b0448c37c40ecf48bf60a1240dda67085b12ab9f6848a3c9f026682acd66621",
"blockHash": "0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": 3454649,
"timestamp": 1731952691,
"confirmations": 1744481,
"from": "0000000000000000000000000000000000000000",
"fromAddress": "NQ72 C5TJ 9RU2 HM64 SH8P D222 GCCE 41N3 HXC1",
"fromType": 2,
"to": "0000000000000000000000000000000000000000",
"toAddress": "NQ56 VE9K PV0Y TK4T QBMY 8E9G B85J QR6M MSHL",
"toType": 0,
"value": 515101564,
"fee": 0,
"data": "",
"flags": 0,
"validityStartHeight": 3454647,
"proof": "0103013913543fa4e5b6c41176ee552d314db28d786bd87f103ee25f49f4e2555e51d1bff5b88ef94cd7c2ba354a8e4b50fef063ab1659646570b34effbb48f36ecb4c08600ec9f0d44dc8d43275c705d7780caa31497d2620da4d7838d10574a6dfa100410b82decb73b7c6f4047b4fb504000c364edd9a3337e5194b60f896d31904ccab8bf310cf808fd98a9b3b13096b6701d53bbba8402465d08cb99948c8407500",
"networkId": 42
},
{
"hash": "25ab1a2708b2ca845ffebaa6a1cfa6cb0e99f7221b71f0036caff0930ab24bc3",
"blockHash": "0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": 3455024,
"timestamp": 1731974479,
"confirmations": 1745593,
"from": "0000000000000000000000000000000000000000",
"fromAddress": "NQ83 5445 36JA 0528 2H8H 7PEJ 3S82 5H9X AT6J",
"fromType": 2,
"to": "0000000000000000000000000000000000000000",
"toAddress": "NQ54 S7LK APF3 KNDN 4YCE V9UY 22VN V160 S96D",
"toType": 2,
"value": 85957876150,
"fee": 0,
"data": "15f81c52d072d974d4a8463f8c7c36950be8228aa70b9e44a448b5183ac4e186cd749d3d889fff8401000000000000000000000000000000000000000000000000000000000000000001003506ee",
"flags": 1,
"validityStartHeight": 3455022,
"proof": "0291b21f4b100273bd7034f6369c29d1f7ba72dba7de6720ad3cd8b8191621891300d18f33335132492722f0c7024573bc73ec074eddc7b8a83bc1fccc11ce2f6cf126d1500003b788c969cca371439e48c7decc7ff5dcb7851aca4475e53f9bf901381418b67ad00bf1d056858aa31f10c5bf3d70a44bfe9c5245e3e5bf3f798189005ee1023a0af18e6388df3ba29ed03d5e8834da00307d326545d6cee0b56b9bba6d9fc42d22970efa47c83f9f1b67dbe34f0fef968dd943fbdafa54c9f9e0ee0d",
"networkId": 42
},
{
"hash": "19a8088a3450e0f46870a678a7c062edf543449a3f2b48196b0d92ac7260a6c3",
"blockHash": "0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": 2802559,
"timestamp": 1700088904,
"confirmations": 2515175,
"from": "0000000000000000000000000000000000000000",
"fromAddress": "NQ15 VH3S CFFN BJVE LBGM 7YJA 4B85 8A9V CNTU",
"fromType": 0,
"to": "0000000000000000000000000000000000000000",
"toAddress": "NQ86 UVS1 UJP6 J1SX QVDP EXF7 GYR2 YEN5 JKV9",
"toType": 0,
"value": 300000,
"fee": 0,
"data": "",
"flags": 0,
"validityStartHeight": 2802559,
"proof": "c79090f344bf7ed4cdd6c25512ee61d1d5fe9cff643263342996ba3448df189f0280de8d7ee7e54f301095294d494024430c8b251b4ebf9b1384922dc7f9dd24422f830e231d26cdc3bbd1f55f1918757568522acae62c21e8046190ea84d6e8ff160caadca71723067d5080d6c3858b61ef8cdf286326818e90ddbefe23af2d529cef7654be5b99bb418786d49e164b24f9db1c482545d8e4473804a53b889e4b07",
"networkId": 1
}
]"#;

Expand All @@ -560,7 +662,11 @@ mod test {
let pow_transactions: Vec<PoWTransaction> = serde_json::from_str(TRANSACTIONS).unwrap();
for txn in pow_transactions {
let pos_transaction = from_pow_transaction(&txn).unwrap();
assert_eq!(txn.hash, pos_transaction.hash::<Blake2bHash>().to_hex())
assert_eq!(txn.hash, pos_transaction.hash::<Blake2bHash>().to_hex());

let web_transaction: WebTransaction = pos_transaction.into();
// Convert with the genesis block number and timestamp of mainnet.
web_transaction.to_plain_transaction(Some(3456000), Some(1732034720000));
}
}
}
8 changes: 4 additions & 4 deletions primitives/account/tests/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn it_can_commit_and_revert_a_block_body() {
Coin::from_u64_unchecked(10),
Coin::ZERO,
1,
NetworkId::Main,
NetworkId::MainAlbatross,
);

let transactions = vec![tx];
Expand Down Expand Up @@ -192,7 +192,7 @@ fn it_correctly_rewards_validators() {
value1,
fee1,
2,
NetworkId::Main,
NetworkId::MainAlbatross,
);

let tx2 = Transaction::new_basic(
Expand All @@ -201,7 +201,7 @@ fn it_correctly_rewards_validators() {
value2,
fee2,
2,
NetworkId::Main,
NetworkId::MainAlbatross,
);

// Validator 2 mines second block.
Expand Down Expand Up @@ -261,7 +261,7 @@ fn it_checks_for_sufficient_funds() {
Coin::try_from(10).unwrap(),
Coin::from_u64_unchecked(1),
1,
NetworkId::Main,
NetworkId::MainAlbatross,
);

let reward = Inherent::Reward {
Expand Down
10 changes: 7 additions & 3 deletions primitives/transaction/src/account/basic_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use nimiq_primitives::{account::AccountType, policy::Policy};
use nimiq_serde::Deserialize;

use crate::{
account::AccountTransactionVerification, SignatureProof, Transaction, TransactionError,
TransactionFlags,
account::AccountTransactionVerification, PoWSignatureProof, SignatureProof, Transaction,
TransactionError, TransactionFlags,
};

/// The verifier trait for a basic account. This only uses data available in the transaction.
Expand Down Expand Up @@ -56,7 +56,11 @@ impl AccountTransactionVerification for BasicAccountVerifier {
}

// Verify signer & signature.
let signature_proof = SignatureProof::deserialize_all(&transaction.proof)?;
let signature_proof = if transaction.network_id.is_albatross() {
SignatureProof::deserialize_all(&transaction.proof)?
} else {
PoWSignatureProof::deserialize_all(&transaction.proof)?.into_pos()
};

if !signature_proof.is_signed_by(&transaction.sender)
|| !signature_proof.verify(&transaction.serialize_content())
Expand Down
Loading
Loading