Hashing issue

This commit is contained in:
Mariano Sorgente 2020-02-14 11:48:22 -08:00
parent b92e7bb527
commit 65d789a999
No known key found for this signature in database
GPG Key ID: 0F866338C369278C
3 changed files with 52 additions and 24 deletions

View File

@ -40,8 +40,9 @@ class Program(SExp): # type: ignore # noqa
self.stream(f) # type: ignore # noqa
return f.getvalue()
def __str__(self):
def __str__(self) -> str:
return bytes(self).hex()
def get_hash(self):
def get_hash(self) -> bytes32:
# print("Bytes self", bytes(self))
return bytes32(std_hash(bytes(self)))

View File

@ -1,11 +1,13 @@
import asyncio
from secrets import token_bytes
import pytest
from src.protocols import full_node_protocol
from src.types.peer_info import PeerInfo
from src.util.ints import uint16
from src.util.ints import uint16, uint32
from tests.setup_nodes import setup_two_nodes, test_constants, bt
from tests.wallet_tools import WalletTool
@pytest.fixture(scope="module")
@ -14,21 +16,29 @@ def event_loop():
yield loop
@pytest.fixture
@pytest.fixture(scope="module")
async def two_nodes():
async for _ in setup_two_nodes():
yield _
@pytest.mark.usefixtures("two_nodes")
@pytest.fixture(scope="module")
def wallet_blocks():
num_blocks = 3
wallet_a = WalletTool()
coinbase_puzzlehash = wallet_a.get_new_puzzlehash()
wallet_receiver = WalletTool()
blocks = bt.get_consecutive_blocks(test_constants, num_blocks, [], 10, coinbase_puzzlehash)
return wallet_a, wallet_receiver, blocks
class TestFullNode:
@pytest.mark.asyncio
async def test_new_tip(self, two_nodes):
num_blocks = 3
async def test_new_tip(self, two_nodes, wallet_blocks):
full_node_1, full_node_2, server_1, server_2 = two_nodes
blocks = bt.get_consecutive_blocks(test_constants, num_blocks, [], 10)
_, _, blocks = wallet_blocks
for i in range(1, num_blocks - 1):
for i in range(1, 3):
async for _ in full_node_1.respond_block(
full_node_protocol.RespondBlock(blocks[i])
):
@ -39,17 +49,27 @@ class TestFullNode:
)
await asyncio.sleep(2) # Allow connections to get made
# msgs = []
msgs = [
x
async for x in full_node_1.new_tip(
full_node_protocol.NewTip(
blocks[-1].height, blocks[-1].weight, blocks[-1].header_hash
)
)
]
print("Msgs", msgs)
new_tip_1 = full_node_protocol.NewTip(blocks[-1].height, blocks[-1].weight, blocks[-1].header_hash)
msgs_1 = [x async for x in full_node_1.new_tip(new_tip_1)]
assert len(msgs_1) == 1
assert msgs_1[0].message.data == full_node_protocol.RequestBlock(uint32(3), blocks[-1].header_hash)
new_tip_2 = full_node_protocol.NewTip(blocks[-2].height, blocks[-2].weight, blocks[-2].header_hash)
msgs_2 = [x async for x in full_node_1.new_tip(new_tip_2)]
assert len(msgs_2) == 0
@pytest.mark.asyncio
async def test_new_tip_2(self, two_nodes):
print("Running second test.")
async def test_new_transaction(self, two_nodes, wallet_blocks):
wallet_a, wallet_receiver, blocks = wallet_blocks
receiver_puzzlehash = wallet_receiver.get_new_puzzlehash()
spent_block = blocks[1]
print(spent_block.body.coinbase, receiver_puzzlehash)
spend_bundle = wallet_a.generate_signed_transaction(
1000, receiver_puzzlehash, spent_block.body.coinbase
)
assert spend_bundle is not None
# new_transaction_1 = full_node_protocol.NewTransaction(tx_id, )
print("abc1")

View File

@ -47,6 +47,7 @@ class WalletTool:
pubkey = self.extended_secret_key.public_child(
self.next_address
).get_public_key()
print("Signing with pubkey", pubkey, self.next_address)
self.pubkey_num_lookup[pubkey.serialize()] = self.next_address
self.next_address = self.next_address + 1
return pubkey
@ -67,10 +68,13 @@ class WalletTool:
)
)
def get_keys(self, hash):
def get_keys(self, puzzle_hash):
for child in range(self.next_address):
pubkey = self.extended_secret_key.public_child(child).get_public_key()
if hash == puzzle_for_pk(pubkey.serialize()).get_hash():
print("PK", pubkey, child)
print("Hash", puzzle_hash, puzzle_for_pk(pubkey.serialize()).get_hash())
print("GET puzzle:", puzzle_for_pk(pubkey.serialize()))
if puzzle_hash == puzzle_for_pk(pubkey.serialize()).get_hash():
return (
pubkey,
self.extended_secret_key.private_child(child).get_private_key(),
@ -80,13 +84,16 @@ class WalletTool:
return puzzle_for_pk(pubkey)
def get_new_puzzle(self):
pubkey = self.get_next_public_key().serialize()
pubkey_a = self.get_next_public_key()
pubkey = pubkey_a.serialize()
puzzle = puzzle_for_pk(pubkey)
return puzzle
def get_new_puzzlehash(self):
puzzle = self.get_new_puzzle()
print("Puzzle", puzzle)
puzzlehash = puzzle.get_hash()
print("Puzzle hash:", puzzlehash)
return puzzlehash
def sign(self, value, pubkey):