Skip to content

Commit

Permalink
fix timestamp error
Browse files Browse the repository at this point in the history
  • Loading branch information
welbon committed Feb 20, 2024
1 parent 985d02d commit 5cb6a56
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 44 deletions.
90 changes: 60 additions & 30 deletions cmd/db-exporter/src/command_decode_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::command_progress::{
ParallelCommandReadBlockFromDB,
};
use anyhow::Result;
use chrono::{TimeZone, Utc};
use chrono::{DateTime, TimeZone, Utc};

Check failure on line 9 in cmd/db-exporter/src/command_decode_payload.rs

View workflow job for this annotation

GitHub Actions / build and test

unused import: `TimeZone`
use clap::Parser;
use csv::{Writer, WriterBuilder};
use move_binary_format::errors::{Location, PartialVMError};
use serde::Serialize;
use starcoin_abi_decoder;
use starcoin_abi_decoder::DecodedTransactionPayload;
use starcoin_config::BuiltinNetworkID::Barnard;
use starcoin_config::BuiltinNetworkID::{Barnard, Main};

Check failure on line 16 in cmd/db-exporter/src/command_decode_payload.rs

View workflow job for this annotation

GitHub Actions / build and test

unused import: `Barnard`
use starcoin_config::ChainNetwork;
use starcoin_crypto::{hash::CryptoHash, HashValue};
use starcoin_statedb::ChainStateDB;
Expand All @@ -22,6 +22,7 @@ use starcoin_types::{block::Block, transaction::TransactionPayload};
use starcoin_vm_types::errors::VMError;
use std::fs::File;
use std::sync::{Arc, Mutex};
use std::time::{Duration, UNIX_EPOCH};
use std::{fmt::Debug, path::PathBuf};

const DECODE_PAYLOAD_COMMAND_NAME: &str = "decode_payload_command";
Expand Down Expand Up @@ -96,6 +97,7 @@ impl DecodePayloadCommandError {
pub struct CSVHeaders {
txn_hash: String,
signer: String,
txn_type: String,
func_name: String,
ty_args: String,
args: String,
Expand All @@ -119,13 +121,21 @@ impl ParallelCommandObserver for CommandDecodePayload {
}
}

fn timestamp_to_datetime(timestamp: u64) -> String {
// Creates a new SystemTime from the specified number of whole seconds
let d = UNIX_EPOCH + Duration::from_secs(timestamp);
// Create DateTime from SystemTime
let datetime = DateTime::<Utc>::from(d);
// Formats the combined date and time with the specified format string.
datetime.format("%Y-%m-%d %H:%M:%S.%f").to_string()
}

impl ParallelCommand<CommandDecodePayload, DecodePayloadCommandError> for Block {
fn execute(&self, command: &CommandDecodePayload) -> (usize, Vec<DecodePayloadCommandError>) {
// let errors = vec![];
// let mut success_module_size = 0;

let datetime = Utc.timestamp_opt(self.header.timestamp() as i64, 0);
let formatted_date = datetime.unwrap().format("%Y-%m-%d %H:%M:%s").to_string();
let formatted_date = timestamp_to_datetime(self.header.timestamp() / 1000);

let root = self.header.state_root();
let statedb = ChainStateDB::new(command.storage.clone(), Some(root));
Expand All @@ -136,31 +146,51 @@ impl ParallelCommand<CommandDecodePayload, DecodePayloadCommandError> for Block
starcoin_abi_decoder::decode_txn_payload(&statedb, txn.payload())
.expect("Decode transaction payload failed!");

let mut writer = command.writer_mutex.lock().unwrap();
match decoded_txn_payload {
DecodedTransactionPayload::ScriptFunction(payload) => {
let mut writer = command.writer_mutex.lock().unwrap();
writer
.serialize(CSVHeaders {
txn_hash: txn.hash().to_string(),
signer,
func_name: format!("{}::{}", payload.module, payload.function),
ty_args: payload
.ty_args
.iter()
.map(|a| a.to_string())
.collect::<Vec<_>>()
.join(","),
args: payload
.args
.iter()
.map(|a| a.0.to_string())
.collect::<Vec<_>>()
.join(","),
timestamp: formatted_date.clone(),
})
.expect("Write into CSV failed!")
}
DecodedTransactionPayload::Script(_) | DecodedTransactionPayload::Package(_) => (),
DecodedTransactionPayload::ScriptFunction(payload) => writer
.serialize(CSVHeaders {
txn_hash: txn.hash().to_string(),
txn_type: String::from("ScriptFunction"),
signer,
func_name: format!("{}::{}", payload.module, payload.function),
ty_args: payload
.ty_args
.iter()
.map(|a| a.to_string())
.collect::<Vec<_>>()
.join("|"),
args: payload
.args
.iter()
.map(|a| a.0.to_string())
.collect::<Vec<_>>()
.join("|"),
timestamp: formatted_date.clone(),
})
.expect("Write into CSV failed!"),
DecodedTransactionPayload::Script(script) => writer

Check failure on line 172 in cmd/db-exporter/src/command_decode_payload.rs

View workflow job for this annotation

GitHub Actions / build and test

unused variable: `script`
.serialize(CSVHeaders {
txn_hash: txn.hash().to_string(),
txn_type: String::from("Script"),
signer,
func_name: "".to_string(),
ty_args: "".to_string(),
args: "".to_string(),
timestamp: formatted_date.clone(),
})
.expect("Write into CSV failed!"),
DecodedTransactionPayload::Package(package) => writer

Check failure on line 183 in cmd/db-exporter/src/command_decode_payload.rs

View workflow job for this annotation

GitHub Actions / build and test

unused variable: `package`
.serialize(CSVHeaders {
txn_hash: txn.hash().to_string(),
txn_type: String::from("Package"),
signer,
func_name: "".to_string(),
ty_args: "".to_string(),
args: "".to_string(),
timestamp: formatted_date.clone(),
})
.expect("Write into CSV failed!"),
}
}
//(success_module_size, errors)
Expand Down Expand Up @@ -201,7 +231,7 @@ pub fn decode_payload(

let (dbreader, storage) = ParallelCommandReadBlockFromDB::new(
input_path,
ChainNetwork::from(Barnard),
ChainNetwork::from(Main),
start_height.unwrap_or(0),
end_height.unwrap_or(0),
)?;
Expand All @@ -223,7 +253,7 @@ pub fn decode_payload(
#[test]
pub fn test_decode_payload() -> Result<()> {
decode_payload(
PathBuf::from("~/.starcoin/barnard"),
PathBuf::from("/Users/bobong/.starcoin/main"),
PathBuf::from("/Users/bobong/Downloads/STC-DB-mainnet/output.csv"),
None,
None,
Expand Down
55 changes: 41 additions & 14 deletions cmd/db-exporter/src/command_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ use starcoin_storage::storage::StorageInstance;
use starcoin_storage::{Storage, StorageVersion};
use starcoin_types::block::{Block, BlockNumber};
use starcoin_vm_types::language_storage::TypeTag;
use std::sync::Arc;
use std::{fs::File, io::{BufRead, BufReader}, path::PathBuf, time::SystemTime};
use std::io::{Seek, SeekFrom};
use std::sync::Arc;
use std::{
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
time::SystemTime,
};

struct CommandResult {
succeed: usize,
Expand Down Expand Up @@ -78,7 +83,6 @@ pub struct ParallelCommandReadBodyFromExportLine {
}

impl ParallelCommandReadBodyFromExportLine {

fn count_lines(reader: &mut BufReader<File>) -> Result<u64> {
let line_count = reader.lines().count();
reader.seek(SeekFrom::Start(0))?;
Expand All @@ -87,11 +91,10 @@ impl ParallelCommandReadBodyFromExportLine {

pub fn new(input_path: PathBuf) -> Result<Self> {
let file = File::open(input_path.display().to_string())?;
let line_count = ParallelCommandReadBodyFromExportLine::count_lines(&mut BufReader::new(file.try_clone()?))?;
Ok(Self {
file,
line_count,
})
let line_count = ParallelCommandReadBodyFromExportLine::count_lines(&mut BufReader::new(
file.try_clone()?,
))?;
Ok(Self { file, line_count })
}
}

Expand Down Expand Up @@ -129,22 +132,27 @@ impl ParallelCommandReadBlockFromDB {
let (chain_info, _) =
Genesis::init_and_check_storage(&net, storage.clone(), input_path.as_ref())
.expect("Failed init_and_check_storage");
let chain = BlockChain::new(net.time_service(), chain_info.head().id(), storage.clone(), None)
.expect("Failed to initialize block chain");
let chain = BlockChain::new(
net.time_service(),
chain_info.head().id(),
storage.clone(),
None,
)
.expect("Failed to initialize block chain");

let cur_num = chain.status().head().number();

let (start_num, end_num) = if start != 0 && end == 0 {
let (start_num, end_num) = if start == 0 && end == 0 {
(0, cur_num)
} else {
let end = if cur_num > end + BLOCK_GAP {
let final_end = if cur_num > end + BLOCK_GAP {
end
} else if cur_num > BLOCK_GAP {
cur_num - BLOCK_GAP
} else {
end
};
(start, end)
(start, final_end)
};

if start > cur_num || start > end {
Expand Down Expand Up @@ -291,7 +299,6 @@ impl ParallelCommandProgress {
.collect::<Vec<CommandResult>>()
})
.collect::<Vec<Vec<CommandResult>>>();

let result = excution_result.into_iter().flatten().fold(
CommandResult {
succeed: 0,
Expand All @@ -302,6 +309,26 @@ impl ParallelCommandProgress {
failed: acc.failed + result.failed,
},
);
//
// let excution_result = all_items
// .iter()
// .map(|item| {
// let (succeed, failed) = item.execute(command);
// progress_bar.inc(1);
// CommandResult::new(succeed, failed.len())
// })
// .collect::<Vec<CommandResult>>();
//
// let result = excution_result.into_iter().fold(
// CommandResult {
// succeed: 0,
// failed: 0,
// },
// |acc, result| CommandResult {
// succeed: acc.succeed + result.succeed,
// failed: acc.failed + result.failed,
// },
// );

progress_bar.finish();

Expand Down

0 comments on commit 5cb6a56

Please sign in to comment.