Skip to content

Commit

Permalink
Add settlement module with batch add and verify functions
Browse files Browse the repository at this point in the history
- Implemented the settlement module in the sequencer.
- This module introduces 'add batch' and 'verify batch' functionalities.
- Enhances the sequencer's efficiency in handling batch operations.

Co-authored-by: Uddesh Jaiswal <[email protected]>
Co-authored-by: Rahul Singh Maraskole <[email protected]>
Co-authored-by: Shubham Sharma <[email protected]>
Co-authored-by: Kritarth Agrawal <[email protected]>
Co-authored-by: Shobhit Sharma <[email protected]>
  • Loading branch information
5 people committed Dec 22, 2023
1 parent 841e31d commit 7fa3328
Show file tree
Hide file tree
Showing 20 changed files with 636 additions and 58 deletions.
Binary file modified .DS_Store
Binary file not shown.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
![Project Logo](https://www.airchains.io/assets/logos/airchains-svm-rollup-full-logo.png)

# Overview

The SVM Chain Sequencer is a groundbreaking, high-performance tool developed to enhance transaction and block management on Smart Virtual Machine (SVM) chains. This innovative tool is distinguished by its integration of cutting-edge technologies and processes, tailored specifically for SVM chains, to ensure streamlined, efficient, and reliable blockchain operations.

## Table of Contents

- [Overview](#overview)
- [Table of Contents](#table-of-contents)
- [Key Features](#key-features)
- [Usage](#usage)
- [Script Files Overview](#script-files-overview)
- [License](#license)
- [Acknowledgments](#acknowledgments)

## Key Features

- **Optimized Transaction Processing**: Utilizes advanced algorithms to effectively manage and process transactions, significantly increasing throughput and reducing latency in SVM chain environments.

- **Sophisticated Block Management**: The sequencer is equipped with refined block management capabilities, ensuring smooth and efficient block generation and propagation within the SVM ecosystem.

- **Data Integrity and Availability**: Integrates robust Data Availability (DA) processes, vital for maintaining the integrity and accessibility of data on the chain. This feature enhances the trustworthiness and transparency of the SVM network.

- **Custom Batching Techniques**: Features bespoke batching techniques that are specifically designed for SVM chains, optimizing the handling of transaction loads and enhancing overall network performance.

- **Seamless Integration with SVM Layer**: Designed to seamlessly integrate with the SVM layer, the sequencer maintains consistent performance and compatibility, reinforcing the strength and stability of SVM-based applications.

- **High Scalability and Flexibility**: Built to accommodate the growing demands of SVM chains, offering scalable solutions that adapt to varying transaction volumes and network conditions.

- **Reliability and Security**: Prioritizes reliability and security in its design, ensuring that the SVM Chain Sequencer operates with high resilience and robustness, safeguarding against potential threats and vulnerabilities.

## Usage

In order to tailor the Sequencer to better align with your specific requirements, please proceed to update key configuration parameters within the `common/constants.go` file. The following constants are crucial for the optimal functioning of the sequencer and can be adjusted to meet your operational needs:

- **BatchSize**: Modify this value to alter the batch size for transaction processing. This adjustment can optimize throughput and efficiency based on your workload.

- **BlockDelay**: Adjust this constant to set the delay between blocks check, aligning it with your network's block generation rate for synchronized operations.

- **ExecutionClientRPC**: Update this URL to connect the sequencer with your preferred execution client's RPC interface.

- **SettlementClientRPC**: Change this URL to integrate the sequencer with the desired settlement layer's RPC service.

- **KeyringDirectory**: Specify a new directory path for the keyring, ensuring secure and organized storage of cryptographic keys.

- **DaClientRPC**: Alter this URL to link the sequencer with your chosen Data Availability (DA) service's RPC endpoint.

Each of these parameters plays a critical role in the configuration and performance of the sequencer. It is recommended to carefully consider the implications of these changes to maintain optimal functionality and security of the system.

## Script Files Overview

This repository includes a set of four essential script files designed to facilitate various operational aspects of the node. Each script has a specific purpose, detailed below:

- **init.sh**: This script is responsible for setting up the initial environment. It creates all necessary folders and files required for the node's operation.

```
sh scripts/init.sh
```

- **start.sh**: Designed to simply start the node without altering its current state or block height. Ideal for routine starts where no changes to the environment are needed.

```
sh scripts/start.sh
```

- **restart.sh**: Used for refreshing the node's environment. It first deletes and then recreates all folders and files, followed by restarting the node to apply these changes.

```
sh scripts/restart.sh
```

- **test.sh**: As the name suggests, this script is used for testing purposes. It contains commands and procedures specifically intended for testing the node's functionalities and performance.

```
sh scripts/test.sh
```

> Note: before proceeding to run the sequencer, please ensure that the `init.sh` script has been executed to initialize the basic directory structure and configuration files.
_Important Security Notice Regarding init.sh Execution_

Please be aware that running the `init.sh` script necessitates the entry of your terminal password. This requirement stems from the inclusion of `sudo` commands within the script. These commands elevate privileges for certain operations, which are essential for the correct setup and configuration of the environment.

## License

This project is licensed under the MIT License - see the LICENSE file for details.

## Acknowledgments

Special thanks to the `gnark` library, an efficient and elegant toolkit for zk-SNARKs on Go. This library has been instrumental in our development process. For more information and to explore their work, visit their GitHub repository at [Consensys/gnark.](https://github.com/Consensys/gnark)
7 changes: 2 additions & 5 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package common
const BatchSize = 25
const BlockDelay = 5

// ExecutionClientRPC const ExecutionClientRPC = "http://192.168.1.24:8545/"
const ExecutionClientRPC = "http://127.0.0.1:8899/"

// const ExecutionClientRPC = "http://192.168.1.70:8899/"
// const DaClientRPC = "http://192.168.1.105:5050/celestia"
const DaClientRPC = "http://192.168.1.105:5050/celestia"
const DaClientRPC = "http://192.168.1.106:5050/celestia"
const SettlementClientRPC = "http://192.168.1.105:8080"
2 changes: 1 addition & 1 deletion common/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/airchains-network/solana-seq-indexer/common/logs"
"github.com/airchains-network/svm-sequencer-node/common/logs"
"github.com/btcsuite/btcutil/base58"
"math/big"
"net/http"
Expand Down
15 changes: 15 additions & 0 deletions config/chainInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"chainInfo": {
"chainID": "aircosmic_2605-1107",
"key": "AccountName",
"moniker": "ChainName"
},
"daInfo": {
"daSelected": "Celestia",
"daWalletAddress": "0xaddress",
"daWalletKeypair": "0xaddress"
},
"sequencerInfo": {
"sequencerType": "Air Sequencer"
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/airchains-network/solana-seq-indexer
module github.com/airchains-network/svm-sequencer-node

go 1.21

Expand Down
29 changes: 22 additions & 7 deletions handlers/batchCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package handlers
import (
"encoding/json"
"fmt"
"github.com/airchains-network/solana-seq-indexer/common"
"github.com/airchains-network/solana-seq-indexer/common/logs"
"github.com/airchains-network/solana-seq-indexer/prover"
"github.com/airchains-network/solana-seq-indexer/types"
"github.com/airchains-network/svm-sequencer-node/common"
"github.com/airchains-network/svm-sequencer-node/common/logs"
settlement_client "github.com/airchains-network/svm-sequencer-node/handlers/settlement-client"
"github.com/airchains-network/svm-sequencer-node/prover"
"github.com/airchains-network/svm-sequencer-node/types"
"github.com/syndtr/goleveldb/leveldb"
"os"
"strconv"
Expand Down Expand Up @@ -83,20 +84,34 @@ func BatchGeneration(wg *sync.WaitGroup, lds *leveldb.DB, ldt *leveldb.DB, ldbat
batch.AccountNonces = AccountNonces

// add prover here
_, currentStateHash, pkErr := prover.GenerateProof(batch, limitInt+1)
witnessVector, currentStatusHash, proofByte, pkErr := prover.GenerateProof(batch, limitInt+1)
if pkErr != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error in generating proof : %s", pkErr.Error()))
os.Exit(0)
}

// !adding Da client here
daKeyHash, err := DaCall(batch.TransactionHash, currentStateHash, limitInt+1, ldDA)
daKeyHash, err := DaCall(batch.TransactionHash, currentStatusHash, limitInt+1, ldDA)
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error in adding Da client : %s", err.Error()))
os.Exit(0)
logs.LogMessage("INFO:", "Waiting for 3 seconds")
time.Sleep(3 * time.Second)
BatchGeneration(wg, lds, ldt, ldbatch, ldDA, []byte(strconv.Itoa(common.BatchSize*(limitInt+1))))
}
logs.LogMessage("SUCCESS:", fmt.Sprintf("Successfully added Da client for Batch %s in the latest phase", daKeyHash))

addBatchRes := settlement_client.AddBatch(witnessVector, limitInt+1, lds)
if addBatchRes == "nil" {
logs.LogMessage("ERROR:", fmt.Sprintf("Error in adding batch to settlement client : %s", addBatchRes))
os.Exit(0)
}

status := settlement_client.VerifyBatch(limitInt+1, proofByte, ldDA, lds)
if !status {
logs.LogMessage("ERROR:", fmt.Sprintf("Error in verifying batch to settlement client : %s", status))
os.Exit(0)
}

logs.LogMessage("SUCCESS:", fmt.Sprintf("Successfully generated proof for Batch %s in the latest phase", strconv.Itoa(limitInt+1)))

batchJSON, err := json.Marshal(batch)
Expand Down
4 changes: 2 additions & 2 deletions handlers/blockCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/airchains-network/solana-seq-indexer/common"
"github.com/airchains-network/solana-seq-indexer/common/logs"
"github.com/airchains-network/svm-sequencer-node/common"
"github.com/airchains-network/svm-sequencer-node/common/logs"
"github.com/syndtr/goleveldb/leveldb"
"log"
"net/http"
Expand Down
6 changes: 3 additions & 3 deletions handlers/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/airchains-network/solana-seq-indexer/common"
"github.com/airchains-network/solana-seq-indexer/common/logs"
"github.com/airchains-network/solana-seq-indexer/types"
"github.com/airchains-network/svm-sequencer-node/common"
"github.com/airchains-network/svm-sequencer-node/common/logs"
"github.com/airchains-network/svm-sequencer-node/types"
"github.com/syndtr/goleveldb/leveldb"
"net/http"
"os"
Expand Down
10 changes: 5 additions & 5 deletions handlers/daCall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
air "github.com/airchains-network/solana-seq-indexer/airdb/air-leveldb"
"github.com/airchains-network/solana-seq-indexer/common"
"github.com/airchains-network/solana-seq-indexer/common/logs"
"github.com/airchains-network/solana-seq-indexer/types"
air "github.com/airchains-network/svm-sequencer-node/airdb/air-leveldb"
"github.com/airchains-network/svm-sequencer-node/common"
"github.com/airchains-network/svm-sequencer-node/common/logs"
"github.com/airchains-network/svm-sequencer-node/types"
"github.com/syndtr/goleveldb/leveldb"
"net/http"
"strconv"
Expand Down Expand Up @@ -95,7 +95,7 @@ func DaCall(transactions []string, currentStateHash string, batchNumber int, ldd
DAKey: response.DaKeyHash,
DAClientName: "celestia",
BatchNumber: strconv.Itoa(batchNumber),
PreviousStateHash: daDecode.PreviousStateHash,
PreviousStateHash: daDecode.CurrentStateHash,
CurrentStateHash: currentStateHash,
}

Expand Down
95 changes: 95 additions & 0 deletions handlers/settlement-client/addBatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package settlement_client

import (
"bytes"
"encoding/json"
"fmt"
"github.com/airchains-network/svm-sequencer-node/common"
"github.com/airchains-network/svm-sequencer-node/common/logs"
"github.com/airchains-network/svm-sequencer-node/types"
"github.com/syndtr/goleveldb/leveldb"
"io"
"net/http"
"time"
)

type PostAddBatchStruct struct {
ChainID string `json:"chain_id"`
BatchNumber uint64 `json:"batch_number"`
Witness []byte `json:"witness"`
}

func AddBatch(witnessVector any, batchNumber int, lds *leveldb.DB) string {

logs.LogMessage("INFO:", "Submitting batch to settlement")

settlementChainInfoByte, err := lds.Get([]byte("settlementChainInfo"), nil)
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error in getting settlementChainInfo from static db : %s", err.Error()))
return "nil"
}

var settlementChainInfo types.SettlementLayerChainInfoStruct
err = json.Unmarshal(settlementChainInfoByte, &settlementChainInfo)
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error in unmarshalling settlementChainInfo : %s", err.Error()))
return "nil"
}
chainID := settlementChainInfo.ChainId

//var frVector fr.Vector

wvByte, _ := json.Marshal(witnessVector)

postAddBatchStruct := PostAddBatchStruct{
ChainID: chainID,
BatchNumber: uint64(batchNumber),
Witness: wvByte,
}

jsonData, err := json.Marshal(postAddBatchStruct)
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error in marshalling postAddBatchStruct : %s", err.Error()))
return "nil"
}
rpcUrl := fmt.Sprintf("%s/add_batch", common.SettlementClientRPC)
req, err := http.NewRequest("POST", rpcUrl, bytes.NewBuffer(jsonData))
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error creating request:", err))
return "nil"
}

req.Header.Set("Content-Type", "application/json")
logs.LogMessage("INFO:", "Sending request to settlement client")
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error sending request:", err))
return "nil"
}
defer resp.Body.Close()

// Read and unmarshal the response
body, err := io.ReadAll(resp.Body)
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error reading response:", err))
return "nil"
}

var response types.SettlementClientResponseStruct
err = json.Unmarshal(body, &response)
if err != nil {
logs.LogMessage("ERROR:", fmt.Sprintf("Error unmarshalling response:", err))
return "nil"
}

if !response.Status {
logs.LogMessage("ERROR:", "error in adding batch to settlement")
logs.LogMessage("INFO:", "Retrying in 5 seconds...")
time.Sleep(5 * time.Second)
AddBatch(witnessVector, batchNumber, lds)
}

return response.Data
}
Loading

0 comments on commit 7fa3328

Please sign in to comment.