chia-blockchain/chia/plotters/bladebit.py
Florin Chirica f550555c7d
Add plotters. (#8497)
* Initial commit add plotters.

* Lint.

* Add progress for bladebit.

* Address some review comments.

* Oops...

* Add install option.

* Change chiapos to fit the old standard.

* Update chia/plotters/bladebit.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/install_plotter.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Lint.

* Remove farmerkey as required.

* Chia plotters chiapos works with no arguments.

* Added get_plotters RPC to support the GUI (#8530)

* Added 'get_plotters' daemon RPC. Probes installed/available plotters on behalf of the GUI.

* Linter fix

* Minor type tweak

* Tweaks.

* Run with default arguments all plotters.

* Fix bug.

* Update chia/plotters/bladebit.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Change bladebit repo.

* Update chia/plotters/bladebit.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Update chia/plotters/plotters.py

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>

* Re-added the connect-to-daemon hidden option

* Use connect_to_daemon value for madmax and bladebit plot key resolution.

* Updated --tmp_dir, --tmp_dir2, --final_dir options to match old options from chia plots create

* Add CONNECT_TO_DAEMON as a valid option for madmax/bladebit

* Thread multiplier should be an int

* Passing params for madmax to start_plotting. Still needs cleanup/refactoring.

* Update chia/plotters/bladebit.py

Co-authored-by: Kyle Altendorf <sda@fstab.net>

* Update chia/plotters/madmax.py

Co-authored-by: Kyle Altendorf <sda@fstab.net>

* Filename option -z.

* Factor out calling the plotter.

* First attempt refactor install scripts.

* Switch to exec.

* Attempt to fix mypy warning.

* Remove filename.

* Increase RLIMIT_NOFILE for madmax on non-Windows platforms

* Add trailing path separator to madmax tmpdir/tmp2dir/finaldir arguments (required by madmax)

* Fixes to support madmax plotting from the GUI.
Writing output from the plotters now includes a flush to ensure the plotter log (used by the GUI) is updated frequently.

* Handle madmax's tmptoggle option internally when plotting with the GUI

* Formatting and linter fix

* Fixed the -i option for bladebit

* Construct BladeBit plotting options

* Cleanup code for building plotter command line options

* Added a post-processing step after each plotting job completes. Adds the final_dir plot directory as necessary.

* Fix plotter root path

* Reverting prior checkin. Need to figure out how to handle CHIA_ROOT being overridden

* BladeBit support for Windows

* BladeBit's --memory-json option is used to check memory requirements

* Madmax Windows support

* Plotters directory is now under CHIA_ROOT

* Madmax version detection

* BladeBit will default to 0 threads. BladeBit will max-out available threads with this configuration.

* LGTM fixes

* Module definition for chia.plotters to resolve mypy issues with chiapos (package vs chiapos.py)

* Updated BladeBit build script to account for 1.2.0 changes.
Replaced remaining subprocess.run calls with run_command.
Use BladeBit's reported memory requirement instead of hardcoded value.

* Show a disclaimer when using thirdparty plotter

* Test adding mac madmax plotter to the installers

* Get latest madmax from the latest GH release

* Fix bad var name

* m1 madmax

* Add linux/linux arm

* pip install -e for arm installer, so that its consistent with the other platforms when looking for additional files

* madmax + windows

* Get madmax with Invoke-WebRequest

* Use the correct windows slashes

* add madmax to the list of windows binaries

* Check if madmax exists on windows install and move it to site packages if it does

* Make sure windows has .exe extension for madmax

* Update azure to get latest version of madmax from GH releases

* Bladebit for linux/linux arm

* Fix error with binaries.extend

* Bundle bladebit + windows

* Fix download url for bladebit

* Check for bladebit in windows script. move to correct directory if it exists

* Detect and use packaged plotters

* Removed unnecessary import

* Updating the branch to use chiaplotters_gui for installer verification

* Removed a change that was intended for debugging only

* Remove disclaimer

* Updated for new madmax plotter with k33, k34 support.
Updated chia-blockchain-gui submodule

* Fixed typo

* Package the chia_plot_k34 executable

* Boink

* Revert "Boink"

This reverts commit 8d13c07110.

* Additional chia_plot_k34 spots that I missed

* pyinstaller.spec fix for chia_plot_k34

* Windows installer fix for chia_plot_k34.exe

* Restoring chia-blockchain-gui submodule to 047ce16 (as in main)

* Update to chiapos 1.0.6

Co-authored-by: Jeff Cruikshank <paninaro@gmail.com>
Co-authored-by: Jeff Cruikshank <jeff@chia.net>
Co-authored-by: Kyle Altendorf <sda@fstab.net>
Co-authored-by: Chris Marslender <chrismarslender@gmail.com>
Co-authored-by: Earle Lowe <30607889+emlowe@users.noreply.github.com>
2021-10-28 15:37:46 -07:00

217 lines
7.4 KiB
Python

import asyncio
import json
import traceback
import os
import sys
import logging
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
from chia.plotting.create_plots import resolve_plot_keys
from chia.plotters.plotters_util import run_plotter, run_command
log = logging.getLogger(__name__)
BLADEBIT_PLOTTER_DIR = "bladebit"
def is_bladebit_supported() -> bool:
return sys.platform.startswith("linux") or sys.platform in ["win32", "cygwin"]
def meets_memory_requirement(plotters_root_path: Path) -> Tuple[bool, Optional[str]]:
have_enough_memory: bool = False
warning_string: Optional[str] = None
if get_bladebit_executable_path(plotters_root_path).exists():
try:
proc = run_command(
[os.fspath(get_bladebit_executable_path(plotters_root_path)), "--memory-json"],
"Failed to call bladebit with --memory-json option",
capture_output=True,
text=True,
)
memory_info: Dict[str, int] = json.loads(proc.stdout)
total_bytes: int = memory_info.get("total", -1)
required_bytes: int = memory_info.get("required", 0)
have_enough_memory = total_bytes >= required_bytes
if have_enough_memory is False:
warning_string = f"BladeBit requires at least {int(required_bytes / 1024**3)} GiB of RAM to operate"
except Exception as e:
print(f"Failed to determine bladebit memory requirements: {e}")
return have_enough_memory, warning_string
def get_bladebit_install_path(plotters_root_path: Path) -> Path:
return plotters_root_path / BLADEBIT_PLOTTER_DIR
def get_bladebit_package_path() -> Path:
return Path(os.path.dirname(sys.executable)) / "bladebit"
def get_bladebit_executable_path(plotters_root_path: Path) -> Path:
bladebit_dir: Path = get_bladebit_package_path()
bladebit_exec: str = "bladebit"
build_dir: str = "build"
if sys.platform in ["win32", "cygwin"]:
bladebit_exec = "bladebit.exe"
build_dir = "build/Release"
if not bladebit_dir.exists():
bladebit_dir = get_bladebit_install_path(plotters_root_path) / build_dir
return bladebit_dir / bladebit_exec
def get_bladebit_install_info(plotters_root_path: Path) -> Optional[Dict[str, Any]]:
info: Dict[str, Any] = {"display_name": "BladeBit Plotter"}
installed: bool = False
supported: bool = is_bladebit_supported()
if get_bladebit_executable_path(plotters_root_path).exists():
version: Optional[str] = None
try:
proc = run_command(
[os.fspath(get_bladebit_executable_path(plotters_root_path)), "--version"],
"Failed to call bladebit with --version option",
capture_output=True,
text=True,
)
version = proc.stdout.strip()
except Exception as e:
print(f"Failed to determine bladebit version: {e}")
if version is not None:
installed = True
info["version"] = version
else:
installed = False
info["installed"] = installed
if installed is False:
info["can_install"] = supported
if supported:
_, memory_warning = meets_memory_requirement(plotters_root_path)
if memory_warning is not None:
info["bladebit_memory_warning"] = memory_warning
return info
progress = {
"Finished F1 sort": 0.01,
"Finished forward propagating table 2": 0.06,
"Finished forward propagating table 3": 0.12,
"Finished forward propagating table 4": 0.2,
"Finished forward propagating table 5": 0.28,
"Finished forward propagating table 6": 0.36,
"Finished forward propagating table 7": 0.42,
"Finished prunning table 6": 0.43,
"Finished prunning table 5": 0.48,
"Finished prunning table 4": 0.51,
"Finished prunning table 3": 0.55,
"Finished prunning table 2": 0.58,
"Finished compressing tables 1 and 2": 0.66,
"Finished compressing tables 2 and 3": 0.73,
"Finished compressing tables 3 and 4": 0.79,
"Finished compressing tables 4 and 5": 0.85,
"Finished compressing tables 5 and 6": 0.92,
"Finished compressing tables 6 and 7": 0.98,
}
def install_bladebit(root_path):
if is_bladebit_supported():
print("Installing dependencies.")
run_command(
[
"sudo",
"apt",
"install",
"-y",
"build-essential",
"cmake",
"libnuma-dev",
"git",
],
"Could not install dependencies",
)
print("Cloning repository and its submodules.")
run_command(
[
"git",
"clone",
"--recursive",
"https://github.com/Chia-Network/bladebit.git",
],
"Could not clone bladebit repository",
cwd=os.fspath(root_path),
)
bladebit_path: str = os.fspath(root_path.joinpath("bladebit"))
build_path: str = os.fspath(Path(bladebit_path) / "build")
print("Build bladebit.")
run_command(["mkdir", build_path], "Failed to create build directory", cwd=bladebit_path)
run_command(["cmake", ".."], "Failed to generate build config", cwd=build_path)
run_command(
["cmake", "--build", ".", "--target", "bladebit", "--config", "Release"],
"Building bladebit failed",
cwd=build_path,
)
else:
raise RuntimeError("Platform not supported yet for bladebit plotter.")
def plot_bladebit(args, chia_root_path, root_path):
if not os.path.exists(get_bladebit_executable_path(root_path)):
print("Installing bladebit plotter.")
try:
install_bladebit(root_path)
except Exception as e:
print(f"Exception while installing bladebit plotter: {e}")
return
plot_keys = asyncio.get_event_loop().run_until_complete(
resolve_plot_keys(
None if args.farmerkey == b"" else args.farmerkey.hex(),
None,
None if args.pool_key == b"" else args.pool_key.hex(),
None if args.contract == "" else args.contract,
chia_root_path,
log,
args.connect_to_daemon,
)
)
call_args = []
call_args.append(os.fspath(get_bladebit_executable_path(root_path)))
call_args.append("-t")
call_args.append(str(args.threads))
call_args.append("-n")
call_args.append(str(args.count))
call_args.append("-f")
call_args.append(bytes(plot_keys.farmer_public_key).hex())
if plot_keys.pool_public_key is not None:
call_args.append("-p")
call_args.append(bytes(plot_keys.pool_public_key).hex())
if plot_keys.pool_contract_address is not None:
call_args.append("-c")
call_args.append(plot_keys.pool_contract_address)
if args.warmstart:
call_args.append("-w")
if args.id is not None and args.id != b"":
call_args.append("-i")
call_args.append(args.id.hex())
if args.verbose:
call_args.append("-v")
if args.nonuma:
call_args.append("-m")
call_args.append(args.finaldir)
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(run_plotter(call_args, progress))
except Exception as e:
print(f"Exception while plotting: {e} {type(e)}")
print(f"Traceback: {traceback.format_exc()}")