1
1
use std:: {
2
- collections:: { HashMap , VecDeque } ,
2
+ collections:: HashMap ,
3
3
io:: { Error , ErrorKind , Result } ,
4
4
sync:: Arc ,
5
5
time,
@@ -23,11 +23,15 @@ use crate::{
23
23
block:: { self , state:: State } ,
24
24
chain:: { self , tx:: Transaction , vm:: Vm } ,
25
25
genesis:: Genesis ,
26
+ mempool,
26
27
} ;
27
28
28
29
const PUBLIC_API_ENDPOINT : & str = "/public" ;
29
30
const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
30
31
32
+ // TODO: make configurable
33
+ const MEMPOOL_SIZE : usize = 1024 ;
34
+
31
35
pub struct ChainVmInterior {
32
36
pub ctx : Option < rpcchainvm:: context:: Context > ,
33
37
pub bootstrapped : bool ,
@@ -59,29 +63,29 @@ impl Default for ChainVmInterior {
59
63
#[ derive( Clone ) ]
60
64
pub struct ChainVm {
61
65
pub db : Box < dyn rpcchainvm:: database:: Database + Sync + Send > ,
62
- pub mempool : Arc < RwLock < VecDeque < chain :: tx :: tx :: Transaction > > > ,
66
+ pub mempool : Arc < RwLock < mempool :: Mempool > > ,
63
67
pub inner : Arc < RwLock < ChainVmInterior > > ,
64
- pub verified_blocks : Arc < RwLock < HashMap < ids:: Id , crate :: block:: Block > > > ,
68
+ pub verified_blocks : Arc < RwLock < HashMap < ids:: Id , block:: Block > > > ,
65
69
}
66
70
67
71
impl ChainVm {
68
72
/// Returns initialized ChainVm Boxed as rpcchainvm::vm::Vm trait.
69
73
pub fn new ( ) -> Box < dyn rpcchainvm:: vm:: Vm + Send + Sync > {
70
74
let inner = Arc :: new ( RwLock :: new ( ChainVmInterior :: default ( ) ) ) ;
71
75
let db = rpcchainvm:: database:: memdb:: Database :: new ( ) ;
72
- let mempool = Arc :: new ( RwLock :: new ( VecDeque :: new ( ) ) ) ;
76
+ let mempool = mempool :: Mempool :: new ( MEMPOOL_SIZE ) ;
73
77
let verified_blocks = Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ;
74
78
75
79
Box :: new ( ChainVm {
76
80
db,
77
81
inner,
78
- mempool,
82
+ mempool : Arc :: new ( RwLock :: new ( mempool ) ) ,
79
83
verified_blocks,
80
84
} )
81
85
}
82
86
83
87
pub fn new_with_state ( db : & Box < dyn rpcchainvm:: database:: Database + Sync + Send > ) -> Self {
84
- let mempool = Arc :: new ( RwLock :: new ( VecDeque :: new ( ) ) ) ;
88
+ let mempool = mempool :: Mempool :: new ( MEMPOOL_SIZE ) ;
85
89
let verified_blocks = & Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ;
86
90
let inner = ChainVmInterior {
87
91
ctx : None ,
@@ -97,7 +101,7 @@ impl ChainVm {
97
101
Self {
98
102
db : db. clone ( ) ,
99
103
inner : Arc :: new ( RwLock :: new ( inner) ) ,
100
- mempool,
104
+ mempool : Arc :: new ( RwLock :: new ( mempool ) ) ,
101
105
verified_blocks : Arc :: clone ( verified_blocks) ,
102
106
}
103
107
}
@@ -112,7 +116,7 @@ impl crate::chain::vm::Vm for ChainVm {
112
116
return vm. bootstrapped ;
113
117
}
114
118
115
- async fn submit ( & self , mut txs : Vec < crate :: chain:: tx:: tx:: Transaction > ) -> Result < ( ) > {
119
+ async fn submit ( & self , mut txs : Vec < chain:: tx:: tx:: Transaction > ) -> Result < ( ) > {
116
120
let now = Utc :: now ( ) . timestamp ( ) as u64 ;
117
121
118
122
// TODO append errors instead of fail
@@ -128,8 +132,11 @@ impl crate::chain::vm::Vm for ChainVm {
128
132
tx. execute ( self . db . clone ( ) , dummy_block)
129
133
. await
130
134
. map_err ( |e| Error :: new ( ErrorKind :: Other , e. to_string ( ) ) ) ?;
135
+
131
136
let mut mempool = self . mempool . write ( ) . await ;
132
- mempool. push_front ( tx. to_owned ( ) ) ;
137
+ let _ = mempool
138
+ . add ( tx. to_owned ( ) )
139
+ . map_err ( |e| Error :: new ( ErrorKind :: Other , e. to_string ( ) ) ) ?;
133
140
}
134
141
Ok ( ( ) )
135
142
}
@@ -429,7 +436,7 @@ impl rpcchainvm::snowman::block::ChainVm for ChainVm {
429
436
async fn build_block (
430
437
& self ,
431
438
) -> Result < Box < dyn rpcchainvm:: concensus:: snowman:: Block + Send + Sync > > {
432
- let mut mempool = self . mempool . write ( ) . await ;
439
+ let mempool = self . mempool . read ( ) . await ;
433
440
if mempool. len ( ) == 0 {
434
441
return Err ( Error :: new ( ErrorKind :: Other , "no pending blocks" ) ) ;
435
442
}
0 commit comments