@@ -35,7 +35,6 @@ use error::SPVError;
35
35
use filtercache:: FilterCache ;
36
36
use headercache:: { HeaderCache , HeaderIterator } ;
37
37
use blockfilter:: { BlockFilter , BlockFilterReader , COIN_FILTER , SCRIPT_FILTER } ;
38
- use scriptcache:: ScriptCache ;
39
38
40
39
use hammersbald:: {
41
40
BitcoinAdaptor , HammersbaldAPI , persistent, PRef ,
@@ -60,38 +59,35 @@ pub struct ChainDB {
60
59
heavy : Option < BitcoinAdaptor > ,
61
60
headercache : HeaderCache ,
62
61
filtercache : FilterCache ,
63
- scriptcache : Mutex < ScriptCache > ,
64
62
network : Network ,
65
63
}
66
64
67
65
impl ChainDB {
68
66
/// Create an in-memory database instance
69
- pub fn mem ( network : Network , heavy : bool , server : bool , script_cache_size : usize ) -> Result < ChainDB , SPVError > {
67
+ pub fn mem ( network : Network , heavy : bool , server : bool ) -> Result < ChainDB , SPVError > {
70
68
info ! ( "working with in memory chain db" ) ;
71
69
let light = BitcoinAdaptor :: new ( transient ( 2 ) ?) ;
72
70
let headercache = HeaderCache :: new ( network) ;
73
71
let filtercache = FilterCache :: new ( server) ;
74
- let scriptcache = Mutex :: new ( ScriptCache :: new ( script_cache_size) ) ;
75
72
if heavy {
76
73
let heavy = Some ( BitcoinAdaptor :: new ( transient ( 2 ) ?) ) ;
77
- Ok ( ChainDB { light, heavy, network, headercache, filtercache, scriptcache } )
74
+ Ok ( ChainDB { light, heavy, network, headercache, filtercache } )
78
75
} else {
79
- Ok ( ChainDB { light, heavy : None , network, headercache, filtercache, scriptcache } )
76
+ Ok ( ChainDB { light, heavy : None , network, headercache, filtercache } )
80
77
}
81
78
}
82
79
83
80
/// Create or open a persistent database instance identified by the path
84
- pub fn new ( path : & Path , network : Network , heavy : bool , server : bool , script_cache_size : usize ) -> Result < ChainDB , SPVError > {
81
+ pub fn new ( path : & Path , network : Network , heavy : bool , server : bool ) -> Result < ChainDB , SPVError > {
85
82
let basename = path. to_str ( ) . unwrap ( ) . to_string ( ) ;
86
83
let light = BitcoinAdaptor :: new ( persistent ( ( basename. clone ( ) + ".h" ) . as_str ( ) , 100 , 2 ) ?) ;
87
84
let headercache = HeaderCache :: new ( network) ;
88
85
let filtercache = FilterCache :: new ( server) ;
89
- let scriptcache = Mutex :: new ( ScriptCache :: new ( script_cache_size) ) ;
90
86
if heavy {
91
87
let heavy = Some ( BitcoinAdaptor :: new ( persistent ( ( basename + ".b" ) . as_str ( ) , 1000 , 2 ) ?) ) ;
92
- Ok ( ChainDB { light, heavy, network, headercache, filtercache, scriptcache } )
88
+ Ok ( ChainDB { light, heavy, network, headercache, filtercache } )
93
89
} else {
94
- Ok ( ChainDB { light, heavy : None , network, headercache, filtercache, scriptcache } )
90
+ Ok ( ChainDB { light, heavy : None , network, headercache, filtercache } )
95
91
}
96
92
}
97
93
@@ -322,44 +318,17 @@ impl ChainDB {
322
318
panic ! ( "should not call store block before header is known {}" , block. bitcoin_hash( ) ) ;
323
319
}
324
320
325
- pub fn cache_scripts ( & mut self , block : & Block , height : u32 ) {
326
- let block_id = Arc :: new ( block. bitcoin_hash ( ) ) ;
327
- let mut script_cache = self . scriptcache . lock ( ) . unwrap ( ) ;
328
- for ( i, tx) in block. txdata . iter ( ) . enumerate ( ) {
329
- let tx_nr = i as u32 ;
330
- let txid = tx. txid ( ) ;
331
- for ( idx, output) in tx. output . iter ( ) . enumerate ( ) {
332
- let vout = idx as u32 ;
333
- if !output. script_pubkey . is_provably_unspendable ( ) {
334
- script_cache. insert ( OutPoint { txid, vout} , output. script_pubkey . clone ( ) , height) ;
335
- }
336
- }
337
- }
338
- }
339
-
340
- pub fn get_scripts ( & self , coins : Vec < OutPoint > , mut sofar : Vec < Script > ) -> Result < Vec < Script > , SPVError > {
341
- let mut remains = Vec :: with_capacity ( coins. len ( ) ) ;
342
- {
343
- // check in script cache
344
- let mut scriptcache = self . scriptcache . lock ( ) . unwrap ( ) ;
345
- for coin in coins {
346
- if let Some ( script) = scriptcache. remove ( & coin) {
347
- sofar. push ( script) ;
348
- }
349
- else {
350
- let mut buf = Vec :: new ( ) ;
351
- coin. consensus_encode ( & mut buf) ?;
352
- remains. push ( buf) ;
353
- }
354
- }
355
- }
321
+ pub fn get_scripts ( & self , height : u32 , coins : Vec < OutPoint > , mut sofar : Vec < Script > ) -> Result < Vec < Script > , SPVError > {
322
+ let mut remains = coins. iter ( ) . map ( |coin| {
323
+ let mut buf = Vec :: new ( ) ;
324
+ coin. consensus_encode ( & mut buf) . unwrap ( ) ;
325
+ buf
326
+ } ) . collect :: < Vec < _ > > ( ) ;
356
327
remains. sort ( ) ;
357
328
if remains. len ( ) > 0 {
358
- let from = self . scriptcache . lock ( ) . unwrap ( ) . complete_after ( ) ;
359
-
360
329
let mapped = remains. par_chunks ( 100 ) . map ( |remains| {
361
330
let remains = remains. to_vec ( ) ;
362
- self . resolve_with_filters ( from , remains)
331
+ self . resolve_with_filters ( height , remains)
363
332
} ) . flatten ( ) . collect :: < Vec < _ > > ( ) ;
364
333
sofar. extend ( mapped) ;
365
334
}
@@ -592,11 +561,11 @@ impl<'a> DBScriptAccessor<'a> {
592
561
593
562
594
563
pub trait ScriptAccessor {
595
- fn get_scripts ( & self , coins : Vec < OutPoint > ) -> Result < Vec < Script > , SPVError > ;
564
+ fn get_scripts ( & self , height : u32 , coins : Vec < OutPoint > ) -> Result < Vec < Script > , SPVError > ;
596
565
}
597
566
598
567
impl < ' a > ScriptAccessor for DBScriptAccessor < ' a > {
599
- fn get_scripts ( & self , coins : Vec < OutPoint > ) -> Result < Vec < Script > , SPVError > {
568
+ fn get_scripts ( & self , height : u32 , coins : Vec < OutPoint > ) -> Result < Vec < Script > , SPVError > {
600
569
let mut sofar = Vec :: with_capacity ( coins. len ( ) ) ;
601
570
let mut remains = Vec :: with_capacity ( coins. len ( ) ) ;
602
571
for coin in coins {
@@ -607,6 +576,6 @@ impl<'a> ScriptAccessor for DBScriptAccessor<'a> {
607
576
remains. push ( coin) ;
608
577
}
609
578
}
610
- self . db . get_scripts ( remains, sofar)
579
+ self . db . get_scripts ( height , remains, sofar)
611
580
}
612
581
}
0 commit comments