Skip to content

Commit b3396e5

Browse files
committed
Move accepted transactions as a top-level vec to VSPC2
GetVirtualChainFromBlockV2Response now has chain_block_accepted_transactions
1 parent 770abb7 commit b3396e5

File tree

10 files changed

+156
-30
lines changed

10 files changed

+156
-30
lines changed

rpc/core/src/model/message.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,15 +2774,15 @@ pub struct GetVirtualChainFromBlockV2Response {
27742774
/// always present, no matter the verbosity level
27752775
pub added_chain_block_hashes: Arc<Vec<RpcHash>>,
27762776
/// struct properties are optionally returned depending on the verbosity level
2777-
pub added_acceptance_data: Arc<Vec<RpcAcceptanceData>>,
2777+
pub chain_block_accepted_transactions: Arc<Vec<RpcChainBlockAcceptedTransactions>>,
27782778
}
27792779

27802780
impl Serializer for GetVirtualChainFromBlockV2Response {
27812781
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
27822782
store!(u16, &1, writer)?;
27832783
store!(Vec<RpcHash>, &self.removed_chain_block_hashes, writer)?;
27842784
store!(Vec<RpcHash>, &self.added_chain_block_hashes, writer)?;
2785-
serialize!(Vec<RpcAcceptanceData>, &self.added_acceptance_data, writer)?;
2785+
serialize!(Vec<RpcChainBlockAcceptedTransactions>, &self.chain_block_accepted_transactions, writer)?;
27862786
Ok(())
27872787
}
27882788
}
@@ -2792,11 +2792,11 @@ impl Deserializer for GetVirtualChainFromBlockV2Response {
27922792
let _version = load!(u16, reader)?;
27932793
let removed_chain_block_hashes = load!(Vec<RpcHash>, reader)?;
27942794
let added_chain_block_hashes = load!(Vec<RpcHash>, reader)?;
2795-
let added_acceptance_data = deserialize!(Vec<RpcAcceptanceData>, reader)?;
2795+
let chain_block_accepted_transactions = deserialize!(Vec<RpcChainBlockAcceptedTransactions>, reader)?;
27962796
Ok(Self {
27972797
removed_chain_block_hashes: removed_chain_block_hashes.into(),
27982798
added_chain_block_hashes: added_chain_block_hashes.into(),
2799-
added_acceptance_data: added_acceptance_data.into(),
2799+
chain_block_accepted_transactions: chain_block_accepted_transactions.into(),
28002800
})
28012801
}
28022802
}

rpc/core/src/model/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ mod mockery {
5656
where
5757
T: Mock,
5858
{
59-
Mock::mock()
59+
// forward to the type's Mock implementation
60+
T::mock()
6061
}
6162

6263
// this function tests serialization and deserialization of a type
@@ -1286,7 +1287,7 @@ mod mockery {
12861287
GetVirtualChainFromBlockV2Response {
12871288
removed_chain_block_hashes: mock(),
12881289
added_chain_block_hashes: mock(),
1289-
added_acceptance_data: mock(),
1290+
chain_block_accepted_transactions: mock(),
12901291
}
12911292
}
12921293
}

rpc/core/src/model/tx.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use kaspa_utils::{hex::ToHex, serde_bytes_fixed_ref};
88
use serde::{Deserialize, Serialize};
99
use workflow_serializer::prelude::*;
1010

11-
use crate::prelude::{RpcHash, RpcScriptClass, RpcSubnetworkId};
11+
use crate::{
12+
prelude::{RpcHash, RpcScriptClass, RpcSubnetworkId},
13+
RpcOptionalHeader, RpcOptionalTransaction,
14+
};
1215

1316
/// Represents the ID of a Kaspa transaction
1417
pub type RpcTransactionId = TransactionId;
@@ -398,3 +401,31 @@ pub struct RpcAcceptedTransactionIds {
398401
pub accepting_block_hash: RpcHash,
399402
pub accepted_transaction_ids: Vec<RpcTransactionId>,
400403
}
404+
405+
/// Represents accepted transaction ids
406+
#[derive(Clone, Debug, Serialize, Deserialize)]
407+
#[serde(rename_all = "camelCase")]
408+
pub struct RpcChainBlockAcceptedTransactions {
409+
pub chain_block_header: RpcOptionalHeader,
410+
pub accepted_transactions: Vec<RpcOptionalTransaction>,
411+
}
412+
413+
impl Serializer for RpcChainBlockAcceptedTransactions {
414+
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
415+
store!(u16, &1, writer)?;
416+
serialize!(RpcOptionalHeader, &self.chain_block_header, writer)?;
417+
serialize!(Vec<RpcOptionalTransaction>, &self.accepted_transactions, writer)?;
418+
419+
Ok(())
420+
}
421+
}
422+
423+
impl Deserializer for RpcChainBlockAcceptedTransactions {
424+
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
425+
let _struct_version = load!(u16, reader)?;
426+
let chain_block_header = deserialize!(RpcOptionalHeader, reader)?;
427+
let accepted_transactions = deserialize!(Vec<RpcOptionalTransaction>, reader)?;
428+
429+
Ok(Self { chain_block_header, accepted_transactions })
430+
}
431+
}

rpc/core/src/wasm/message.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,35 @@ const TS_ACCEPTED_TRANSACTION_IDS: &'static str = r#"
4444
}
4545
"#;
4646

47+
#[wasm_bindgen(typescript_custom_section)]
48+
const TS_ADDED_ACCEPTANCE_DATA: &'static str = r#"
49+
/**
50+
* Accepted Acceptance Data
51+
*
52+
* @category Node RPC
53+
*/
54+
export interface IChainBlockAddedTransactions {
55+
chainBlockHeader: Header;
56+
acceptedTransactions: Transaction[];
57+
}
58+
"#;
59+
60+
// DataVerbosityLevel
61+
#[wasm_bindgen(typescript_custom_section)]
62+
const TS_DATA_VERBOSITY_LEVEL: &'static str = r#"
63+
/**
64+
* Data Verbosity level
65+
*
66+
* @category Node RPC
67+
*/
68+
export enum DataVerbosityLevel {
69+
None = "None",
70+
Low = "Low",
71+
High = "High",
72+
Full = "Full",
73+
}
74+
"#;
75+
4776
// ---
4877

4978
declare! {
@@ -1263,6 +1292,44 @@ try_from! ( args: GetVirtualChainFromBlockResponse, IGetVirtualChainFromBlockRes
12631292
Ok(to_value(&args)?.into())
12641293
});
12651294

1295+
declare! {
1296+
IGetVirtualChainFromBlockV2Request,
1297+
r#"
1298+
/**
1299+
*
1300+
*
1301+
* @category Node RPC
1302+
*/
1303+
export interface IGetVirtualChainFromBlockV2Request {
1304+
startHash : HexString;
1305+
dataVerbosityLevel?: string;
1306+
}
1307+
"#,
1308+
}
1309+
1310+
try_from! ( args: IGetVirtualChainFromBlockV2Request, GetVirtualChainFromBlockV2Request, {
1311+
Ok(from_value(args.into())?)
1312+
});
1313+
1314+
declare! {
1315+
IGetVirtualChainFromBlockV2Response,
1316+
r#"
1317+
/**
1318+
*
1319+
*
1320+
* @category Node RPC
1321+
*/
1322+
export interface IGetVirtualChainFromBlockV2Response {
1323+
removedChainBlockHashes : HexString[];
1324+
addedChainBlockHashes : HexString[];
1325+
chainBlockAcceptedTransactions : IChainBlockAddedTransactions[];
1326+
}
1327+
"#,
1328+
}
1329+
1330+
try_from! ( args: GetVirtualChainFromBlockV2Response, IGetVirtualChainFromBlockV2Response, {
1331+
Ok(to_value(&args)?.into())
1332+
});
12661333
// ---
12671334

12681335
declare! {

rpc/grpc/core/proto/rpc.proto

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ message RpcTransactionOutputVerboseData{
121121
string scriptPublicKeyAddress = 6;
122122
}
123123

124+
message RpcChainBlockAcceptedTransactions {
125+
RpcBlockHeader chainBlockHeader = 1;
126+
repeated RpcTransaction acceptedTransactions = 2;
127+
}
128+
124129
enum RpcNotifyCommand {
125130
NOTIFY_START = 0;
126131
NOTIFY_STOP = 1;
@@ -1029,7 +1034,7 @@ message GetVirtualChainFromBlockV2ResponseMessage{
10291034

10301035
// The mergeset data accepted by each block in addedChainBlockHashes.
10311036
// Will be filled dependent on the supplied RpcAcceptanceDataVerbosity.
1032-
repeated RpcAcceptanceData addedAcceptanceData = 3;
1037+
repeated RpcChainBlockAcceptedTransactions chainBlockAcceptedTransactions = 3;
10331038

10341039
RPCError error = 1000;
10351040
}

rpc/grpc/core/src/convert/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ from!(item: RpcResult<&kaspa_rpc_core::GetVirtualChainFromBlockV2Response>, prot
527527
Self {
528528
removed_chain_block_hashes: item.removed_chain_block_hashes.iter().map(|x| x.to_string()).collect(),
529529
added_chain_block_hashes: item.added_chain_block_hashes.iter().map(|x| x.to_string()).collect(),
530-
added_acceptance_data: item.added_acceptance_data.iter().map(|x| x.into()).collect(),
530+
chain_block_accepted_transactions: item.chain_block_accepted_transactions.iter().map(|x| x.into()).collect(),
531531
error: None,
532532
}
533533
});
@@ -795,7 +795,7 @@ try_from!(item: &protowire::GetVirtualChainFromBlockV2ResponseMessage, RpcResult
795795
Self {
796796
removed_chain_block_hashes: Arc::new(item.removed_chain_block_hashes.iter().map(|x| RpcHash::from_str(x)).collect::<Result<Vec<_>, _>>()?),
797797
added_chain_block_hashes: Arc::new(item.added_chain_block_hashes.iter().map(|x| RpcHash::from_str(x)).collect::<Result<Vec<_>, _>>()?),
798-
added_acceptance_data: Arc::new(item.added_acceptance_data.iter().map(|x| x.try_into()).collect::<Result<Vec<_>, _>>()?),
798+
chain_block_accepted_transactions: Arc::new(item.chain_block_accepted_transactions.iter().map(|x| x.try_into()).collect::<Result<Vec<_>, _>>()?),
799799
}
800800
});
801801

rpc/grpc/core/src/convert/tx.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::protowire::{self};
22
use crate::{from, try_from};
3-
use kaspa_rpc_core::{FromRpcHex, RpcAddress, RpcError, RpcHash, RpcResult, RpcScriptClass, RpcScriptVec, ToRpcHex};
3+
use kaspa_rpc_core::{
4+
FromRpcHex, RpcAddress, RpcError, RpcHash, RpcOptionalHeader, RpcResult, RpcScriptClass, RpcScriptVec, ToRpcHex,
5+
};
46
use std::str::FromStr;
57

68
// ----------------------------------------------------------------------------
@@ -106,6 +108,13 @@ from!(item: &kaspa_rpc_core::RpcOptionalUtxoEntryVerboseData, protowire::RpcUtxo
106108
}
107109
});
108110

111+
from!(item: &kaspa_rpc_core::RpcChainBlockAcceptedTransactions, protowire::RpcChainBlockAcceptedTransactions, {
112+
Self {
113+
chain_block_header: Some((&item.chain_block_header).into()),
114+
accepted_transactions: item.accepted_transactions.iter().map(protowire::RpcTransaction::from).collect(),
115+
}
116+
});
117+
109118
from!(item: &kaspa_rpc_core::RpcScriptPublicKey, protowire::RpcScriptPublicKey, {
110119
Self { version: item.version().into(), script_public_key: item.script().to_rpc_hex() }
111120
});
@@ -375,6 +384,13 @@ try_from!(item: &protowire::RpcAcceptedTransactionIds, kaspa_rpc_core::RpcAccept
375384
}
376385
});
377386

387+
try_from!(item: &protowire::RpcChainBlockAcceptedTransactions, kaspa_rpc_core::RpcChainBlockAcceptedTransactions, {
388+
Self {
389+
chain_block_header: RpcOptionalHeader::try_from(item.chain_block_header.as_ref().ok_or_else(|| RpcError::MissingRpcFieldError("RpcChainBlockAcceptedTransactions".to_string(), "chain_block_header".to_string()))?)?,
390+
accepted_transactions: item.accepted_transactions.iter().map(kaspa_rpc_core::RpcOptionalTransaction::try_from).collect::<Result<_, _>>()?,
391+
}
392+
});
393+
378394
try_from!(item: &protowire::RpcUtxosByAddressesEntry, kaspa_rpc_core::RpcUtxosByAddressesEntry, {
379395
let address = if item.address.is_empty() { None } else { Some(item.address.as_str().try_into()?) };
380396
Self {

rpc/service/src/service.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,12 +1271,23 @@ NOTE: This error usually indicates an RPC conversion error between the node and
12711271
let added_acceptance_data =
12721272
self.consensus_converter.get_acceptance_data_with_verbosity(&session, &verbosity, &chain_path, Some(batch_size)).await?;
12731273

1274+
let chain_block_accepted_transactions: Vec<_> = added_acceptance_data
1275+
.iter()
1276+
.map(|data| {
1277+
let chain_block_header = data.accepting_chain_header.as_ref().unwrap().clone();
1278+
// Flatten all accepted transactions from mergeset blocks into a single vec
1279+
let accepted_transactions: Vec<RpcOptionalTransaction> =
1280+
data.mergeset_block_acceptance_data.iter().flat_map(|tx| tx.accepted_transactions.clone()).collect();
1281+
1282+
RpcChainBlockAcceptedTransactions { chain_block_header, accepted_transactions }
1283+
})
1284+
.collect();
12741285
chain_path.added.truncate(added_acceptance_data.len());
12751286

12761287
Ok(GetVirtualChainFromBlockV2Response {
12771288
removed_chain_block_hashes: chain_path.removed.into(),
12781289
added_chain_block_hashes: chain_path.added.into(),
1279-
added_acceptance_data: added_acceptance_data.into(),
1290+
chain_block_accepted_transactions: chain_block_accepted_transactions.into(),
12801291
})
12811292
}
12821293

rpc/wrpc/examples/vcc_v2/src/main.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,22 @@ async fn get_vcc_v2() -> Result<()> {
6060
)
6161
.await?;
6262

63-
let mut count_mergeset = 0;
64-
let mut count_block = 0;
6563
let mut seen_tx = HashSet::<RpcHash>::with_capacity(30_000);
6664
let mut count_duplicated_tx = 0;
6765

6866
println!("{:#?}", response);
69-
response.added_acceptance_data.iter().for_each(|acd| {
67+
response.chain_block_accepted_transactions.iter().for_each(|acd| {
7068
// println!("mergeset of {}", acd.accepting_chain_header.as_ref().unwrap().hash.unwrap());
7169

72-
count_mergeset += 1;
73-
acd.mergeset_block_acceptance_data.iter().for_each(|bad| {
74-
// println!("block {}", bad.merged_header.as_ref().unwrap().hash.unwrap());
75-
count_block += 1;
76-
77-
bad.accepted_transactions.iter().for_each(|tx| {
78-
let id = tx.verbose_data.as_ref().unwrap().transaction_id.unwrap();
79-
if seen_tx.contains(&id) {
80-
count_duplicated_tx += 1;
81-
}
82-
seen_tx.insert(id);
83-
});
84-
})
70+
acd.accepted_transactions.iter().for_each(|tx| {
71+
let id = tx.verbose_data.as_ref().unwrap().transaction_id.unwrap();
72+
if seen_tx.contains(&id) {
73+
count_duplicated_tx += 1;
74+
}
75+
seen_tx.insert(id);
76+
});
8577
});
8678

87-
println!("ms: {}, b: {}", count_mergeset, count_block);
8879
println!("duplicated tx {}", count_duplicated_tx);
8980
// Disconnect client from Kaspa node
9081
client.disconnect().await?;

rpc/wrpc/wasm/src/client.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,10 @@ build_wrpc_wasm_bindgen_interface!(
10561056
/// Returned information: None.
10571057
Unban,
10581058
/// Get UTXO Return Addresses.
1059-
GetUtxoReturnAddress
1059+
GetUtxoReturnAddress,
1060+
/// Retrieves the virtual chain corresponding to a specified block hash.
1061+
/// Returned information: Virtual chain information. (Version 2)
1062+
/// May be used to get fully populated transactions
1063+
GetVirtualChainFromBlockV2
10601064
]
10611065
);

0 commit comments

Comments
 (0)