mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-19 13:27:35 +02:00
test compact block rpcs
This commit is contained in:
parent
c3cbe6e203
commit
c3fe935f91
1 changed files with 36 additions and 18 deletions
|
|
@ -68,28 +68,45 @@ class BlockSignTest(test_framework.BitcoinTestFramework):
|
||||||
# Have every node import its block signing private key.
|
# Have every node import its block signing private key.
|
||||||
for i in range(self.num_nodes):
|
for i in range(self.num_nodes):
|
||||||
self.nodes[i].importprivkey(self.wifs[i])
|
self.nodes[i].importprivkey(self.wifs[i])
|
||||||
if i + 1 < self.num_nodes:
|
self.is_network_split = True
|
||||||
util.connect_nodes_bi(self.nodes, i, i + 1)
|
|
||||||
else:
|
|
||||||
util.connect_nodes_bi(self.nodes, 0, i)
|
|
||||||
self.is_network_split = False
|
|
||||||
self.sync_all()
|
|
||||||
|
|
||||||
def check_height(self, expected_height):
|
def check_height(self, expected_height):
|
||||||
for n in self.nodes:
|
for n in self.nodes:
|
||||||
util.assert_equal(n.getblockcount(), expected_height)
|
util.assert_equal(n.getblockcount(), expected_height)
|
||||||
|
|
||||||
def mine_block(self):
|
def mine_block(self, make_transactions):
|
||||||
# mine block in round robin sense: depending on the block number, a node
|
# mine block in round robin sense: depending on the block number, a node
|
||||||
# is selected to create the block, others sign it and the selected node
|
# is selected to create the block, others sign it and the selected node
|
||||||
# broadcasts it
|
# broadcasts it
|
||||||
mineridx = self.nodes[0].getblockcount() % self.num_nodes # assuming in sync
|
mineridx = self.nodes[0].getblockcount() % self.num_nodes # assuming in sync
|
||||||
|
mineridx_next = (self.nodes[0].getblockcount() + 1) % self.num_nodes
|
||||||
miner = self.nodes[mineridx]
|
miner = self.nodes[mineridx]
|
||||||
|
miner_next = self.nodes[mineridx_next]
|
||||||
blockcount = miner.getblockcount()
|
blockcount = miner.getblockcount()
|
||||||
|
|
||||||
|
# Make a few transactions to make non-empty blocks for compact transmission
|
||||||
|
if make_transactions:
|
||||||
|
for i in range(5):
|
||||||
|
miner.sendtoaddress(miner_next.getnewaddress(), int(miner.getbalance()["bitcoin"]/10), "", "", True)
|
||||||
# miner makes a block
|
# miner makes a block
|
||||||
block = miner.getnewblockhex()
|
block = miner.getnewblockhex()
|
||||||
|
|
||||||
|
# other nodes get fed compact blocks
|
||||||
|
for i in range(self.required_signers):
|
||||||
|
if i == mineridx:
|
||||||
|
continue
|
||||||
|
sketch = miner.getcompactsketch(block)
|
||||||
|
compact_response = self.nodes[i].consumecompactsketch(sketch)
|
||||||
|
if make_transactions:
|
||||||
|
block_txn = self.nodes[i].consumegetblocktxn(block, compact_response["block_tx_req"])
|
||||||
|
final_block = self.nodes[i].finalizecompactblock(sketch, block_txn, compact_response["found_transactions"])
|
||||||
|
else:
|
||||||
|
# If there's only coinbase, it should succeed immediately
|
||||||
|
final_block = compact_response["blockhex"]
|
||||||
|
# Block should be complete, sans signatures
|
||||||
|
self.nodes[i].testproposedblock(final_block)
|
||||||
|
|
||||||
|
|
||||||
# collect required_signers signatures
|
# collect required_signers signatures
|
||||||
sigs = []
|
sigs = []
|
||||||
for i in range(self.required_signers):
|
for i in range(self.required_signers):
|
||||||
|
|
@ -99,28 +116,29 @@ class BlockSignTest(test_framework.BitcoinTestFramework):
|
||||||
self.check_height(blockcount)
|
self.check_height(blockcount)
|
||||||
sigs.append(self.nodes[i].signblock(block))
|
sigs.append(self.nodes[i].signblock(block))
|
||||||
|
|
||||||
# miner submits
|
|
||||||
result = miner.combineblocksigs(block, sigs)
|
result = miner.combineblocksigs(block, sigs)
|
||||||
util.assert_equal(result["complete"], True)
|
util.assert_equal(result["complete"], True)
|
||||||
miner.submitblock(result["hex"])
|
|
||||||
|
# All must submit... we're not connected!
|
||||||
|
for node in self.nodes:
|
||||||
|
node.submitblock(result["hex"])
|
||||||
|
|
||||||
def mine_blocks(self, num_blocks):
|
def mine_blocks(self, num_blocks):
|
||||||
for i in range(num_blocks):
|
for i in range(num_blocks):
|
||||||
self.mine_block()
|
self.mine_block(True)
|
||||||
self.sync_all()
|
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
self.check_height(0)
|
self.check_height(0)
|
||||||
|
|
||||||
# mine a block
|
# mine a block with no transactions
|
||||||
self.mine_block()
|
print("Mining and signing a single empty block")
|
||||||
self.sync_all()
|
self.mine_block(False)
|
||||||
|
|
||||||
# mine blocks
|
# mine blocks with transactions
|
||||||
self.mine_blocks(100)
|
print("Mining and signing non-empty blocks")
|
||||||
self.sync_all()
|
self.mine_blocks(10)
|
||||||
|
|
||||||
self.check_height(101)
|
self.check_height(11)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
BlockSignTest(num_nodes=9, required_signers=7).main()
|
BlockSignTest(num_nodes=9, required_signers=7).main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue