Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/agglayer/aggkit/prometheus"
"github.com/agglayer/aggkit/reorgdetector"
"github.com/agglayer/aggkit/rpc"
"github.com/agglayer/aggkit/sqliteapi"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -152,6 +153,11 @@ func start(cliCtx *cli.Context) error {
go pprof.StartProfilingHTTPServer(cliCtx.Context, cfg.Profiling)
}

// Start SQLite API service if enabled
if cfg.SqliteAPI.Enabled {
go startSQLiteAPIService(cliCtx.Context, cfg.SqliteAPI, cfg)
}

waitSignal(nil)

return nil
Expand Down Expand Up @@ -713,3 +719,23 @@ func startPrometheusHTTPServer(c prometheus.Config) {
return
}
}

func startSQLiteAPIService(ctx context.Context, cfg sqliteapi.Config, mainCfg *config.Config) {
logger := log.WithFields("module", "sqliteapi")

// Build database paths map from configuration
dbPaths := make(map[string]string)

// Add all configured databases
dbPaths["L1InfoTreeSync"] = mainCfg.L1InfoTreeSync.DBPath
dbPaths["BridgeL2Sync"] = mainCfg.BridgeL2Sync.DBPath
dbPaths["AggSender"] = mainCfg.AggSender.StoragePath
dbPaths["ReorgDetectorL1"] = mainCfg.ReorgDetectorL1.DBPath
dbPaths["ReorgDetectorL2"] = mainCfg.ReorgDetectorL2.DBPath

service := sqliteapi.NewService(cfg, dbPaths, logger)

if err := service.Start(); err != nil {
log.Fatal("Failed to start SQLite API service: ", err)
}
}
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/agglayer/aggkit/pprof"
"github.com/agglayer/aggkit/prometheus"
"github.com/agglayer/aggkit/reorgdetector"
"github.com/agglayer/aggkit/sqliteapi"
"github.com/mitchellh/mapstructure"
"github.com/pelletier/go-toml/v2"
"github.com/spf13/viper"
Expand Down Expand Up @@ -157,6 +158,9 @@ type Config struct {

// Profiling is the configuration of the profiling service
Profiling pprof.Config

// SqliteAPI is the configuration for the SQLite API service
SqliteAPI sqliteapi.Config
}

// Load loads the configuration
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go v1.0.3 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/go-bexpr v0.1.11 // indirect
github.com/holiman/billy v0.0.0-20240216141850-2abb0c79d3c4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ github.com/googleapis/gax-go v1.0.3/go.mod h1:QyXYajJFdARxGzjwUfbDFIse7Spkw81SJ4
github.com/googleapis/gax-go/v2 v2.0.2/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q=
github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/go-bexpr v0.1.11 h1:6DqdA/KBjurGby9yTY0bmkathya0lfwF2SeuubCI7dY=
Expand Down
183 changes: 183 additions & 0 deletions sqliteapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# SQLite API Service

This module provides a REST API service for interacting with SQLite databases used by various components of the aggkit project.

## Features

- **Database List**: Get list of available databases with their tables
- **Select Support**: Execute SELECT queries on SQLite databases
- **Rate Limiting**: Configurable rate limiting per IP address
- **CORS Support**: Cross-origin resource sharing enabled
- **JSON-RPC 2.0**: Standard JSON-RPC 2.0 protocol support

## Configuration

The SQLite API service can be configured in the main configuration file:

```toml
[SqliteAPI]
Enabled = true
Host = "0.0.0.0"
Port = 8080
MaxRequestsPerIPAndSecond = 10
```

### Configuration Options

- `Enabled`: Enable or disable the SQLite API service
- `Host`: Host address to bind the service to
- `Port`: Port number for the service
- `MaxRequestsPerIPAndSecond`: Maximum number of requests allowed per IP address per second

## SQLite JSON-RPC Methods

The following JSON-RPC methods can be used to perform database operations:

### sqlite_getDbs

This method retrieves the list of databases managed by the SQLite transaction manager.

#### Request:
```json
{
"id": 1,
"jsonrpc": "2.0",
"method": "sqlite_getDbs",
"params": []
}
```

---

### sqlite_select

This method executes a SQL `SELECT` query on the `monitored_txs` table to retrieve transaction data for a specific transaction ID.

#### Request:
```json
{
"id": 1,
"jsonrpc": "2.0",
"method": "sqlite_select",
"params": ["AggSender", "SELECT * FROM certificate_info LIMIT 2;"]
}
```



## Database Types

The following database types are supported:

- `L1InfoTreeSync`: L1 info tree sync database (L1InfoTreeSync.sqlite)
- `BridgeL2Sync`: Bridge L2 sync database (bridgel2sync.sqlite)
- `AggSender`: Aggregator sender database (aggsender.sqlite)
- `ReorgDetectorL1`: L1 reorg detector database (reorgdetectorl1.sqlite)
- `ReorgDetectorL2`: L2 reorg detector database (reorgdetectorl2.sqlite)

## Error Handling

The API returns standard JSON-RPC 2.0 error responses:

```json
{
"jsonrpc": "2.0",
"id": 1,
"result": null,
"error": {
"code": 400,
"message": "Invalid request body"
}
}
```



## Rate Limiting

The service implements rate limiting based on client IP addresses. Each IP is limited to the number of requests specified in `MaxRequestsPerIPAndSecond` configuration.

When rate limit is exceeded, the service returns HTTP 429 (Too Many Requests) status code.

## Security Considerations

- The service binds to all interfaces by default (`0.0.0.0`)
- Rate limiting helps prevent abuse
- CORS is enabled for cross-origin requests
- Only SELECT statements are allowed - all data modification operations are blocked
- Input validation is performed on all requests

## Usage Example

```bash
# Get available databases
curl -X POST http://localhost:8080/ \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "sqlite_getDbs",
"params": []
}'

# Response includes:
# - AggSender: ["gorp_migrations", "certificate_info", "certificate_info_history", "key_value"]
# - BridgeL2Sync: ["gorp_migrations", "key_value", "block", "bridge", "claim", "root", "rht"]
# - L1InfoTreeSync: ["gorp_migrations", "key_value", "l1_info_root", "l1_info_rht", "block", "l1info_leaf", "verify_batches", "l1info_initial", "rollup_exit_root", "rollup_exit_rht"]
# - ReorgDetectorL1: ["gorp_migrations", "key_value", "tracked_block", "reorg_event"]
# - ReorgDetectorL2: ["gorp_migrations", "key_value", "tracked_block", "reorg_event"]



# Select example - Query certificate_info table
curl -X POST http://localhost:8080/ \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "sqlite_select",
"params": ["AggSender", "SELECT * FROM certificate_info LIMIT 2;"]
}'

# Select example - Query ReorgDetectorL1 tracked_block table
curl -X POST http://localhost:8080/ \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "sqlite_select",
"params": ["ReorgDetectorL1", "SELECT * FROM tracked_block LIMIT 2;"]
}'



# Error example - Attempting DELETE operation (blocked)
curl -X POST http://localhost:8080/ \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "sqlite_select",
"params": ["AggSender", "DELETE FROM certificate_info WHERE id = 1;"]
}'


```

## Integration with aggkit

The SQLite API service is automatically started when the aggkit application starts and the `SqliteAPI.Enabled` configuration is set to `true`. The service runs in a separate goroutine and provides HTTP endpoints for database operations.

The service maps database types to the actual database file paths used by various aggkit components, allowing external applications to interact with the underlying SQLite databases.

## Database File Locations

The service uses the actual database paths configured in the main configuration file:

- `L1InfoTreeSync`: Uses `L1InfoTreeSync.DBPath` from configuration (default: `/tmp/aggkit/L1InfoTreeSync.sqlite`)
- `BridgeL2Sync`: Uses `BridgeL2Sync.DBPath` from configuration (default: `/tmp/aggkit/bridgel2sync.sqlite`)
- `AggSender`: Uses `AggSender.StoragePath` from configuration (default: `/tmp/aggkit/aggsender.sqlite`)
- `ReorgDetectorL1`: Uses `ReorgDetectorL1.DBPath` from configuration (default: `/tmp/aggkit/reorgdetectorl1.sqlite`)
- `ReorgDetectorL2`: Uses `ReorgDetectorL2.DBPath` from configuration (default: `/tmp/aggkit/reorgdetectorl2.sqlite`)

The SQLite API service will attempt to access all configured databases. If a database file doesn't exist or is not accessible, the service will log a warning but continue to operate with the available databases.
13 changes: 13 additions & 0 deletions sqliteapi/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sqliteapi

// Config is the configuration for the SQLite API service
type Config struct {
// Enabled is a flag to enable the SQLite API service
Enabled bool `mapstructure:"Enabled"`
// Host is the host address for the SQLite API service
Host string `mapstructure:"Host"`
// Port is the port for the SQLite API service
Port int `mapstructure:"Port"`
// MaxRequestsPerIPAndSecond is the maximum number of requests per IP per second
MaxRequestsPerIPAndSecond int `mapstructure:"MaxRequestsPerIPAndSecond"`
}
Loading