Skip to content

Commit

Permalink
Fix ut
Browse files Browse the repository at this point in the history
  • Loading branch information
zjg555543 committed Dec 16, 2024
1 parent c849c53 commit 3a54e69
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ build-go:
$(GOENVVARS) go build -ldflags "all=$(LDFLAGS)" -o $(GOBIN)/$(GOBINARY) $(GOCMD)

.PHONY: build-docker
build-docker:
build-docker: build-mock-signer-docker ## Builds a docker image with the cdk binary
docker build -t cdk -f ./Dockerfile .

.PHONY: build-mock-signer-docker
Expand Down
10 changes: 8 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/0xPolygon/cdk/db/types"
"math/big"
"net/http"
"os"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/0xPolygon/cdk/dataavailability"
"github.com/0xPolygon/cdk/dataavailability/datacommittee"
sqldb "github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/db/types"
"github.com/0xPolygon/cdk/etherman"
ethermanconfig "github.com/0xPolygon/cdk/etherman/config"
"github.com/0xPolygon/cdk/etherman/contracts"
Expand Down Expand Up @@ -821,6 +821,11 @@ func runSqliteServiceIfNeeded(
components []string,
cfg config.Config,
) {
if !cfg.Sqlite.Enabled {
log.Warn(fmt.Sprintf("Sqlite service is disabled"))
return
}

dbPath := make(map[string]string)
if isNeeded([]string{
cdkcommon.AGGREGATOR},
Expand All @@ -845,7 +850,8 @@ func runSqliteServiceIfNeeded(
}

server := sqldb.CreateSqliteService(cfg.Sqlite, dbPath)
log.Info(fmt.Sprintf("Starting sqlite service on %s:%d,max:%v,\n%v", cfg.Sqlite.Host, cfg.Sqlite.Port, cfg.Sqlite.MaxRequestsPerIPAndSecond, allDBPath))
log.Info(fmt.Sprintf("Starting sqlite service on %s:%d,max:%v,\n%v",
cfg.Sqlite.Host, cfg.Sqlite.Port, cfg.Sqlite.MaxRequestsPerIPAndSecond, allDBPath))
go func() {
if err := server.Start(); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"github.com/0xPolygon/cdk/db"
"os"
"strings"

Expand All @@ -15,6 +14,7 @@ import (
"github.com/0xPolygon/cdk/bridgesync"
"github.com/0xPolygon/cdk/claimsponsor"
"github.com/0xPolygon/cdk/common"
"github.com/0xPolygon/cdk/db"
ethermanconfig "github.com/0xPolygon/cdk/etherman/config"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/lastgersync"
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ DelayBeetweenRetries = "60s"
KeepCertificatesHistory = true
[Sqlite]
Enabled = false
Host = "0.0.0.0"
Port = 8081
ReadTimeout = "2s"
Expand Down
3 changes: 3 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import "github.com/0xPolygon/cdk-rpc/config/types"

// Config represents the configuration of the json rpc
type Config struct {
// Enabled defines if the HTTP server is enabled
Enabled bool `mapstructure:"Enabled"`

// Host defines the network adapter that will be used to serve the HTTP requests
Host string `mapstructure:"Host"`

Expand Down
207 changes: 207 additions & 0 deletions db/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@

# SQLite JSON-RPC Operations
```json

[Sqlite]
Enabled = true
Host = "0.0.0.0"
Port = 8081
ReadTimeout = "2s"
WriteTimeout = "2s"
AuthMethodList = "select,insert,update,delete"
MaxRequestsPerIPAndSecond = 500

```

## Table of Contents

1. [SQLite JSON-RPC Methods](#sqlite-json-rpc-methods)
- [sqlite_getDbs](#sqlite_getdbs)
- [sqlite_select](#sqlite_select)
- [sqlite_insert](#sqlite_insert)
- [sqlite_update](#sqlite_update)
- [sqlite_delete](#sqlite_delete)
2. [Example Requests](#example-requests)

## 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": []
}
```

#### Response:
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"seqs_l1tree": [
"block",
"gorp_migrations",
"l1_info_rht",
"l1_info_root",
"l1info_initial",
"l1info_leaf",
"rollup_exit_rht",
"rollup_exit_root",
"verify_batches"
],
"seqs_reorg_l1": [
"gorp_migrations",
"tracked_block"
],
"seqs_txmgr": [
"gorp_migrations",
"monitored_txs"
]
},
"error": null
}
```

---

### 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": ["seqs_txmgr", "SELECT * FROM monitored_txs WHERE id='0x1c89ee4fbd62bcf844dacb5157df8fdf9036f210a1e3200d610ef1f778f2c32c';"]
}
```

#### Response:
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": [
{
"Fields": {
"blob_gas": 0,
"blob_gas_price": null,
"blob_sidecar": null,
"block_number": 891,
"created_at": "2024-12-16T02:12:34Z",
"estimate_gas": 0,
"from_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"gas": 224513,
"gas_offset": 80000,
"gas_price": "1",
"gas_tip_cap": null,
"history": null,
"id": "0x1c89ee4fbd62bcf844dacb5157df8fdf9036f210a1e3200d610ef1f778f2c32c",
"nonce": 117,
"status": "finalized",
"to_address": "0x1b173087729c88a47568AF87b17C653039377BA6",
"tx_data": null,
"updated_at": "2024-12-16T02:12:54Z",
"value": "0"
}
}
],
"error": null
}
```
---

### sqlite_insert

This method inserts a new record into the `monitored_txs` table. The transaction details such as `from_address`, `to_address`, `gas`, `block_number`, etc., are inserted into the table.

#### Request:
```json
{
"id": 1,
"jsonrpc": "2.0",
"method": "sqlite_insert",
"params": ["seqs_txmgr", "INSERT INTO monitored_txs (blob_gas, blob_gas_price, block_number, created_at, estimate_gas, from_address, gas, gas_offset, gas_price, gas_tip_cap, id, nonce, status, to_address, updated_at, value) VALUES (0, NULL, 891, '2024-12-16T02:12:34Z', 0, '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 224513, 80000, '1', NULL, '0x1c89ee4fbd62bcf844dacb5157df8fdf9036f210a1e3200d610ef1f778f2c32c', 117, 'finalized', '0x1b173087729c88a47568AF87b17C653039377BA6', '2024-12-16T02:12:54Z', '0');
}
```

#### Response:
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"RowsAffected": 1
},
"error": null
}
```

---

### sqlite_update

This method updates an existing record in the `monitored_txs` table. In this example, it updates the `to_address` field for a transaction with the specified ID.

#### Request:
```json
{
"id": 1,
"jsonrpc": "2.0",
"method": "sqlite_update",
"params": ["seqs_txmgr", "UPDATE monitored_txs SET to_address = '0x2b173087729c88a47568AF87b17C653039377BA6' WHERE id = '0x1c89ee4fbd62bcf844dacb5157df8fdf9036f210a1e3200d610ef1f778f2c32c';"]
}
```

#### Response:
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"RowsAffected": 1
},
"error": null
}
```

---

### sqlite_delete

This method deletes a record from the `monitored_txs` table. In this example, it deletes the transaction with the specified ID.

#### Request:
```json
{
"id": 1,
"jsonrpc": "2.0",
"method": "sqlite_delete",
"params": ["seqs_txmgr", "DELETE FROM monitored_txs WHERE id = '0x1c89ee4fbd62bcf844dacb5157df8fdf9036f210a1e3200d610ef1f778f2c32c';"]
}
```

#### Response:
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"RowsAffected": 1
},
"error": null
}
```

---
Loading

0 comments on commit 3a54e69

Please sign in to comment.