Execute a law

The executeLaw function is the main entry point for executing a law in the Powers protocol. It handles the complete execution flow of a law, from validation to state changes.

Execution Flow

1. Initial Validation

function executeLaw(
    address caller,
    uint16 lawId,
    bytes calldata lawCalldata,
    uint256 nonce
) public returns (bool success)

The function first validates:

  • The caller is the Powers contract

  • The law is properly initialized

  • The law hash is valid

2. Validation Checks

Two sets of checks are performed:

Proposal Checks

  • Validates parent law completion

  • Checks role permissions

  • Verifies input parameters

Execution Checks

  • Validates execution timing

  • Checks proposal success

  • Verifies execution delay

3. Law Execution

The law's logic is executed through handleRequest:

4. State Changes

If state changes are required:

5. Reply to Powers

If execution targets are provided:

6. Execution Tracking

The execution is recorded:

Error Handling

The following errors may be thrown during execution:

  • Law__OnlyPowers: Caller is not the Powers contract

  • Law__ProposalNotSucceeded: Proposal has not succeeded

  • Law__ExecutionGapTooSmall: Execution gap is too small

  • Law__DeadlineNotPassed: Execution deadline has not passed

Best Practices

  1. Execution Safety

    • Validate all inputs

    • Check execution conditions

    • Verify state consistency

    • Handle errors gracefully

  2. Gas Optimization

    • Minimize state changes

    • Use efficient data structures

    • Cache frequently accessed values

    • Batch operations when possible

  3. State Management

    • Update state atomically

    • Validate state transitions

    • Emit appropriate events

    • Maintain state consistency

  4. Security

    • Validate caller permissions

    • Check for reentrancy

    • Verify execution conditions

    • Protect against manipulation

Example Implementation

Last updated