Skip to content

Commit

Permalink
Basic test to add blocks to node
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano54 authored and Yostra committed Jan 12, 2021
1 parent e874c73 commit e1c1f6c
Show file tree
Hide file tree
Showing 8 changed files with 1,081 additions and 1,147 deletions.
4 changes: 4 additions & 0 deletions src/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ async def _reconsider_peak(self, sub_block: SubBlockRecord, genesis: bool) -> Op
def get_next_difficulty(self, header_hash: bytes32, new_slot: bool) -> uint64:
assert header_hash in self.sub_blocks
curr = self.sub_blocks[header_hash]
if curr.height <= 2:
return self.constants.DIFFICULTY_STARTING
return get_next_difficulty(
self.constants,
self.sub_blocks,
Expand All @@ -294,6 +296,8 @@ def get_next_difficulty(self, header_hash: bytes32, new_slot: bool) -> uint64:
def get_next_slot_iters(self, header_hash: bytes32, new_slot: bool) -> uint64:
assert header_hash in self.sub_blocks
curr = self.sub_blocks[header_hash]
if curr.height <= 2:
return self.constants.SUB_SLOT_ITERS_STARTING
return get_next_sub_slot_iters(
self.constants,
self.sub_blocks,
Expand Down
85 changes: 43 additions & 42 deletions src/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ async def _send_peak_to_timelords(self):
"""
peak_block = await self.blockchain.get_full_peak()
peak = self.blockchain.sub_blocks[peak_block.header_hash]
difficulty = self.blockchain.get_next_difficulty(peak, False)
difficulty = self.blockchain.get_next_difficulty(peak.header_hash, False)
if peak is not None:
ses: Optional[SubEpochSummary] = next_sub_epoch_summary(
self.constants,
self.blockchain.sub_blocks,
self.blockchain.height_to_hash,
peak.signage_point_index,
peak.sub_slot_itsub_slot_iters,
peak.required_iters,
peak_block,
)
timelord_new_peak: timelord_protocol.NewPeak = timelord_protocol.NewPeak(
Expand All @@ -172,18 +172,9 @@ async def _send_peak_to_timelords(self):

async def _on_connect(self, connection: WSChiaConnection):
"""
Whenever we connect to another node / wallet, send them our current heads. Also send heads to farmers
and challenges to timelords.
"""
peak_full: FullBlock = await self.blockchain.get_full_peak()
peak: SubBlockRecord = self.blockchain.sub_blocks[peak_full.header_hash]
request_node = full_node_protocol.NewPeak(
peak.header_hash,
peak.sub_block_height,
peak.weight,
peak.sub_block_height,
peak_full.reward_chain_sub_block.get_unfinished().get_hash(),
)
Whenever we connect to another node / wallet, send them our current heads. Also send heads to farmers
and challenges to timelords.
"""
if connection.connection_type is NodeType.FULL_NODE:
# Send filter to node and request mempool items that are not in it
my_filter = self.mempool_manager.get_filter()
Expand All @@ -192,15 +183,28 @@ async def _on_connect(self, connection: WSChiaConnection):
msg = Message("request_mempool_transactions", mempool_request)
await connection.send_message(msg)

return Message("new_peak", request_node)
elif connection.connection_type is NodeType.WALLET:
# If connected to a wallet, send the LCA
request_wallet = wallet_protocol.NewPeak(
peak.header_hash, peak.sub_block_height, peak.weight, peak.sub_block_height
)
return Message("new_peak", request_wallet)
elif connection.connection_type is NodeType.TIMELORD:
await self._send_peak_to_timelords()
peak_full: Optional[FullBlock] = await self.blockchain.get_full_peak()

if peak_full is not None:
peak: SubBlockRecord = self.blockchain.sub_blocks[peak_full.header_hash]
if connection.connection_type is NodeType.FULL_NODE:
request_node = full_node_protocol.NewPeak(
peak.header_hash,
peak.sub_block_height,
peak.weight,
peak.sub_block_height,
peak_full.reward_chain_sub_block.get_unfinished().get_hash(),
)
return Message("new_peak", request_node)

elif connection.connection_type is NodeType.WALLET:
# If connected to a wallet, send the LCA
request_wallet = wallet_protocol.NewPeak(
peak.header_hash, peak.sub_block_height, peak.weight, peak.sub_block_height
)
return Message("new_peak", request_wallet)
elif connection.connection_type is NodeType.TIMELORD:
await self._send_peak_to_timelords()

async def _on_disconnect(self, connection: WSChiaConnection):
self.log.info("peer disconnected")
Expand Down Expand Up @@ -293,11 +297,8 @@ async def _sync(self):

cur_peers: List[WSChiaConnection] = [
con
for id, con in self.server.all_connections.items()
if (
con.peer_node_id is not None
and con.connection_type == NodeType.FULL_NODE
)
for _, con in self.server.all_connections.items()
if (con.peer_node_id is not None and con.connection_type == NodeType.FULL_NODE)
]

for node_id in cur_peers:
Expand Down Expand Up @@ -398,7 +399,7 @@ async def respond_sub_block(
return

# Adds the block to seen, and check if it's seen before (which means header is in memory)
header_hash = sub_block.header.get_hash()
header_hash = sub_block.foliage_sub_block.get_hash()
if self.blockchain.contains_sub_block(header_hash):
return

Expand All @@ -414,7 +415,7 @@ async def respond_sub_block(
# Tries to add the block to the blockchain
added, error_code, fork_height = await self.blockchain.receive_block(sub_block, False)
if added == ReceiveBlockResult.NEW_PEAK:
await self.mempool_manager.new_peak(await self.blockchain.get_peak())
await self.mempool_manager.new_peak(self.blockchain.get_peak())

if added == ReceiveBlockResult.ALREADY_HAVE_BLOCK:
return
Expand Down Expand Up @@ -469,8 +470,8 @@ async def respond_sub_block(
new_peak: SubBlockRecord = self.blockchain.get_peak()
self.log.info(f"Updated peak to {new_peak} at height {new_peak.height}, " f"forked at {fork_height}")

difficulty = self.blockchain.get_next_difficulty(new_peak, False)
sub_slot_iters = self.blockchain.get_next_slot_iters(new_peak, False)
difficulty = self.blockchain.get_next_difficulty(new_peak.header_hash, False)
sub_slot_iters = self.blockchain.get_next_slot_iters(new_peak.header_hash, False)
self.log.info(f"Difficulty {difficulty} slot iterations {sub_slot_iters}")

sp_sub_slot, ip_sub_slot = await self.blockchain.get_sp_and_ip_sub_slots(sub_block.header_hash)
Expand Down Expand Up @@ -507,22 +508,22 @@ async def respond_sub_block(
sub_block.height,
sub_block.weight,
fork_height,
sub_block.rewardchain_sub_block.get_unfinished().get_hash(),
sub_block.reward_chain_sub_block.get_unfinished().get_hash(),
),
)
self.server.send_to_all([msg], NodeType.FULL_NODE)

# Tell wallets about the new peak
msg = Message(
"new_peak",
wallet_protocol.NewPeak(
sub_block.header_hash,
sub_block.height,
sub_block.weight,
fork_height,
),
)
self.server.send_to_all([msg], NodeType.Wallet)
"new_peak",
wallet_protocol.NewPeak(
sub_block.header_hash,
sub_block.height,
sub_block.weight,
fork_height,
),
)
self.server.send_to_all([msg], NodeType.WALLET)

elif added == ReceiveBlockResult.ADDED_AS_ORPHAN:
self.log.info(f"Received orphan block of height {sub_block.height}")
Expand Down
2 changes: 1 addition & 1 deletion src/full_node/mempool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ async def new_peak(self, new_peak: SubBlockRecord):
"""
Called when a new peak is available, we try to recreate a mempool for the new tip.
"""
if self.peak.header_hash == new_peak.header_hash:
if self.peak == new_peak:
return
self.peak = new_peak

Expand Down
2 changes: 1 addition & 1 deletion src/protocols/full_node_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NewPeak:
header_hash: bytes32
sub_block_height: uint32
weight: uint128
fork_point_with_previous_peak: bytes32
fork_point_with_previous_peak: uint32
unfinished_reward_block_hash: bytes32


Expand Down
2 changes: 1 addition & 1 deletion src/protocols/wallet_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NewPeak:
header_hash: bytes32
sub_block_height: uint32
weight: uint128
fork_point_with_previous_peak: bytes32
fork_point_with_previous_peak: uint32


@dataclass(frozen=True)
Expand Down
19 changes: 10 additions & 9 deletions src/rpc/full_node_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from typing import Callable, List, Optional, Dict

from src.full_node.full_node_api import FullNodeAPI
#from src.types.header import Header

# from src.types.header import Header
from src.types.full_block import FullBlock
from src.util.ints import uint32, uint64, uint128
from src.types.sized_bytes import bytes32
Expand All @@ -21,14 +22,14 @@ def __init__(self, api: FullNode):

def get_routes(self) -> Dict[str, Callable]:
return {
"/get_blockchain_state": self.get_blockchain_state,
"/get_block": self.get_block,
"/get_header_by_height": self.get_header_by_height,
"/get_header": self.get_header,
"/get_unfinished_block_headers": self.get_unfinished_block_headers,
"/get_network_space": self.get_network_space,
"/get_unspent_coins": self.get_unspent_coins,
"/get_heaviest_block_seen": self.get_heaviest_block_seen,
# "/get_blockchain_state": self.get_blockchain_state,
# "/get_block": self.get_block,
# "/get_header_by_height": self.get_header_by_height,
# "/get_header": self.get_header,
# "/get_unfinished_block_headers": self.get_unfinished_block_headers,
# "/get_network_space": self.get_network_space,
# "/get_unspent_coins": self.get_unspent_coins,
# "/get_heaviest_block_seen": self.get_heaviest_block_seen,
}

async def _state_changed(self, change: str) -> List[Dict]:
Expand Down
10 changes: 6 additions & 4 deletions src/server/start_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@


# See: https://bugs.python.org/issue29288
u"".encode("idna")
"".encode("idna")

SERVICE_NAME = "full_node"


def service_kwargs_for_full_node(
root_path: pathlib.Path, config: Dict, consensus_constants: ConsensusConstants
) -> Dict:
full_node = FullNode(
config, root_path=root_path, consensus_constants=consensus_constants
)
full_node = FullNode(config, root_path=root_path, consensus_constants=consensus_constants)
api = FullNodeAPI(full_node)

upnp_list = []
if config["enable_upnp"]:
upnp_list = [config["port"]]

kwargs = dict(
root_path=root_path,
node=api.full_node,
Expand Down
Loading

0 comments on commit e1c1f6c

Please sign in to comment.