Add blockchain event bridge service with smart contract event integration
Some checks failed
Blockchain Synchronization Verification / sync-verification (push) Failing after 2s
Integration Tests / test-service-integration (push) Failing after 15s
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 2s
P2P Network Verification / p2p-verification (push) Successful in 2s
Python Tests / test-python (push) Successful in 12s
Security Scanning / security-scan (push) Successful in 41s
Systemd Sync / sync-systemd (push) Successful in 7s

- Phase 1: Core bridge service with gossip broker subscription
- Phase 2: Smart contract event integration via eth_getLogs RPC endpoint
- Add contract event subscriber for AgentStaking, PerformanceVerifier, Marketplace, Bounty, CrossChainBridge
- Add contract event handlers in agent_daemon.py and marketplace.py
- Add systemd service file for blockchain-event-bridge
- Update blockchain node router.py with eth_getLogs endpoint
- Add configuration for contract addresses
- Add tests for contract subscriber and handlers (27 tests passing)
This commit is contained in:
aitbc
2026-04-23 10:58:00 +02:00
parent ab45a81bd7
commit 90edea2da2
29 changed files with 3704 additions and 0 deletions

View File

@@ -0,0 +1 @@
"""Tests for blockchain event bridge."""

View File

@@ -0,0 +1,116 @@
"""Tests for action handlers."""
import pytest
from unittest.mock import Mock, AsyncMock, patch
from blockchain_event_bridge.action_handlers.coordinator_api import CoordinatorAPIHandler
from blockchain_event_bridge.action_handlers.agent_daemon import AgentDaemonHandler
from blockchain_event_bridge.action_handlers.marketplace import MarketplaceHandler
@pytest.mark.asyncio
async def test_coordinator_api_handler_initialization():
"""Test coordinator API handler initialization."""
handler = CoordinatorAPIHandler("http://localhost:8011", "test-key")
assert handler.base_url == "http://localhost:8011"
assert handler.api_key == "test-key"
assert handler._client is None
@pytest.mark.asyncio
async def test_coordinator_api_handler_handle_block():
"""Test coordinator API handler handling a block."""
handler = CoordinatorAPIHandler("http://localhost:8011")
block_data = {"height": 100, "hash": "0x123"}
transactions = [{"type": "ai_job", "hash": "0x456"}]
with patch.object(handler, "handle_transaction", new_callable=AsyncMock) as mock_handle_tx:
await handler.handle_block(block_data, transactions)
mock_handle_tx.assert_called_once_with(transactions[0])
@pytest.mark.asyncio
async def test_coordinator_api_handler_close():
"""Test closing coordinator API handler."""
handler = CoordinatorAPIHandler("http://localhost:8011")
# Create a client first
await handler._get_client()
assert handler._client is not None
await handler.close()
assert handler._client is None
@pytest.mark.asyncio
async def test_agent_daemon_handler_initialization():
"""Test agent daemon handler initialization."""
handler = AgentDaemonHandler("http://localhost:8006")
assert handler.blockchain_rpc_url == "http://localhost:8006"
assert handler._client is None
@pytest.mark.asyncio
async def test_agent_daemon_handler_handle_transaction():
"""Test agent daemon handler handling a transaction."""
handler = AgentDaemonHandler("http://localhost:8006")
tx_data = {
"hash": "0x123",
"type": "agent_message",
"to": "agent_address",
"payload": {"trigger": "process"}
}
with patch.object(handler, "_notify_agent_daemon", new_callable=AsyncMock) as mock_notify:
await handler.handle_transaction(tx_data)
mock_notify.assert_called_once_with(tx_data)
@pytest.mark.asyncio
async def test_agent_daemon_handler_is_agent_transaction():
"""Test checking if transaction is an agent transaction."""
handler = AgentDaemonHandler("http://localhost:8006")
# Agent transaction
assert handler._is_agent_transaction({"payload": {"trigger": "test"}}) is True
assert handler._is_agent_transaction({"payload": {"agent": "test"}}) is True
# Not an agent transaction
assert handler._is_agent_transaction({"payload": {"other": "test"}}) is False
assert handler._is_agent_transaction({}) is False
@pytest.mark.asyncio
async def test_marketplace_handler_initialization():
"""Test marketplace handler initialization."""
handler = MarketplaceHandler("http://localhost:8011", "test-key")
assert handler.base_url == "http://localhost:8011"
assert handler.api_key == "test-key"
assert handler._client is None
@pytest.mark.asyncio
async def test_marketplace_handler_filter_marketplace_transactions():
"""Test filtering marketplace transactions."""
handler = MarketplaceHandler("http://localhost:8011")
transactions = [
{"type": "marketplace", "hash": "0x1"},
{"type": "transfer", "hash": "0x2"},
{"type": "listing", "hash": "0x3"},
{"type": "transfer", "payload": {"listing_id": "123"}, "hash": "0x4"},
]
filtered = handler._filter_marketplace_transactions(transactions)
assert len(filtered) == 3
assert filtered[0]["hash"] == "0x1"
assert filtered[1]["hash"] == "0x3"
assert filtered[2]["hash"] == "0x4"

View File

@@ -0,0 +1,77 @@
"""Tests for contract event handlers."""
import pytest
from unittest.mock import Mock, AsyncMock
from blockchain_event_bridge.action_handlers.agent_daemon import AgentDaemonHandler
from blockchain_event_bridge.action_handlers.marketplace import MarketplaceHandler
@pytest.mark.asyncio
async def test_agent_daemon_handle_staking_event():
"""Test agent daemon handler for staking events."""
handler = AgentDaemonHandler("http://localhost:8006")
event_log = {
"topics": ["StakeCreated"],
"data": '{"stakeId": "123", "staker": "0xabc"}'
}
# Should not raise an error
await handler.handle_staking_event(event_log)
@pytest.mark.asyncio
async def test_agent_daemon_handle_performance_event():
"""Test agent daemon handler for performance events."""
handler = AgentDaemonHandler("http://localhost:8006")
event_log = {
"topics": ["PerformanceVerified"],
"data": '{"verificationId": "456", "withinSLA": true}'
}
# Should not raise an error
await handler.handle_performance_event(event_log)
@pytest.mark.asyncio
async def test_agent_daemon_handle_bounty_event():
"""Test agent daemon handler for bounty events."""
handler = AgentDaemonHandler("http://localhost:8006")
event_log = {
"topics": ["BountyCreated"],
"data": '{"bountyId": "789", "creator": "0xdef"}'
}
# Should not raise an error
await handler.handle_bounty_event(event_log)
@pytest.mark.asyncio
async def test_agent_daemon_handle_bridge_event():
"""Test agent daemon handler for bridge events."""
handler = AgentDaemonHandler("http://localhost:8006")
event_log = {
"topics": ["BridgeInitiated"],
"data": '{"requestId": "101", "sourceChain": "ethereum"}'
}
# Should not raise an error
await handler.handle_bridge_event(event_log)
@pytest.mark.asyncio
async def test_marketplace_handle_contract_event():
"""Test marketplace handler for contract events."""
handler = MarketplaceHandler("http://localhost:8011")
event_log = {
"topics": ["ServiceListed"],
"data": '{"serviceId": "202", "provider": "0x123"}'
}
# Should not raise an error
await handler.handle_contract_event(event_log)

View File

@@ -0,0 +1,103 @@
"""Tests for contract event subscriber."""
import pytest
from unittest.mock import Mock, AsyncMock, patch
from blockchain_event_bridge.event_subscribers.contracts import ContractEventSubscriber
@pytest.mark.asyncio
async def test_contract_subscriber_initialization():
"""Test contract subscriber initialization."""
from blockchain_event_bridge.config import Settings
settings = Settings()
subscriber = ContractEventSubscriber(settings)
assert subscriber.settings == settings
assert subscriber._running is False
assert subscriber.contract_addresses is not None
@pytest.mark.asyncio
async def test_contract_subscriber_set_bridge():
"""Test setting bridge on contract subscriber."""
from blockchain_event_bridge.config import Settings
from blockchain_event_bridge.bridge import BlockchainEventBridge
settings = Settings()
subscriber = ContractEventSubscriber(settings)
bridge = Mock(spec=BlockchainEventBridge)
subscriber.set_bridge(bridge)
assert subscriber._bridge == bridge
@pytest.mark.asyncio
async def test_contract_subscriber_disabled():
"""Test contract subscriber when disabled."""
from blockchain_event_bridge.config import Settings
settings = Settings(subscribe_contracts=False)
subscriber = ContractEventSubscriber(settings)
await subscriber.run()
assert subscriber._running is False
@pytest.mark.asyncio
async def test_contract_subscriber_stop():
"""Test stopping contract subscriber."""
from blockchain_event_bridge.config import Settings
settings = Settings(subscribe_contracts=False)
subscriber = ContractEventSubscriber(settings)
await subscriber.stop()
assert subscriber._running is False
@pytest.mark.asyncio
async def test_process_staking_event():
"""Test processing staking event."""
from blockchain_event_bridge.config import Settings
from blockchain_event_bridge.bridge import BlockchainEventBridge
settings = Settings()
subscriber = ContractEventSubscriber(settings)
bridge = Mock(spec=BlockchainEventBridge)
bridge.handle_staking_event = AsyncMock()
subscriber.set_bridge(bridge)
event_log = {
"topics": ["StakeCreated"],
"data": "{}",
"address": "0x123"
}
await subscriber._handle_staking_event(event_log)
bridge.handle_staking_event.assert_called_once_with(event_log)
@pytest.mark.asyncio
async def test_process_performance_event():
"""Test processing performance event."""
from blockchain_event_bridge.config import Settings
from blockchain_event_bridge.bridge import BlockchainEventBridge
settings = Settings()
subscriber = ContractEventSubscriber(settings)
bridge = Mock(spec=BlockchainEventBridge)
bridge.handle_performance_event = AsyncMock()
subscriber.set_bridge(bridge)
event_log = {
"topics": ["PerformanceVerified"],
"data": "{}",
"address": "0x123"
}
await subscriber._handle_performance_event(event_log)
bridge.handle_performance_event.assert_called_once_with(event_log)

View File

@@ -0,0 +1,69 @@
"""Tests for event subscribers."""
import pytest
import asyncio
from unittest.mock import Mock, AsyncMock
from blockchain_event_bridge.event_subscribers.blocks import BlockEventSubscriber
from blockchain_event_bridge.event_subscribers.transactions import TransactionEventSubscriber
@pytest.mark.asyncio
async def test_block_subscriber_initialization():
"""Test block subscriber initialization."""
from blockchain_event_bridge.config import Settings
settings = Settings()
subscriber = BlockEventSubscriber(settings)
assert subscriber.settings == settings
assert subscriber._running is False
@pytest.mark.asyncio
async def test_block_subscriber_set_bridge():
"""Test setting bridge on block subscriber."""
from blockchain_event_bridge.config import Settings
from blockchain_event_bridge.bridge import BlockchainEventBridge
settings = Settings()
subscriber = BlockEventSubscriber(settings)
bridge = Mock(spec=BlockchainEventBridge)
subscriber.set_bridge(bridge)
assert subscriber._bridge == bridge
@pytest.mark.asyncio
async def test_block_subscriber_stop():
"""Test stopping block subscriber."""
from blockchain_event_bridge.config import Settings
settings = Settings()
subscriber = BlockEventSubscriber(settings)
# Start and immediately stop
task = asyncio.create_task(subscriber.run())
await asyncio.sleep(0.1) # Let it start
await subscriber.stop()
# Cancel the task
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
assert subscriber._running is False
@pytest.mark.asyncio
async def test_transaction_subscriber_initialization():
"""Test transaction subscriber initialization."""
from blockchain_event_bridge.config import Settings
settings = Settings()
subscriber = TransactionEventSubscriber(settings)
assert subscriber.settings == settings
assert subscriber._running is False

View File

@@ -0,0 +1,70 @@
"""Integration tests for blockchain event bridge."""
import pytest
from unittest.mock import Mock, AsyncMock, patch
from blockchain_event_bridge.bridge import BlockchainEventBridge
from blockchain_event_bridge.config import Settings
@pytest.mark.asyncio
async def test_bridge_initialization():
"""Test bridge initialization."""
settings = Settings()
bridge = BlockchainEventBridge(settings)
assert bridge.settings == settings
assert bridge.is_running is False
@pytest.mark.asyncio
async def test_bridge_start_stop():
"""Test bridge start and stop."""
settings = Settings(
subscribe_blocks=False,
subscribe_transactions=False,
)
bridge = BlockchainEventBridge(settings)
await bridge.start()
assert bridge.is_running is True
await bridge.stop()
assert bridge.is_running is False
@pytest.mark.asyncio
async def test_bridge_handle_block_event():
"""Test bridge handling a block event."""
settings = Settings(
enable_coordinator_api_trigger=False,
enable_marketplace_trigger=False,
)
bridge = BlockchainEventBridge(settings)
block_data = {
"height": 100,
"hash": "0x123",
"transactions": []
}
# Should not raise an error even without handlers
await bridge.handle_block_event(block_data)
@pytest.mark.asyncio
async def test_bridge_handle_transaction_event():
"""Test bridge handling a transaction event."""
settings = Settings(
enable_agent_daemon_trigger=False,
enable_coordinator_api_trigger=False,
)
bridge = BlockchainEventBridge(settings)
tx_data = {
"hash": "0x456",
"type": "transfer"
}
# Should not raise an error even without handlers
await bridge.handle_transaction_event(tx_data)