Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from src.types.proof_of_time import ProofOfTime
from src.types.sized_bytes import bytes32
from src.util.errors import NoProofsOfSpaceFound
from src.util.ints import uint8, uint32, uint64
# Can't go much lower than 19, since plots start having no solutions
k: uint8 = uint8(19)
# Uses many plots for testing, in order to guarantee proofs of space at every height
num_plots = 80
# Use the empty string as the seed for the private key
pool_sk: PrivateKey = PrivateKey.from_seed(b"")
pool_pk: PublicKey = pool_sk.get_public_key()
plot_sks: List[PrivateKey] = [
PrivateKey.from_seed(pn.to_bytes(4, "big")) for pn in range(num_plots)
]
plot_pks: List[PublicKey] = [sk.get_public_key() for sk in plot_sks]
farmer_sk: PrivateKey = PrivateKey.from_seed(b"coinbase")
coinbase_target = sha256(bytes(farmer_sk.get_public_key())).digest()
fee_target = sha256(bytes(farmer_sk.get_public_key())).digest()
n_wesolowski = uint8(3)
class BlockTools:
"""
Tools to generate blocks for testing.
"""
def __init__(self):
plot_seeds: List[bytes32] = [
ProofOfSpace.calculate_plot_seed(pool_pk, plot_pk) for plot_pk in plot_pks
]
def aggregate_pubkeys(pubkeys: Sequence[BLSPubkey]) -> BLSPubkey:
if len(pubkeys) == 0:
return EMPTY_PUBKEY
pubkeys_chia = [
_pubkey_from_bytes(pubkey)
for pubkey in pubkeys
]
aggregated_pubkey_chia = PublicKey.aggregate_insecure(pubkeys_chia)
return cast(BLSPubkey, aggregated_pubkey_chia.serialize())
from src.types.proof_of_space import ProofOfSpace
from src.types.sized_bytes import bytes32
from src.util.cbor_message import cbor_message
from src.util.ints import uint8
from src.util.streamable import List
"""
Protocol between harvester and farmer.
"""
@dataclass(frozen=True)
@cbor_message
class HarvesterHandshake:
pool_pubkeys: List[PublicKey]
@dataclass(frozen=True)
@cbor_message
class NewChallenge:
challenge_hash: bytes32
@dataclass(frozen=True)
@cbor_message
class ChallengeResponse:
challenge_hash: bytes32
quality: bytes32
plot_size: uint8
self.node_server: ChiaServer = server
self.connections: PeerConnections = server.global_connections
self.logs: List[logging.LogRecord] = []
self.app: Optional[Application] = None
self.closed: bool = False
self.num_blocks: int = 10
self.num_top_block_pools: int = 5
self.top_winners: List[Tuple[uint64, bytes32]] = []
self.our_winners: List[Tuple[uint64, bytes32]] = []
self.prev_route: str = "home/"
self.route: str = "home/"
self.focused: bool = False
self.parent_close_cb = parent_close_cb
self.kb = self.setup_keybindings()
self.style = Style([("error", "#ff0044")])
self.pool_pks: List[PublicKey] = []
key_config_filename = os.path.join(ROOT_DIR, "config", "keys.yaml")
if os.path.isfile(key_config_filename):
config = safe_load(open(key_config_filename, "r"))
self.pool_pks = [
PrivateKey.from_bytes(bytes.fromhex(ce)).get_public_key()
for ce in config["pool_sks"]
]
self.draw_initial()
self.app = Application(
style=self.style,
layout=self.layout,
full_screen=True,
key_bindings=self.kb,
mouse_support=True,
"-n", "--num_plots", help="Number of plots", type=int, default=10
)
parser.add_argument(
"-p", "--pool_pub_key", help="Hex public key of pool", type=str, default=""
)
# We need the keys file, to access pool keys (if the exist), and the sk_seed.
args = parser.parse_args()
if not os.path.isfile(key_config_filename):
raise RuntimeError("Keys not generated. Run ./scripts/regenerate_keys.py.")
# The seed is what will be used to generate a private key for each plot
key_config = safe_load(open(key_config_filename, "r"))
sk_seed: bytes = bytes.fromhex(key_config["sk_seed"])
pool_pk: PublicKey
if len(args.pool_pub_key) > 0:
# Use the provided pool public key, useful for using an external pool
pool_pk = PublicKey.from_bytes(bytes.fromhex(args.pool_pub_key))
else:
# Use the pool public key from the config, useful for solo farming
pool_sk = PrivateKey.from_bytes(bytes.fromhex(key_config["pool_sks"][0]))
pool_pk = pool_sk.get_public_key()
print(
f"Creating {args.num_plots} plots of size {args.size}, sk_seed {sk_seed.hex()} ppk {pool_pk}"
)
for i in range(args.num_plots):
# Generate a sk based on the seed, plot size (k), and index
sk: PrivateKey = PrivateKey.from_seed(
sk_seed + args.size.to_bytes(1, "big") + i.to_bytes(4, "big")
import dataclasses
import pprint
from typing import Type, BinaryIO, get_type_hints, Any, List
from hashlib import sha256
from blspy import (PrivateKey, PublicKey, InsecureSignature, Signature, PrependSignature,
ExtendedPrivateKey, ExtendedPublicKey, ChainCode)
from src.util.type_checking import strictdataclass, is_type_List, is_type_SpecificOptional
from src.types.sized_bytes import bytes32
from src.util.ints import uint32
pp = pprint.PrettyPrinter(indent=1, width=120, compact=True)
# TODO: Remove hack, this allows streaming these objects from binary
size_hints = {
"PrivateKey": PrivateKey.PRIVATE_KEY_SIZE,
"PublicKey": PublicKey.PUBLIC_KEY_SIZE,
"Signature": Signature.SIGNATURE_SIZE,
"InsecureSignature": InsecureSignature.SIGNATURE_SIZE,
"PrependSignature": PrependSignature.SIGNATURE_SIZE,
"ExtendedPublicKey": ExtendedPublicKey.EXTENDED_PUBLIC_KEY_SIZE,
"ExtendedPrivateKey": ExtendedPrivateKey.EXTENDED_PRIVATE_KEY_SIZE,
"ChainCode": ChainCode.CHAIN_CODE_KEY_SIZE
}
unhashable_types = [PrivateKey, PublicKey, Signature, PrependSignature, InsecureSignature,
ExtendedPublicKey, ExtendedPrivateKey, ChainCode]
def streamable(cls: Any):
"""
This is a decorator for class definitions. It applies the strictdataclass decorator,
which checks all types at construction. It also defines a simple serialization format,
and adds parse, from bytes, stream, and __bytes__ methods.