Skip to content

Commit 378be52

Browse files
committed
Package more neatly the encoding / decoding
1 parent 5c8bdfb commit 378be52

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

rsc.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22
from rscoin.rscservice import RSCProtocol, RSCFactory, load_setup, unpackage_commit_response, get_authorities, \
3-
package_query, unpackage_query_response, package_commit, unpackage_commit_response
3+
package_query, unpackage_query_response, package_commit, package_issue, unpackage_commit_response
44

55
import rscoin
66
from base64 import b64encode, b64decode
@@ -393,9 +393,11 @@ def play_another_song(var):
393393
sig = mykey.sign(tx.id())
394394

395395
## Now we test the Commit
396-
tx_ser = tx.serialize()
397-
core = map(b64encode, [tx_ser, mykey.pub.export(), sig])
398-
data = " ".join(["Commit", str(len(core))] + core)
396+
#tx_ser = tx.serialize()
397+
#core = map(b64encode, [tx_ser, mykey.pub.export(), sig])
398+
#data = " ".join(["Commit", str(len(core))] + core)
399+
400+
data = package_issue(tx, [mykey, sig])
399401

400402
if args.mock:
401403
print data

rscoin/rscservice.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def package_query(tx, tx_deps, keys):
4343
dataCore = map(b64encode, items)
4444

4545
H = sha256(" ".join(dataCore)).digest()
46-
data = " ".join(["Query", str(len(dataCore))] + dataCore)
46+
data = " ".join(["xQuery", str(len(dataCore))] + dataCore)
4747

4848
return H, data, dataCore
4949

@@ -57,14 +57,24 @@ def unpackage_query_response(response):
5757

5858
return resp
5959

60+
6061
def package_commit(core, ks_list):
6162
ks_flat = []
6263
for (k, s) in ks_list:
6364
ks_flat += [ k, s ]
6465

65-
data = " ".join(["Commit", str(len(core))] + core + map(b64encode, ks_flat))
66+
data = " ".join(["xCommit", str(len(core))] + core + map(b64encode, ks_flat))
67+
return data
68+
69+
70+
def package_issue(tx, ks):
71+
tx_ser = tx.serialize()
72+
k, s = ks
73+
core = map(b64encode, [tx_ser, k.export()[0], s])
74+
data = " ".join(["xCommit", str(len(core))] + core)
6675
return data
6776

77+
6878
def unpackage_commit_response(response):
6979
resp = response.strip().split(" ")
7080

@@ -75,8 +85,6 @@ def unpackage_commit_response(response):
7585
return resp
7686

7787

78-
79-
8088
class RSCProtocol(LineReceiver):
8189

8290
def __init__(self, factory):
@@ -185,11 +193,12 @@ def handle_Commit(self, items):
185193

186194
def lineReceived(self, line):
187195
""" Simple de-multiplexer """
196+
188197
items = line.split(" ")
189-
if items[0] == "Query":
198+
if items[0] == "xQuery":
190199
return self.handle_Query(items) # Get signatures
191200

192-
if items[0] == "Commit":
201+
if items[0] == "xCommit":
193202
return self.handle_Commit(items) # Seal a transaction
194203

195204
if items[0] == "Ping":

tests/test_rscservice.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import rscoin
1717
from rscoin.rscservice import RSCFactory, load_setup, get_authorities
1818
from rscoin.rscservice import package_query, unpackage_query_response, \
19-
package_commit, unpackage_commit_response
19+
package_commit, package_issue, unpackage_commit_response
2020

2121

2222

@@ -131,12 +131,13 @@ def test_TxCommit(sometx):
131131
for ik in tx3.get_utxo_in_keys():
132132
assert ik in factory.db
133133

134-
data1 = map(b64encode, [tx3.serialize(), tx1.serialize(), tx2.serialize(),
135-
k1.export()[0], k2.export()[0], k1.sign(tx3.id()), k2.sign(tx3.id())])
134+
#data1 = map(b64encode, [tx3.serialize(), tx1.serialize(), tx2.serialize(),
135+
# k1.export()[0], k2.export()[0], k1.sign(tx3.id()), k2.sign(tx3.id())])
136136

137-
H = sha256(" ".join(data1)).digest()
137+
#H = sha256(" ".join(data1)).digest()
138138

139-
data = " ".join(["Query", str(len(data1))] + data1)
139+
#data = " ".join(["Query", str(len(data1))] + data1)
140+
H, data, dataCore = package_query(tx3, [tx1, tx2], [k1, k2])
140141

141142
instance.lineReceived(data)
142143
response = tr.value()
@@ -148,7 +149,9 @@ def test_TxCommit(sometx):
148149

149150
## Now we test the Commit
150151
tr.clear()
151-
data = " ".join(["Commit", str(len(data1))] + data1 + map(b64encode, [k, s]))
152+
# data = " ".join(["Commit", str(len(dataCore))] + dataCore + map(b64encode, [k, s]))
153+
154+
data = package_commit(dataCore, [(k, s)])
152155
instance.lineReceived(data)
153156

154157
flag, pub, sig = tr.value().split(" ")
@@ -161,29 +164,27 @@ def test_TxCommit_Issued(sometx):
161164
(factory, instance, tr), (k1, k2, tx1, tx2, tx3) = sometx
162165

163166
kIssue = rscoin.Key(urandom(32), public=False)
164-
pubIssue = kIssue.pub.export()
165-
factory.special_key = kIssue.id() # Asssign this as the special key
167+
pubIssue = kIssue.export()[0]
166168

167-
k1 = rscoin.Key(urandom(32), public=False)
168-
k1pub = k1.pub.export()
169+
factory.special_key = kIssue.id() # Asssign this as the special key
169170

170171
tx3 = rscoin.Tx([], [rscoin.OutputTx(k1.id(), 250)])
171172

172173
sig1 = kIssue.sign(tx3.id())
173174
assert tx3.check_transaction_utxo([], [pubIssue], [sig1], kIssue.id())
174-
175175
assert tx3.check_transaction([], [pubIssue], [sig1], kIssue.id())
176176

177177
## Now we test the Commit
178-
data1 = map(b64encode, [tx3.serialize(), pubIssue, sig1])
179178

180179
# Ensure the entries are not in before sending message
181180
for k, v in tx3.get_utxo_out_entries():
182181
assert k not in factory.db
183182

184183
# Send message
185184
tr.clear()
186-
data = " ".join(["Commit", str(len(data1))] + data1)
185+
186+
data = package_issue(tx3, [kIssue, sig1])
187+
187188
instance.lineReceived(data)
188189

189190
# Ensure the returned signatures check
@@ -463,7 +464,7 @@ def test_full_client(msg_mass):
463464
def test_commit_error(sometx):
464465
(factory, instance, tr), (k1, k2, tx1, tx2, tx3) = sometx
465466

466-
instance.lineReceived("Commit X Y Z")
467+
instance.lineReceived("xCommit X Y Z")
467468
assert tr.value().strip() == "Error ParsingError"
468469

469470
@pytest.mark.online
@@ -503,8 +504,10 @@ def test_online_issue(sometx):
503504
sig1 = kIssue.sign(tx3.id())
504505

505506
## Now we test the Commit
506-
data1 = map(b64encode, [tx3.serialize(), pubIssue, sig1])
507-
data = " ".join(["Commit", str(len(data1))] + data1)
507+
#data1 = map(b64encode, [tx3.serialize(), pubIssue, sig1])
508+
#data = " ".join(["Commit", str(len(data1))] + data1)
509+
510+
data = package_issue(tx3, [kIssue, sig1])
508511

509512
# test on fake
510513
tr.clear()

0 commit comments

Comments
 (0)