def process_request(self, key, id, seq, req, view): self.transaction_history[seq] = req self.total_tx += 1 msg = req.inner.msg src = req.inner.id type = msg[:4] if not type in ["DEPT", "WDRL"]: m = message.add_sig(key, id, seq, view, "RESP", "INVALID", req.inner.timestamp) return m amount = int(msg[4:12]) if type == "WDRL": # withdraw if amount > self.balance: # we do not want the account to be negative amount = self.balance self.balance = self.balance - amount m = message.add_sig(key, id, seq, view, "RESP", "APPROVED", req.inner.timestamp) return m if type == "DEPT": if amount > 100: # we do not support more than 100 usd deposit at a time m = message.add_sig(key, id, seq, view, "RESP", "INVALID", req.inner.timestamp) # print "transfer request invalid" return m else: self.balance = self.balance + amount m = message.add_sig(key, id, seq, view, "RESP", "APPROVED", req.inner.timestamp) # print "transfer request approved" return m
def process_request(self, key, id, seq, req): self.transaction_history[seq] = req self.total_tx += 1 #print "EXECUTED TX #", self.total_tx msg = req.inner.msg src = req.inner.id type = msg[:4] if type != "TRAN": m = message.add_sig(key, id, seq, req.inner.view, "RESP", "INVALID", req.inner.timestamp) return m ammount = int(msg[4:8]) dest = int(msg[8:12]) if type == "TRAN": if ammount > self.accounts[src]: m = message.add_sig(key, id, seq, req.inner.view, "RESP", "INVALID", req.inner.timestamp) #print "transfer request invalid" return m else: self.accounts[src] = self.accounts[src] - ammount self.accounts[dest] = self.accounts[dest] + ammount m = message.add_sig(key, id, seq, req.inner.view, "RESP", "APPROVED", req.inner.timestamp) #print "transfer request approved" return m
def prepare_client_requests(): global counter counter += 1 amount = random.randint(100, 300) msg = payload msg += msg * 1024 key = signing_key temp = message.add_sig(key, id, counter + 1, 0, "REQU", msg, counter) size = temp.ByteSize() b = struct.pack("!I", size) return b + temp.SerializeToString()
def gen_requests(max_requests, batch_size, f): client_key_pub = ecdsa_sig.get_asymm_key(client_id-1, ktype='verify') client_key_pem = ecdsa_sig.get_asymm_key(client_id-1, ktype='sign') for i in range(max_requests): # print("for request: [%s]" % i) r = random.randint(1, max_requests) r2 = random.randint(max_requests/2, max_requests) # amount = random.randint(0,100) amount = 50 msg = "TRAN%s%s%s" % ( str(amount).zfill(4), str(r2).zfill(4), client_key_pub.to_string().decode('latin1') ) if i == 0: print(len(client_key_pub.to_string()), "ECDSA") current_key = ecdsa_sig.get_asymm_key(r%N, ktype='sign') # import pdb; pdb.set_trace() req = message.add_sig(current_key, r, 0, 0, "REQU", msg.encode('utf-8'), i) req.sig = client_key_pem.to_string() msg = req.SerializeToString() if i == 0: print("one request:", len(msg)) ################################ #padding = "0123456789" * 10 #msg += padding * 1024 #msg += "x" * (400 - len(msg)) msg += msg * (batch_size-1) ################################ temp = message.add_sig(current_key, r, 0, 0, "REQU", msg, i+offset) size = temp.ByteSize() if i == 0: print("inner message:", len(msg)) print("message byte size:", size) b = struct.pack("!I", size) f.write(b+temp.SerializeToString()) # if i % batch_size == 0: s = "Generation Progress: {:.1%}".format(i/max_requests) print(s, end='') backspace(len(s))
def create_request(self, req_type, seq, msg, outer_req=None): key = self.key_dict[self.id] m = message.add_sig(key, self.id, seq, self.view, req_type, msg) if outer_req: m.outer = outer_req.SerializeToString() return m