Skip to content

Add batch chain operations (batch_anchor, record_access) #56

Description

@crtahlin

Summary

Batch operations for efficiency: register multiple hashes in a single transaction and record data access events. These reduce gas costs when working with multiple data items.

Part of #43. Depends on chain infrastructure setup.

What to Implement

1. batch_anchor Tool

Register up to 50 Swarm hashes in a single blockchain transaction.

Tool(
    name="batch_anchor",
    description="Register multiple Swarm reference hashes on-chain in a single transaction. More gas-efficient than individual anchor_hash calls when registering many hashes. Maximum 50 hashes per batch. Costs gas.",
    inputSchema={
        "type": "object",
        "properties": {
            "swarm_hashes": {
                "type": "array",
                "items": {
                    "type": "string",
                    "pattern": "^[a-fA-F0-9]{64}$"
                },
                "description": "Array of 64-character hex Swarm reference hashes to anchor",
                "minItems": 1,
                "maxItems": 50
            },
            "data_type": {
                "type": "string",
                "description": "Category/type label applied to all hashes in the batch (default: 'swarm-provenance')",
                "default": "swarm-provenance",
                "maxLength": 64
            }
        },
        "required": ["swarm_hashes"]
    }
)

Handler calls ChainClient.batch_anchor(hashes, data_types). Report: how many anchored, tx hash, gas used, any that were already registered.

2. record_access Tool

Record that data was accessed (for audit trail / usage tracking).

Tool(
    name="record_access",
    description="Record a data access event on-chain. Creates an audit trail of when data was accessed. Supports single hash or batch of up to 100 hashes. Costs gas.",
    inputSchema={
        "type": "object",
        "properties": {
            "swarm_hashes": {
                "type": "array",
                "items": {
                    "type": "string",
                    "pattern": "^[a-fA-F0-9]{64}$"
                },
                "description": "Array of Swarm reference hashes that were accessed (1-100)",
                "minItems": 1,
                "maxItems": 100
            }
        },
        "required": ["swarm_hashes"]
    }
)

Handler calls ChainClient.access() for single hash or ChainClient.batch_access() for multiple.

Design Notes

  • batch_anchor: Validates all hashes before submitting. If any are already registered, skip them and report.
  • record_access: Each access event is timestamped on-chain. Useful for compliance and audit trails.
  • Both are gas-consuming write operations.
  • Contract limits: MAX_BATCH_REGISTER = 50, MAX_BATCH_ACCESS = 100

Files to Modify

  • swarm_provenance_mcp/server.py — add 2 tool definitions, handlers, routing
  • tests/test_integration.py — add tests

Reference

  • CLI: chain_anchor (single), ChainClient batch_anchor() and batch_access()
  • Contract constants: MAX_BATCH_REGISTER = 50, MAX_BATCH_ACCESS = 100

Acceptance Criteria

  • batch_anchor accepts array of hashes (1-50)
  • batch_anchor validates all hashes before submitting
  • batch_anchor reports count anchored, skipped (already registered), gas used
  • record_access accepts array of hashes (1-100)
  • record_access uses batch method for multiple hashes
  • Both handle insufficient funds gracefully
  • Tests cover batch of 1, batch of many, and over-limit validation

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    chain-anchoringOn-chain provenance & blockchain anchoringenhancementNew feature or requestpriority-lowLow priority / nice to have

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions