@@ -13,20 +13,31 @@ def __init__(self, datadir):
13
13
self .blockDB = dbm .ndbm .open (datadir + "/blocks" , 'c' )
14
14
self .currentBlock = 0
15
15
self .headers_map = dict ()
16
-
16
+
17
17
def close (self ):
18
18
self .blockDB .close ()
19
19
20
+ def erase (self , blockhash ):
21
+ del self .blockDB [repr (blockhash )]
22
+
23
+ # lookup an entry and return the item as raw bytes
20
24
def get (self , blockhash ):
21
- serialized_block = None
25
+ value = None
22
26
try :
23
- serialized_block = self .blockDB [repr (blockhash )]
27
+ value = self .blockDB [repr (blockhash )]
24
28
except KeyError :
25
29
return None
26
- f = BytesIO (serialized_block )
27
- ret = CBlock ()
28
- ret .deserialize (f )
29
- ret .calc_sha256 ()
30
+ return value
31
+
32
+ # lookup an entry and return it as a CBlock
33
+ def get_block (self , blockhash ):
34
+ ret = None
35
+ serialized_block = self .get (blockhash )
36
+ if serialized_block is not None :
37
+ f = BytesIO (serialized_block )
38
+ ret = CBlock ()
39
+ ret .deserialize (f )
40
+ ret .calc_sha256 ()
30
41
return ret
31
42
32
43
def get_header (self , blockhash ):
@@ -75,13 +86,16 @@ def add_block(self, block):
75
86
def add_header (self , header ):
76
87
self .headers_map [header .sha256 ] = header
77
88
89
+ # lookup the hashes in "inv", and return p2p messages for delivering
90
+ # blocks found.
78
91
def get_blocks (self , inv ):
79
92
responses = []
80
93
for i in inv :
81
94
if (i .type == 2 ): # MSG_BLOCK
82
- block = self .get (i .hash )
83
- if block is not None :
84
- responses .append (msg_block (block ))
95
+ data = self .get (i .hash )
96
+ if data is not None :
97
+ # Use msg_generic to avoid re-serialization
98
+ responses .append (msg_generic (b"block" , data ))
85
99
return responses
86
100
87
101
def get_locator (self , current_tip = None ):
@@ -90,11 +104,11 @@ def get_locator(self, current_tip=None):
90
104
r = []
91
105
counter = 0
92
106
step = 1
93
- lastBlock = self .get (current_tip )
107
+ lastBlock = self .get_block (current_tip )
94
108
while lastBlock is not None :
95
109
r .append (lastBlock .hashPrevBlock )
96
110
for i in range (step ):
97
- lastBlock = self .get (lastBlock .hashPrevBlock )
111
+ lastBlock = self .get_block (lastBlock .hashPrevBlock )
98
112
if lastBlock is None :
99
113
break
100
114
counter += 1
@@ -111,16 +125,23 @@ def __init__(self, datadir):
111
125
def close (self ):
112
126
self .txDB .close ()
113
127
128
+ # lookup an entry and return the item as raw bytes
114
129
def get (self , txhash ):
115
- serialized_tx = None
130
+ value = None
116
131
try :
117
- serialized_tx = self .txDB [repr (txhash )]
132
+ value = self .txDB [repr (txhash )]
118
133
except KeyError :
119
134
return None
120
- f = BytesIO (serialized_tx )
121
- ret = CTransaction ()
122
- ret .deserialize (f )
123
- ret .calc_sha256 ()
135
+ return value
136
+
137
+ def get_transaction (self , txhash ):
138
+ ret = None
139
+ serialized_tx = self .get (txhash )
140
+ if serialized_tx is not None :
141
+ f = BytesIO (serialized_tx )
142
+ ret = CTransaction ()
143
+ ret .deserialize (f )
144
+ ret .calc_sha256 ()
124
145
return ret
125
146
126
147
def add_transaction (self , tx ):
@@ -136,5 +157,5 @@ def get_transactions(self, inv):
136
157
if (i .type == 1 ): # MSG_TX
137
158
tx = self .get (i .hash )
138
159
if tx is not None :
139
- responses .append (msg_tx ( tx ))
160
+ responses .append (msg_generic ( b"tx" , tx ))
140
161
return responses
0 commit comments