Bismuth源码剖析(二)—-服务端工作流程

上篇已经介绍了node节点的启动过程,下面介绍下node节点作为服务端需要干什么事情

1、接收对端的链接,判断是否在禁止名单中banlist,然后接收对端发过来的数据
data = connections.receive(self.request, 10)
2、判断数据的内容,如下:

根据接收的data是什么走不同的分支

3、下面看下各个分支的情况

if data == ‘version’:
接收对端发送的版本号,然后判断是否在本机允许的version范围内,如果在则发送ok,如果不在则发送notok
elif data == ‘mempool’:
接收对端的mempool信息,然后调用mempool_merge(segments, peer_ip, c, mempool, m)函数,再查询本机的mempool信息,发送给对端
elif data == ‘hello’:
首先读取peers.txt的内容,然后发送”peers”给对端,接着把查询的peers信息发送给对端,最后发送”sync”给对端
elif data == “sendsync”:
发送”sync”给对端
elif data == “blocksfnd”:
if int(received_block_height) >= block_req:则发送”blockscf”给对端,然后接收对端的响应
else:发送”blocksrj”给对端
最后发送”sync”给对端
elif data == “blockheight”:
received_block_height = connections.receive(self.request, 10) 接收对端的last_block_height
connections.send(self.request, db_block_height, 10) 发送给对端本地的last_block_height
if int(received_block_height) > db_block_height:
发送给对端db_block_hash
elif int(received_block_height) <= db_block_height:
data = connections.receive(self.request, 10) 接收对端的last_block_hash
if db_block_hash == data:
connections.send(self.request, “nonewblk”, 10) 发送”nonewblk”
else:
connections.send(self.request, “blocksfnd”, 10) 发送”blocksfnd”
confirmation = connections.receive(self.request, 10) 接收确认结果
if confirmation == “blockscf”: 如果结果是”blockscf”则发送blocks_send
connections.send(self.request, blocks_send, 10)
elif confirmation == “blocksrj”:
paas
elif data == “nonewblk”:
connections.send(self.request, “sync”, 10)

elif data == “blocknf”:
block_hash_delete = connections.receive(self.request, 10) 接收block_hash_delete
blocknf(block_hash_delete, peer_ip, conn, c, hdd, h, hdd2, h2, backup, b) 执行blocknf函数
connections.send(self.request, “sync”, 10) 给对端发送”sync”

elif data == “block”:
segments = connections.receive(self.request, 10) 接收对端的数据
digest_block(segments, self.request, peer_ip, conn, c, mempool, m, hdd, h, hdd2, h2, h3) 消化对端发送的数据

elif data == “blocklast”:
connections.send(self.request, block_last, 10) 给对端发送本地的block_last

elif data == “blockget”:
block_desired = connections.receive(self.request, 10) 接收对端想要的block信息
connections.send(self.request, block_desired_result, 10) 发送对端想要的block的数据

elif data == “mpinsert”:
mempool_insert = connections.receive(self.request, 10) 接收对端需要插入内存库的数据
connections.send(self.request, “Mempool insert finished”, 10) 插入结束后返回”Mempool insert finished”

elif data == “balanceget”:
balance_address = connections.receive(self.request, 10) 接收对端发送的地址信息
connections.send(self.request, (balance, credit, debit, fees, rewards), 10) 查询该钱包地址的账簿,然后发送给对端

elif data == “mpget”:
connections.send(self.request, mempool_txs, 10)

elif data == “keygen”:
connections.send(self.request, (gen_private_key_readable, gen_public_key_readable, gen_address), 10) 发送给对端private_key,publick_key,address信息,这个地方我一直认为是不安全的,因为从远端获取的这些鉴权文件,如果服务代码更改了进行了保存,则服务端就存在了所有的鉴权文件,账户中的余额就是不安全的

elif data == “addlist”:
address_tx_list = connections.receive(self.request, 10) 接收对端发送的地址tx的信息
connections.send(self.request, result, 10) 发送查询的结果

elif data == “listlim”:
list_limit = connections.receive(self.request, 10) 接收对端需要查询的list_limit信息
connections.send(self.request, result, 10) 发送查询结果给对端

elif data == “addlistlim”:
address_tx_list = connections.receive(self.request, 10) 接收对端的address_tx_list 信息
address_tx_list_limit = connections.receive(self.request, 10) 接收对端的address_tx_list_limit 信息
connections.send(self.request, result, 10) 返回查询的结果

elif data == “aliasget”:
alias_address = connections.receive(self.request, 10) 接收对端查询的alias_address 信息
connections.send(self.request, result, 10) 返回查询结果

elif data == “pubkeyget”:
pub_key_address = connections.receive(self.request, 10) 接收对端需要查询的pub_key_address
connections.send(self.request, target_public_key_hashed, 10) 返回查询结果

elif data == “aliascheck”:
reg_string = connections.receive(self.request, 10)
if registered_already is None and registered_pending is None: 返回”Alias free”
else: 返回”Alias registered”

elif data == “aliasesget”:
alias_addresses = connections.receive(self.request, 10) 接收对端发送的alias_addresses
connections.send(self.request, results, 10) 返回查询的结果

elif data == “txsend”:
tx_remote = connections.receive(self.request, 10) 接收对端发送的tx_remote 信息
connections.send(self.request, str(remote_signature_enc), 10) 返回加密后的签名

elif data == “addvalidate”:
address_validate = connections.receive(self.request, 10) 接收对端需要检验的addr地址
connections.send(self.request, result, 10) 返回校验的结果

elif data == “peersget”:
connections.send(self.request, peers_file , 10) 返回本地的peers信息

elif data == “statusget”:
connections.send(self.request, (revealed_address, nodes_count, nodes_list, threads_count, uptime, consensus, consensus_percentage, VERSION), 10) 返回本地的状态信息

elif data == “diffget”:
connections.send(self.request, diff, 10) 返回难度值

elif data == “difflast”:
connections.send(self.request, difflast, 10) 返回最新的难度值

这一部分主要是接收客户端的链接并进行不同的处理

下一章讲解,node作为客户端的一些动作。