1
1
use crate :: {
2
2
error:: { GeneratorParserError , Result } ,
3
- types:: {
4
- BlockHeightInfo , CoinInfo , CoinSpendInfo , GeneratorAnalysis , GeneratorBlockInfo ,
5
- ParsedBlock , ParsedGenerator ,
6
- } ,
3
+ types:: { BlockHeightInfo , CoinInfo , CoinSpendInfo , GeneratorBlockInfo , ParsedBlock } ,
7
4
} ;
8
5
use chia_bls:: Signature ;
9
6
use chia_consensus:: {
@@ -14,7 +11,7 @@ use chia_consensus::{
14
11
run_block_generator:: { run_block_generator2, setup_generator_args} ,
15
12
validation_error:: { atom, first, next, rest, ErrorCode } ,
16
13
} ;
17
- use chia_protocol:: { Bytes32 , FullBlock } ;
14
+ use chia_protocol:: FullBlock ;
18
15
use chia_traits:: streamable:: Streamable ;
19
16
use clvm_utils:: tree_hash;
20
17
use clvmr:: {
@@ -63,7 +60,7 @@ impl BlockParser {
63
60
. map ( |g| g. len ( ) as u32 ) ;
64
61
65
62
// Extract generator info
66
- let generator_info = block
63
+ let _generator_info = block
67
64
. transactions_generator
68
65
. as_ref ( )
69
66
. map ( |gen| GeneratorBlockInfo {
@@ -101,7 +98,6 @@ impl BlockParser {
101
98
coin_creations,
102
99
has_transactions_generator,
103
100
generator_size,
104
- generator_info,
105
101
} )
106
102
}
107
103
@@ -373,15 +369,15 @@ impl BlockParser {
373
369
// Get created coins from conditions
374
370
let created_coins = self . extract_created_coins ( spend_index, spend_bundle_conditions) ;
375
371
376
- Some ( CoinSpendInfo {
377
- coin : coin_info,
378
- puzzle_reveal,
379
- solution : solution_bytes,
380
- real_data : true ,
381
- parsing_method : "clvm_execution ". to_string ( ) ,
382
- offset : 0 ,
372
+ Some ( CoinSpendInfo :: new (
373
+ coin_info,
374
+ hex :: encode ( puzzle_reveal) ,
375
+ hex :: encode ( solution_bytes) ,
376
+ true ,
377
+ "From transaction generator ". to_string ( ) ,
378
+ 0 ,
383
379
created_coins,
384
- } )
380
+ ) )
385
381
}
386
382
387
383
/// Extract parent coin info from a coin spend node
@@ -464,42 +460,62 @@ impl BlockParser {
464
460
} )
465
461
}
466
462
463
+ /*
467
464
/// Parse generator from hex string
468
465
pub fn parse_generator_from_hex(&self, generator_hex: &str) -> Result<ParsedGenerator> {
469
466
let generator_bytes =
470
- hex:: decode ( generator_hex) . map_err ( |e| GeneratorParserError :: HexDecodingError ( e) ) ?;
467
+ hex::decode(generator_hex).map_err(|e| ParseError ::HexDecodingError(e))?;
471
468
self.parse_generator_from_bytes(&generator_bytes)
472
469
}
473
470
474
471
/// Parse generator from bytes
475
472
pub fn parse_generator_from_bytes(&self, generator_bytes: &[u8]) -> Result<ParsedGenerator> {
476
- let analysis = self . analyze_generator ( generator_bytes) ?;
477
-
473
+ // Create a dummy GeneratorBlockInfo for now
478
474
Ok(ParsedGenerator {
479
- block_info : GeneratorBlockInfo {
480
- prev_header_hash : Bytes32 :: default ( ) ,
481
- transactions_generator : Some ( generator_bytes. to_vec ( ) ) ,
482
- transactions_generator_ref_list : Vec :: new ( ) ,
483
- } ,
475
+ block_info: GeneratorBlockInfo::new(
476
+ [0u8; 32].into (),
477
+ Some(generator_bytes.to_vec()),
478
+ vec![] ,
479
+ ) ,
484
480
generator_hex: Some(hex::encode(generator_bytes)),
485
- analysis,
481
+ analysis: self.analyze_generator(generator_bytes)? ,
486
482
})
487
483
}
488
484
489
485
/// Analyze generator bytecode
490
486
pub fn analyze_generator(&self, generator_bytes: &[u8]) -> Result<GeneratorAnalysis> {
491
487
let size_bytes = generator_bytes.len();
492
- let is_empty = size_bytes == 0 ;
493
-
494
- // Check for CLVM patterns
495
- let contains_clvm_patterns = generator_bytes
496
- . windows ( 2 )
497
- . any ( |w| matches ! ( w, [ 0xff , _] | [ _, 0xff ] ) ) ;
498
-
499
- // Check for coin patterns (CREATE_COIN opcode)
500
- let contains_coin_patterns = generator_bytes. windows ( 1 ) . any ( |w| w[ 0 ] == 0x33 ) ;
488
+ let is_empty = generator_bytes.is_empty();
489
+
490
+ // Check for common CLVM patterns
491
+ let contains_clvm_patterns = generator_bytes.windows(2).any(|w| {
492
+ w == [0x01, 0x00] || // pair
493
+ w == [0x02, 0x00] || // cons
494
+ w == [0x03, 0x00] || // first
495
+ w == [0x04, 0x00] // rest
496
+ });
497
+
498
+ // Check for coin patterns (32-byte sequences)
499
+ let contains_coin_patterns = generator_bytes.len() >= 32;
500
+
501
+ // Calculate simple entropy
502
+ let mut byte_counts = [0u64; 256];
503
+ for &byte in generator_bytes {
504
+ byte_counts[byte as usize] += 1;
505
+ }
501
506
502
- let entropy = self . calculate_entropy ( generator_bytes) ;
507
+ let total = generator_bytes.len() as f64;
508
+ let entropy = if total > 0.0 {
509
+ byte_counts.iter()
510
+ .filter(|&&count| count > 0)
511
+ .map(|&count| {
512
+ let p = count as f64 / total;
513
+ -p * p.log2()
514
+ })
515
+ .sum()
516
+ } else {
517
+ 0.0
518
+ };
503
519
504
520
Ok(GeneratorAnalysis {
505
521
size_bytes,
@@ -509,8 +525,10 @@ impl BlockParser {
509
525
entropy,
510
526
})
511
527
}
528
+ */
512
529
513
530
/// Calculate Shannon entropy of data
531
+ #[ allow( dead_code) ]
514
532
fn calculate_entropy ( & self , data : & [ u8 ] ) -> f64 {
515
533
if data. is_empty ( ) {
516
534
return 0.0 ;
0 commit comments