### Install Pip on Ubuntu Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/installation.md Installs the python3-pip package if pip is not already available on the system. ```sh apt-get install python3-pip ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/building_an_app_that_uses_pyevm.md Install the project's dependencies, including Py-EVM, within the activated virtual environment. ```sh pip install -e ".[dev]" ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Install the necessary development dependencies using pip within a virtual environment. This includes setting up the project in editable mode and installing pre-commit hooks. ```sh cd py-evm virtualenv -p python venv . venv/bin/activate python -m pip install -e ".[dev]" pre-commit install ``` -------------------------------- ### Build and Test Release Package Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Before releasing, build the package and test its installation in a temporary virtual environment. This ensures the release candidate is stable. ```sh git checkout main && git pull make package-test ``` -------------------------------- ### Install virtualenv Source: https://github.com/ethereum/py-evm/blob/main/docs/fragments/virtualenv_explainer.md Install the virtualenv package using pip if it's not already present. ```shell pip install virtualenv ``` -------------------------------- ### Create a State Test Filler Source: https://github.com/ethereum/py-evm/blob/main/docs/api/tools/api.tools.fixtures.md Example of creating a state test filler by piping a dictionary through helper functions like setup_main_filler, pre_state, and expect. This demonstrates the functional approach to defining test cases. ```python filler = pipe( setup_main_filler("test"), pre_state( (sender, "balance", 1), (receiver, "balance", 0), ), expect( networks=["Frontier"], transaction={ "to": receiver, "value": 1, "secretKey": sender_key, }, post_state=[ [sender, "balance", 0], [receiver, "balance", 1], ] ) ) ``` -------------------------------- ### Install Py-EVM Source: https://github.com/ethereum/py-evm/blob/main/README.md Install the Py-EVM package using pip. This command is used to add the library to your Python environment. ```shell python -m pip install py-evm ``` -------------------------------- ### Install Python 3.9 Development Headers on Ubuntu Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/installation.md Installs the python3.9-dev package required for compiling Py-EVM dependencies on Ubuntu. ```sh apt-get install python3.9-dev ``` -------------------------------- ### Clone Ethereum Python Project Template Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/building_an_app_that_uses_pyevm.md Clone the project template to start a new application. This sets up the basic project structure. ```sh git clone https://github.com/ethereum/ethereum-python-project-template.git demo-app ``` -------------------------------- ### Install Py-EVM Package Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/installation.md Installs or upgrades the py-evm package and its dependencies using pip within the active virtual environment. ```sh pip3 install -U py-evm ``` -------------------------------- ### Install Python 3 with Homebrew on macOS Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/installation.md Installs the latest version of Python 3 using the Homebrew package manager on macOS. ```sh brew install python3 ``` -------------------------------- ### Removing Doctest Prefixes Source: https://github.com/ethereum/py-evm/blob/main/docs/fragments/doctest_explainer.md Interactive Python session examples often use `>>>` and `...` prefixes. This snippet shows how to prepare such code for direct execution by removing these prefixes. ```python >>> from eth.chains.mainnet import MainnetChain >>> chain = MainnetChain() >>> chain.get_block_by_number(0) ``` ```python from eth.chains.mainnet import MainnetChain chain = MainnetChain() chain.get_block_by_number(0) # ``` -------------------------------- ### Opcode Consuming Gas Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/creating_opcodes.md An example of an opcode function that explicitly consumes a fixed amount of gas. It demonstrates the use of `computation.consume_gas`. ```python def burn_5_gas(computation): """ An opcode which simply burns 5 gas """ computation.consume_gas(5, reason='why not?') ``` -------------------------------- ### Upgrade Pip Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/installation.md Ensures that pip is updated to the latest version within the active virtual environment to correctly install dependencies. ```sh pip3 install -U pip ``` -------------------------------- ### Simple No-Operation Opcode Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/creating_opcodes.md Defines a basic opcode function that performs no action and consumes no gas. This serves as a fundamental example of the opcode API. ```python def noop(computation): """ An opcode which does nothing (not even consume gas) """ pass ``` -------------------------------- ### Using as_opcode Helper Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/creating_opcodes.md Illustrates how to use the `as_opcode` helper function to create an opcode with a predefined gas cost and mnemonic. The helper manages gas consumption before executing the provided logic function. ```python def custom_op(computation): ... # opcode logic here class ExampleComputation(BaseComputation): opcodes = { b'\x01': as_opcode(custom_op, 'CUSTOM_OP', 10), } ``` -------------------------------- ### Build Release Notes Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Generate the release notes by running 'make notes' and specifying the version part to bump. This should be done before bumping the version number. ```sh make notes bump=$$VERSION_PART_TO_BUMP$$ ``` -------------------------------- ### Creating a Test Chain Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Configures and creates a test blockchain instance using the Byzantium VM and the defined genesis parameters. ```default from eth import MiningChain from eth.vm.forks.byzantium import ByzantiumVM from eth.db.backends.memory import AtomicDB klass = MiningChain.configure( __name__='TestChain', vm_configuration=( (constants.GENESIS_BLOCK_NUMBER, ByzantiumVM), )) chain = klass.from_genesis(AtomicDB(), GENESIS_PARAMS) ``` -------------------------------- ### Py-EVM Application Code Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/building_an_app_that_uses_pyevm.md This Python script initializes a Py-EVM VM, creates a pre-funded account, and reads its balance. ```python from eth_utils import to_wei from py_evm.vm.forks.frontier import FrontierVM from py_evm.db.atomic import AtomicDB from py_evm.vm.state import State # Create a new VM instance with an empty state state = State(AtomicDB()) vm = FrontierVM(state=state) # Create a new account with a balance of 100 Ether address = bytes.fromhex("0000000000000000000000000000000000000000") balance = to_wei(100, 'ether') vm.state.set_balance(address, balance) # Read the balance of the account read_balance = vm.state.get_balance(address) print(f"The balance of address {address.hex()} is {read_balance} wei") ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/building_an_app_that_uses_pyevm.md Initialize a new virtual environment named 'venv' and activate it. This isolates project dependencies. ```sh virtualenv -p python3 venv . venv/bin/activate ``` -------------------------------- ### Initializing a Fresh Chain Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Initializes a new blockchain instance using a genesis header and an atomic database. ```python from eth.db.backends.memory import AtomicDB from eth.chains.base import MiningChain from eth.constants import GENESIS_BLOCK_NUMBER from eth.vm.forks.byzantium import ByzantiumVM # Initialize a fresh chain chain_class = MiningChain.configure( __name__='TestChain', vm_configuration=((GENESIS_BLOCK_NUMBER, ByzantiumVM),) ) genesis_header = chain_class.create_genesis_header() chain = chain_class.from_genesis_header(AtomicDB(), genesis_header) ``` -------------------------------- ### Add Py-EVM Dependency Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/building_an_app_that_uses_pyevm.md Modify the setup.py file to include Py-EVM and eth-utils in your project's dependencies. ```python install_requires=[ "eth-utils>=1,<2", "py-evm==0.5.0a0", ], ``` -------------------------------- ### Initialize a Chain with a Custom Gas Limit Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md This snippet demonstrates how to initialize a new Ethereum chain with a custom gas limit for the genesis block. It uses `AtomicDB` for the database and `MAINNET_GENESIS_HEADER` as a base, copying it to modify the `gas_limit`. ```python from eth.db.atomic import AtomicDB from eth.chains.mainnet import MAINNET_GENESIS_HEADER # increase the gas limit genesis_header = MAINNET_GENESIS_HEADER.copy(gas_limit=3141592) # initialize a fresh chain chain = chain_class.from_genesis_header(AtomicDB(), genesis_header) ``` -------------------------------- ### Defining Genesis Parameters Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Sets up the initial parameters for the genesis block, including difficulty, gas limit, and timestamp. Difficulty is set to 1 for testing purposes. ```default from eth import constants GENESIS_PARAMS = { 'difficulty': 1, 'gas_limit': 3141592, 'timestamp': 1514764800, } ``` -------------------------------- ### Lint Code with Make Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Manually run the code linting process using the make command. This ensures code style consistency across the project. ```sh make lint ``` -------------------------------- ### Setting Up Sender and Receiver Addresses Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Prepare sender and receiver addresses for a transaction. The sender address is derived from a private key, while the receiver is a predefined address. ```python from eth_keys import keys from eth_utils import decode_hex from eth_typing import Address SENDER_PRIVATE_KEY = keys.PrivateKey( decode_hex('0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8') ) SENDER = Address(SENDER_PRIVATE_KEY.public_key.to_canonical_address()) RECEIVER = Address(b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x02') ``` -------------------------------- ### Release New Version Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Automate the release process, including version bumping, tagging, building, and pushing to GitHub and PyPI. Specify the version part to bump. ```sh make release bump=$$VERSION_PART_TO_BUMP$$ ``` -------------------------------- ### Run the Application Script Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/building_an_app_that_uses_pyevm.md Execute the Python script to run the Py-EVM application and see the output. ```sh python app/main.py ``` -------------------------------- ### Preview Release Notes Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Generate a draft of the release notes using towncrier to preview changes before building the final notes. ```sh towncrier --draft ``` -------------------------------- ### Mining a Proof-of-Work Nonce Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Demonstrates how to find a valid nonce and mix hash for mining a block. Note: This is for demonstration purposes only as Py-EVM does not offer a stable API for actual PoW mining. ```default from eth.consensus.pow import mine_pow_nonce # We have to finalize the block first in order to be able read the ``` -------------------------------- ### Mining a Block with PoW Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md This snippet demonstrates the basic process of mining a block using Py-EVM, including finalizing the block, mining the PoW nonce and mix hash, and creating the final block. ```python block_result = chain.get_vm().finalize_block(chain.get_block()) block = block_result.block # based on mining_hash, block number and difficulty we can perform # the actual Proof of Work (PoW) mechanism to mine the correct # nonce and mix_hash for this block nonce, mix_hash = mine_pow_nonce( block.number, block.header.mining_hash, block.header.difficulty) block = chain.mine_block(mix_hash=mix_hash, nonce=nonce) ``` ```default >>> print(block) Block #1 ``` ``` -------------------------------- ### Opcode Implementation as a Class Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/creating_opcodes.md Shows a pattern for implementing opcodes as classes, allowing for shared logic and structure across multiple opcodes or VM forks. The `__call__` method conforms to the standard opcode API. ```python class MyOpcode: def initial_logic(self, computation): ... def main_logic(self, computation): ... def cleanup_logic(self, computation): ... def __call__(self, computation): self.initial_logic(computation) self.main_logic(computation) self.cleanup_logic(computation) ``` -------------------------------- ### Create a Python 3 Virtual Environment Source: https://github.com/ethereum/py-evm/blob/main/docs/fragments/virtualenv_explainer.md Initialize a new virtual environment named 'venv' using a specific Python 3 interpreter. This isolates project dependencies. ```shell virtualenv -p python3 venv ``` -------------------------------- ### Clone the Repository Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Clone the py-evm repository to your development machine. Ensure you use the --recursive flag to include submodules. ```sh git clone --recursive git@github.com:your-github-username/py-evm.git ``` -------------------------------- ### Activate a Virtual Environment Source: https://github.com/ethereum/py-evm/blob/main/docs/fragments/virtualenv_explainer.md Source the activation script to enter the virtual environment. This modifies your shell's PATH to prioritize packages within the 'venv' directory. ```shell . venv/bin/activate ``` -------------------------------- ### Run All Tests Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Execute the entire test suite using pytest. Be aware that this can take a significant amount of time. ```sh pytest tests ``` -------------------------------- ### Run Specific Test File Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md Run tests from a specific file to quickly check changes. This is useful for targeted testing during development. ```sh pytest tests/core/padding-utils/test_padding.py ``` -------------------------------- ### Creating an Unsigned Transaction Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Create an unsigned transaction by specifying nonce, gas price, gas limit, recipient, value, and data. The nonce ensures ordered transaction processing. ```python vm = chain.get_vm() nonce = vm.state.get_nonce(SENDER) tx = vm.create_unsigned_transaction( nonce=nonce, gas_price=0, gas=100000, to=RECEIVER, value=0, data=b'', ) ``` -------------------------------- ### Release with Explicit New Version Source: https://github.com/ethereum/py-evm/blob/main/docs/contributing.md When issuing an unstable version from a stable one, specify the new version explicitly using the '--new-version' flag with 'make release'. ```sh make release bump="--new-version 4.0.0-alpha.1" ``` -------------------------------- ### Mining a Block with Valid Parameters Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Mines a block by providing the correct `nonce` and `mix_hash` values, ensuring compliance with Proof-of-Work rules. ```default chain.mine_block(nonce=valid_nonce, mix_hash=valid_mix_hash) ``` -------------------------------- ### Applying a Transaction to the Chain Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Apply a signed transaction to the Py-EVM chain. This processes the transaction and updates the chain state. ```python chain.apply_transaction(signed_tx) ``` -------------------------------- ### Mining a Block Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Mines a new block on the chain. This operation may fail if the Proof-of-Work rules are not met. ```python chain.mine_block() ``` -------------------------------- ### Signing a Transaction Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md Sign an unsigned transaction using the sender's private key to make it valid for submission to the chain. ```python signed_tx = tx.as_signed_transaction(SENDER_PRIVATE_KEY) ``` -------------------------------- ### Proof-of-Work Validation Function Source: https://github.com/ethereum/py-evm/blob/main/docs/guides/understanding_the_mining_process.md The `check_pow` function validates the Proof-of-Work parameters for a given block, ensuring the mix hash and difficulty meet the required criteria. ```python from eth.exceptions import ValidationError from eth.typing import Hash32 from eth.consensus.pow import get_cache, hashimoto_light from eth.rlp.sedes.serializable import validate_length from eth.tools.validation import validate_lte from eth.utils.hexadecimal import encode_hex from eth.utils.numeric import big_endian_to_int def check_pow( block_number: int, mining_hash: Hash32, mix_hash: Hash32, nonce: bytes, difficulty: int, ) -> None: validate_length(mix_hash, 32, title="Mix Hash") validate_length(mining_hash, 32, title="Mining Hash") validate_length(nonce, 8, title="POW Nonce") cache = get_cache(block_number) mining_output = hashimoto_light( get_dataset_full_size(block_number), cache, mining_hash, nonce, ) if mining_output["mix_digest"] != mix_hash: raise ValidationError( f"mix hash mismatch; expected: {encode_hex(mining_output['mix_digest'])} " f"!= actual: {encode_hex(mix_hash)}.\n " f"Mix hash calculated from block #{block_number},\n " f"mine hash: {encode_hex(mining_hash)}, " f"nonce: {encode_hex(nonce)}, " f"difficulty: {difficulty}" ) result = big_endian_to_int(mining_output["result"]) validate_lte(result, 2**256 // difficulty, title="POW Difficulty") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.