Skip to content

Commit bb5f156

Browse files
committed
add execute state for transaction payload export
1 parent e720db3 commit bb5f156

File tree

7 files changed

+101
-46
lines changed

7 files changed

+101
-46
lines changed

cmd/db-exporter/src/command_progress.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ use indicatif::{ProgressBar, ProgressStyle};
33
use rayon::prelude::*;
44
use starcoin_chain::{BlockChain, ChainReader};
55
use starcoin_config::ChainNetwork;
6+
use starcoin_crypto::HashValue;
67
use starcoin_genesis::Genesis;
78
use starcoin_storage::cache_storage::CacheStorage;
89
use starcoin_storage::db_storage::DBStorage;
910
use starcoin_storage::storage::StorageInstance;
1011
use starcoin_storage::{Storage, StorageVersion};
1112
use starcoin_types::block::{Block, BlockNumber};
1213
use starcoin_vm_types::language_storage::TypeTag;
14+
use starcoin_vm_types::vm_status::KeptVMStatus;
1315
use std::io::{Seek, SeekFrom};
1416
use std::sync::Arc;
1517
use std::{
@@ -127,6 +129,10 @@ impl ParallelCommandBlockReader for ParallelCommandReadBodyFromExportLine {
127129
})
128130
.collect::<Result<Vec<Block>, _>>()?)
129131
}
132+
133+
fn query_txn_exec_state(&self, _txn_hash: HashValue) -> String {
134+
"OK".to_string()
135+
}
130136
}
131137

132138
pub struct ParallelCommandReadBlockFromDB {
@@ -234,8 +240,21 @@ impl ParallelCommandBlockReader for ParallelCommandReadBlockFromDB {
234240
self.chain.get_block_by_number(*num).ok().flatten()
235241
}
236242
})
237-
.collect::<Vec<Block>>()
238-
)
243+
.collect::<Vec<Block>>())
244+
}
245+
246+
fn query_txn_exec_state(&self, txn_hash: HashValue) -> String {
247+
let txn = self
248+
.chain
249+
.get_transaction_info(txn_hash)
250+
.expect("Query failed!");
251+
match txn {
252+
Some(info) => match info.status {
253+
KeptVMStatus::Executed => "OK".to_string(),
254+
_ => "FALIED".to_string(),
255+
},
256+
None => "CANT_FOUND_TXN".to_string(),
257+
}
239258
}
240259
}
241260

@@ -321,7 +340,7 @@ impl ParallelCommandProgress {
321340
item_vec
322341
.into_iter()
323342
.map(|item| {
324-
let (succeed, failed) = item.execute(command);
343+
let (succeed, failed) = item.execute(self.block_reader.as_ref(), command);
325344
progress_bar.inc(1);
326345
CommandResult::new(succeed, failed.len())
327346
})
@@ -355,7 +374,11 @@ impl ParallelCommandProgress {
355374
}
356375

357376
pub trait ParallelCommand<CommandT, ErrorT> {
358-
fn execute(&self, cmd: &CommandT) -> (usize, Vec<ErrorT>);
377+
fn execute(
378+
&self,
379+
block_reader: &dyn ParallelCommandBlockReader,
380+
cmd: &CommandT,
381+
) -> (usize, Vec<ErrorT>);
359382

360383
fn matched(&self, filter: &Option<ParallelCommandFilter>) -> bool;
361384
}
@@ -365,7 +388,8 @@ pub trait ParallelCommandObserver {
365388
fn after_progress(&self) -> Result<()>;
366389
}
367390

368-
pub trait ParallelCommandBlockReader {
391+
pub trait ParallelCommandBlockReader: Sync + Send {
369392
fn get_progress_interval(&self) -> u64;
370393
fn read(&self, load_bar: &ProgressBar) -> Result<Vec<Block>>;
394+
fn query_txn_exec_state(&self, txn_hash: HashValue) -> String;
371395
}

cmd/db-exporter/src/commands/decode_payload.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::command_progress::{
5-
ParallelCommand, ParallelCommandFilter, ParallelCommandObserver, ParallelCommandProgress,
6-
ParallelCommandReadBlockFromDB,
5+
ParallelCommand, ParallelCommandBlockReader, ParallelCommandFilter, ParallelCommandObserver,
6+
ParallelCommandProgress, ParallelCommandReadBlockFromDB,
77
};
88
use anyhow::Result;
99
use chrono::{DateTime, Utc};
@@ -104,6 +104,7 @@ pub struct CSVHeaders {
104104
func_name: String,
105105
ty_args: String,
106106
args: String,
107+
execute_result: String,
107108
timestamp: u64,
108109
date_time: String,
109110
}
@@ -135,7 +136,11 @@ fn timestamp_to_datetime(timestamp: u64) -> String {
135136
}
136137

137138
impl ParallelCommand<CommandDecodePayload, DecodePayloadCommandError> for Block {
138-
fn execute(&self, command: &CommandDecodePayload) -> (usize, Vec<DecodePayloadCommandError>) {
139+
fn execute(
140+
&self,
141+
block_reader: &dyn ParallelCommandBlockReader,
142+
command: &CommandDecodePayload,
143+
) -> (usize, Vec<DecodePayloadCommandError>) {
139144
// let errors = vec![];
140145
// let mut success_module_size = 0;
141146

@@ -152,6 +157,8 @@ impl ParallelCommand<CommandDecodePayload, DecodePayloadCommandError> for Block
152157
starcoin_abi_decoder::decode_txn_payload(&statedb, txn.payload())
153158
.expect("Decode transaction payload failed!");
154159

160+
let execute_result = block_reader.query_txn_exec_state(txn.hash());
161+
155162
let mut writer = command.writer_mutex.lock().unwrap();
156163
match decoded_txn_payload {
157164
DecodedTransactionPayload::ScriptFunction(payload) => writer
@@ -164,29 +171,41 @@ impl ParallelCommand<CommandDecodePayload, DecodePayloadCommandError> for Block
164171
ty_args: payload
165172
.ty_args
166173
.iter()
167-
.map(|a| a.to_string())
174+
.map(|a| a.to_string().trim_matches('"').to_string())
168175
.collect::<Vec<_>>()
169176
.join("|"),
170177
args: payload
171178
.args
172179
.iter()
173-
.map(|a| a.0.to_string())
180+
.map(|a| a.0.to_string().trim_matches('"').to_string())
174181
.collect::<Vec<_>>()
175182
.join("|"),
176183
timestamp,
184+
execute_result,
177185
date_time: formatted_date.clone(),
178186
})
179187
.expect("Write into CSV failed!"),
180-
DecodedTransactionPayload::Script(_) => writer
188+
DecodedTransactionPayload::Script(script) => writer
181189
.serialize(CSVHeaders {
182190
block_num: block_num.clone(),
183191
txn_hash: txn.hash().to_string(),
184192
txn_type: String::from("Script"),
185193
signer,
186-
func_name: "".to_string(),
187-
ty_args: "".to_string(),
188-
args: "".to_string(),
194+
func_name: String::from(""),
195+
ty_args: script
196+
.ty_args
197+
.iter()
198+
.map(|a| a.to_string())
199+
.collect::<Vec<_>>()
200+
.join("|"),
201+
args: script
202+
.args
203+
.iter()
204+
.map(|a| a.0.to_string().trim_matches('"').to_string())
205+
.collect::<Vec<_>>()
206+
.join("|"),
189207
timestamp,
208+
execute_result,
190209
date_time: formatted_date.clone(),
191210
})
192211
.expect("Write into CSV failed!"),
@@ -200,6 +219,7 @@ impl ParallelCommand<CommandDecodePayload, DecodePayloadCommandError> for Block
200219
ty_args: "".to_string(),
201220
args: "".to_string(),
202221
timestamp,
222+
execute_result,
203223
date_time: formatted_date.clone(),
204224
})
205225
.expect("Write into CSV failed!"),
@@ -219,21 +239,17 @@ impl ParallelCommand<CommandDecodePayload, DecodePayloadCommandError> for Block
219239
};
220240

221241
match filters {
222-
Some(filter) => {
223-
self.transactions().iter().any(|txn| {
224-
match txn.payload() {
225-
TransactionPayload::ScriptFunction(payload) => {
226-
filter.match_signer(&txn.sender().to_string())
227-
&& filter.match_func_name(payload.function().as_str())
228-
&& filter.match_ty_args(payload.ty_args())
229-
&& filter.match_args(payload.args())
230-
},
231-
TransactionPayload::Script(_) | TransactionPayload::Package(_) => {
232-
filter.match_signer(&txn.sender().to_string())
233-
},
234-
}
235-
})
236-
},
242+
Some(filter) => self.transactions().iter().any(|txn| match txn.payload() {
243+
TransactionPayload::ScriptFunction(payload) => {
244+
filter.match_signer(&txn.sender().to_string())
245+
&& filter.match_func_name(payload.function().as_str())
246+
&& filter.match_ty_args(payload.ty_args())
247+
&& filter.match_args(payload.args())
248+
}
249+
TransactionPayload::Script(_) | TransactionPayload::Package(_) => {
250+
filter.match_signer(&txn.sender().to_string())
251+
}
252+
}),
237253
None => true,
238254
}
239255
}
@@ -291,7 +307,7 @@ pub fn do_decode_payload(
291307
#[test]
292308
pub fn test_decode_payload() -> Result<()> {
293309
let input = PathBuf::from("/Users/bobong/.starcoin/main");
294-
let output = PathBuf::from("/Users/bobong/Downloads/STC-DB-mainnet/output.csv");
310+
let output = PathBuf::from("/Users/bobong/Downloads/unit-output.csv");
295311

296312
// do_decode_payload(Main, input.clone(), output.clone(), None, None, None)?;
297313

cmd/db-exporter/src/commands/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod decode_payload;
2+
pub mod verify_header;
3+
pub mod verify_module;

cmd/db-exporter/src/commands/verify_header.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::PathBuf;
77
use std::sync::Arc;
88

99
use crate::command_progress::{
10-
ParallelCommand, ParallelCommandFilter, ParallelCommandProgress,
10+
ParallelCommand, ParallelCommandBlockReader, ParallelCommandFilter, ParallelCommandProgress,
1111
ParallelCommandReadBodyFromExportLine,
1212
};
1313
use starcoin_types::block::Block;
@@ -43,7 +43,11 @@ pub fn verify_header_via_export_file(path: PathBuf, batch_size: usize) -> anyhow
4343
}
4444

4545
impl ParallelCommand<VerifyHeaderCmdType, VerifyHeaderError> for Block {
46-
fn execute(&self, _cmd: &VerifyHeaderCmdType) -> (usize, Vec<VerifyHeaderError>) {
46+
fn execute(
47+
&self,
48+
_reader: &dyn ParallelCommandBlockReader,
49+
_cmd: &VerifyHeaderCmdType,
50+
) -> (usize, Vec<VerifyHeaderError>) {
4751
let ret = G_CRYPTONIGHT.verify_header_difficulty(self.header.difficulty(), &self.header);
4852
match ret {
4953
Ok(_) => (1, vec![]),

cmd/db-exporter/src/commands/verify_module.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
use crate::command_progress::{
2+
ParallelCommand, ParallelCommandBlockReader, ParallelCommandFilter, ParallelCommandProgress,
3+
ParallelCommandReadBodyFromExportLine,
4+
};
15
use clap::Parser;
26
use move_binary_format::errors::Location;
37
use starcoin_crypto::HashValue;
48
use starcoin_types::{block::Block, transaction::TransactionPayload};
59
use starcoin_vm_types::{errors::VMError, file_format::CompiledModule};
610
use std::sync::Arc;
711
use std::{fmt::Debug, path::PathBuf};
8-
//use starcoin_accumulator::node::AccumulatorStoreType::Block;
9-
use crate::command_progress::{
10-
ParallelCommand, ParallelCommandFilter, ParallelCommandProgress,
11-
ParallelCommandReadBodyFromExportLine,
12-
};
1312

1413
#[derive(Debug, Parser)]
1514
#[clap(
@@ -32,7 +31,11 @@ pub struct VerifyModuleError {
3231
pub struct VerifyModulesType;
3332

3433
impl ParallelCommand<VerifyModulesType, VerifyModuleError> for Block {
35-
fn execute(&self, _cmd: &VerifyModulesType) -> (usize, Vec<VerifyModuleError>) {
34+
fn execute(
35+
&self,
36+
_reader: &dyn ParallelCommandBlockReader,
37+
_cmd: &VerifyModulesType,
38+
) -> (usize, Vec<VerifyModuleError>) {
3639
let mut errors = vec![];
3740
let mut success_modules = 0;
3841
let block = self;

cmd/db-exporter/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Copyright (c) The Starcoin Core Contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
pub mod command_decode_payload;
54
pub mod command_progress;
6-
pub mod verify_header;
7-
pub mod verify_module;
5+
pub mod commands;

cmd/db-exporter/src/main.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use anyhow::{bail, format_err, Result};
55
use bcs_ext::{BCSCodec, Sample};
66
use clap::{IntoApp, Parser};
77
use csv::Writer;
8-
use db_exporter::{
9-
command_decode_payload::{do_decode_payload_command, DecodePayloadCommandOptions},
10-
verify_header::{verify_header_via_export_file, VerifyHeaderOptions},
11-
verify_module::{verify_modules_via_export_file, VerifyModuleOptions},
12-
};
8+
// use db_exporter::{
9+
// decode_payload::{do_decode_payload_command, DecodePayloadCommandOptions},
10+
// verify_header::{verify_header_via_export_file, VerifyHeaderOptions},
11+
// verify_module::{verify_modules_via_export_file, VerifyModuleOptions},
12+
// };
1313
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
1414
use serde::{ser::SerializeMap, Serialize, Serializer};
1515
use starcoin_account_api::AccountInfo;
@@ -70,6 +70,12 @@ use std::{
7070
time::SystemTime,
7171
};
7272

73+
use db_exporter::commands::{
74+
decode_payload::{do_decode_payload_command, DecodePayloadCommandOptions},
75+
verify_header::{verify_header_via_export_file, VerifyHeaderOptions},
76+
verify_module::{verify_modules_via_export_file, VerifyModuleOptions},
77+
};
78+
7379
const BLOCK_GAP: u64 = 1000;
7480
const BACK_SIZE: u64 = 10000;
7581
const SNAP_GAP: u64 = 128;
@@ -1887,6 +1893,7 @@ pub fn export_resource(
18871893
let now2 = Instant::now();
18881894
for (k, v) in resource_set.iter() {
18891895
let struct_tag = StructTag::decode(k.as_slice())?;
1896+
println!("struct_tag: {:?}", struct_tag.to_string());
18901897
if struct_tag == resource_struct_tag {
18911898
let annotated_struct =
18921899
value_annotator.view_struct(resource_struct_tag.clone(), v.as_slice())?;

0 commit comments

Comments
 (0)