|
| 1 | +# **Sentinel** |
| 2 | + |
| 3 | +## **Overview** |
| 4 | + |
| 5 | +The **Sentinel** is a modular and extensible system for monitoring blockchain events and managing event subscriptions. Sentinel orchestrates multiple `ChainPollerService` instances, each tied to a specific blockchain network, to fetch logs, process events, and notify subscribers. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## **Key Features** |
| 10 | + |
| 11 | +- **Multi-Chain Support**: Manage multiple blockchain networks (e.g., Ethereum, Polygon, Arbitrum) concurrently. |
| 12 | +- **Event Broadcasting**: Relay blockchain events to subscribers via a thread-safe subscription system. |
| 13 | +- **Flexible Subscriptions**: Dynamically subscribe and unsubscribe to events based on addresses and topics. |
| 14 | +- **Graceful Lifecycle Management**: Start, stop, and clean up resources across services effortlessly. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## **System Architecture** |
| 19 | + |
| 20 | +### **How Components Interact** |
| 21 | + |
| 22 | +``` |
| 23 | + +----------------+ |
| 24 | + | Sentinel | |
| 25 | + | (Coordinator) | |
| 26 | + +----------------+ |
| 27 | + | |
| 28 | + +---------------------------------+--------------------------------+ |
| 29 | + | | | |
| 30 | ++------------+ +-------------------+ +-------------------+ |
| 31 | +| Chain ID 1 | | Chain ID 2 | | Chain ID 3 | |
| 32 | +| (Ethereum) | | (Polygon) | | (Arbitrum) | |
| 33 | ++------------+ +-------------------+ +-------------------+ |
| 34 | + | | | |
| 35 | ++----------------+ +-------------------+ +-------------------+ |
| 36 | +| ChainPollerSvc | | ChainPollerSvc | | ChainPollerSvc | |
| 37 | +| (Service 1) | | (Service 2) | | (Service 3) | |
| 38 | ++----------------+ +-------------------+ +-------------------+ |
| 39 | + | | | |
| 40 | ++----------------+ +-------------------+ +-------------------+ |
| 41 | +| ChainPoller | | ChainPoller | | ChainPoller | |
| 42 | +| (Log Fetching) | | (Log Fetching) | | (Log Fetching) | |
| 43 | ++----------------+ +-------------------+ +-------------------+ |
| 44 | + | | | |
| 45 | ++----------------+ +-------------------+ +-------------------+ |
| 46 | +| Subscription | | Subscription | | Subscription | |
| 47 | +| Manager | | Manager | | Manager | |
| 48 | ++----------------+ +-------------------+ +-------------------+ |
| 49 | + | | | |
| 50 | ++----------------+ +-------------------+ +-------------------+ |
| 51 | +| Blockchain | | Blockchain | | Blockchain | |
| 52 | +| (Ethereum) | | (Polygon) | | (Arbitrum) | |
| 53 | ++----------------+ +-------------------+ +-------------------+ |
| 54 | +
|
| 55 | +``` |
| 56 | + |
| 57 | +### **Core Components** |
| 58 | +1. **Sentinel**: |
| 59 | + - Central coordinator managing multiple `ChainPollerService` instances. |
| 60 | + - Handles multi-chain subscriptions, lifecycle management, and configuration. |
| 61 | + |
| 62 | +2. **ChainPollerService**: |
| 63 | + - Polls blockchain logs and broadcasts events to subscribers. |
| 64 | + - Integrates `ChainPoller` and `SubscriptionManager`. |
| 65 | + |
| 66 | +3. **ChainPoller**: |
| 67 | + - Fetches logs from blockchain networks based on filter queries. |
| 68 | + |
| 69 | +4. **SubscriptionManager**: |
| 70 | + - Tracks subscriptions to blockchain events. |
| 71 | + - Broadcasts logs to subscribers. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## **Usage** |
| 76 | + |
| 77 | +### **Initialize Sentinel** |
| 78 | +Set up a `Sentinel` instance: |
| 79 | +```go |
| 80 | +import ( |
| 81 | + "github.com/smartcontractkit/chainlink-testing-framework/sentinel" |
| 82 | + "github.com/smartcontractkit/chainlink-testing-framework/sentinel/chain_poller_service" |
| 83 | +) |
| 84 | + |
| 85 | +logger := internal.NewDefaultLogger() |
| 86 | +sentinelInstance := sentinel.NewSentinel(sentinel.SentinelConfig{ |
| 87 | + Logger: logger, |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +### **Add a Chain** |
| 92 | +Add a blockchain to monitor: |
| 93 | +```go |
| 94 | +config := chain_poller_service.ChainPollerServiceConfig{ |
| 95 | + PollInterval: 100 * time.Millisecond, |
| 96 | + ChainPoller: chainPoller, |
| 97 | + Logger: logger, |
| 98 | + BlockchainClient: client, |
| 99 | + ChainID: 1, |
| 100 | +} |
| 101 | + |
| 102 | +err := sentinelInstance.AddChain(config) |
| 103 | +if err != nil { |
| 104 | + panic("Failed to add chain: " + err.Error()) |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### **Subscribe to Events** |
| 109 | +Subscribe to blockchain events: |
| 110 | +```go |
| 111 | +address := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") |
| 112 | +topic := common.HexToHash("0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef") |
| 113 | + |
| 114 | +logCh, err := sentinelInstance.Subscribe(1, address, topic) |
| 115 | +if err != nil { |
| 116 | + panic("Failed to subscribe: " + err.Error()) |
| 117 | +} |
| 118 | + |
| 119 | +// Listen for logs |
| 120 | +go func() { |
| 121 | + for log := range logCh { |
| 122 | + fmt.Println("Received log:", log) |
| 123 | + } |
| 124 | +}() |
| 125 | +``` |
| 126 | + |
| 127 | +### **Unsubscribe** |
| 128 | +Unsubscribe from events: |
| 129 | +```go |
| 130 | +err = sentinelInstance.Unsubscribe(1, address, topic, logCh) |
| 131 | +if err != nil { |
| 132 | + panic("Failed to unsubscribe: " + err.Error()) |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### **Remove a Chain** |
| 137 | +Remove a blockchain from monitoring: |
| 138 | +```go |
| 139 | +err = sentinelInstance.RemoveChain(1) |
| 140 | +if err != nil { |
| 141 | + panic("Failed to remove chain: " + err.Error()) |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## **Testing** |
| 148 | + |
| 149 | +### **Run Tests** |
| 150 | +Run tests for Sentinel and its components: |
| 151 | +```bash |
| 152 | +go test ./sentinel |
| 153 | +``` |
0 commit comments