Skip to content

Commit

Permalink
stashing initial
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Feb 20, 2024
1 parent 1cc3481 commit f5c7763
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 531 deletions.
19 changes: 19 additions & 0 deletions go/common/storage/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package storage

import gethcommon "github.com/ethereum/go-ethereum/common"

const truncHash = 16

func truncTo16(hash gethcommon.Hash) []byte {
return truncBTo16(hash.Bytes())
}

func truncBTo16(bytes []byte) []byte {
if len(bytes) == 0 {
return bytes
}
b := bytes[0:truncHash]
c := make([]byte, truncHash)
copy(c, b)
return c
}
7 changes: 5 additions & 2 deletions go/config/host_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ type HostConfig struct {
LogPath string
// Whether the host should use in-memory or persistent storage
UseInMemoryDB bool
// filepath for the levelDB persistence dir (can be empty if a throwaway file in /tmp/ is acceptable, or if using InMemory DB)
LevelDBPath string
// Host address for Maria DB instance (can be empty if using InMemory DB or if attestation is disabled)
MariaDBHost string
// filepath for the sqlite DB persistence file (can be empty if a throwaway file in /tmp/ is acceptable or
// if using InMemory DB)
SqliteDBPath string

//////
// NODE NETWORKING
Expand Down
6 changes: 3 additions & 3 deletions go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package enclave

import (
"database/sql"
"fmt"
"github.com/ten-protocol/go-ten/go/host/storage/hostdb"
"math/big"
"strings"
"sync"
Expand Down Expand Up @@ -56,7 +56,7 @@ type Guardian struct {
enclaveClient common.Enclave

sl guardianServiceLocator
db *db.DB
db *sql.DB

submitDataLock sync.Mutex // we only submit one block, batch or transaction to enclave at a time

Expand All @@ -74,7 +74,7 @@ type Guardian struct {
enclaveID *common.EnclaveID
}

func NewGuardian(cfg *config.HostConfig, hostData host.Identity, serviceLocator guardianServiceLocator, enclaveClient common.Enclave, db *db.DB, interrupter *stopcontrol.StopControl, logger gethlog.Logger) *Guardian {
func NewGuardian(cfg *config.HostConfig, hostData host.Identity, serviceLocator guardianServiceLocator, enclaveClient common.Enclave, db *sql.DB, interrupter *stopcontrol.StopControl, logger gethlog.Logger) *Guardian {
return &Guardian{
hostData: hostData,
state: NewStateTracker(logger),
Expand Down
10 changes: 5 additions & 5 deletions go/host/host.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package host

import (
"database/sql"
"encoding/json"
"fmt"
"github.com/ten-protocol/go-ten/go/host/storage/hostdb"

gethcommon "github.com/ethereum/go-ethereum/common"

"github.com/ten-protocol/go-ten/go/host/l2"

"github.com/ten-protocol/go-ten/go/host/enclave"
"github.com/ten-protocol/go-ten/go/host/l1"
"github.com/ten-protocol/go-ten/go/host/storage"

"github.com/ethereum/go-ethereum/rpc"
"github.com/naoina/toml"
Expand Down Expand Up @@ -39,7 +39,7 @@ type host struct {
// ignore incoming requests
stopControl *stopcontrol.StopControl

db *db.DB // Stores the host's publicly-available data
db *sql.DB // Stores the host's publicly-available data

logger gethlog.Logger

Expand All @@ -50,7 +50,7 @@ type host struct {
}

func NewHost(config *config.HostConfig, hostServices *ServicesRegistry, p2p hostcommon.P2PHostService, ethClient ethadapter.EthClient, l1Repo hostcommon.L1RepoService, enclaveClient common.Enclave, ethWallet wallet.Wallet, mgmtContractLib mgmtcontractlib.MgmtContractLib, logger gethlog.Logger, regMetrics gethmetrics.Registry) hostcommon.Host {
database, err := db.CreateDBFromConfig(config, regMetrics, logger)
database, err := storage.CreateDBFromConfig(config, logger)
if err != nil {
logger.Crit("unable to create database for host", log.ErrKey, err)
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func (h *host) Config() *config.HostConfig {
return h.config
}

func (h *host) DB() *db.DB {
func (h *host) DB() *sql.DB {
return h.db
}

Expand Down
6 changes: 3 additions & 3 deletions go/host/l2/batchrepository.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package l2

import (
"database/sql"
"errors"
"github.com/ten-protocol/go-ten/go/host/storage/hostdb"
"math/big"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -36,7 +36,7 @@ type Repository struct {
subscribers []host.L2BatchHandler

sl batchRepoServiceLocator
db *db.DB
db *sql.DB
isSequencer bool

// high watermark for batch sequence numbers seen so far. If we can't find batch for seq no < this, then we should ask peers for missing batches
Expand All @@ -54,7 +54,7 @@ type Repository struct {
logger gethlog.Logger
}

func NewBatchRepository(cfg *config.HostConfig, hostService batchRepoServiceLocator, database *db.DB, logger gethlog.Logger) *Repository {
func NewBatchRepository(cfg *config.HostConfig, hostService batchRepoServiceLocator, database *sql.DB, logger gethlog.Logger) *Repository {
return &Repository{
sl: hostService,
db: database,
Expand Down
30 changes: 9 additions & 21 deletions go/host/storage/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"database/sql"
"fmt"

"github.com/ten-protocol/go-ten/go/enclave/storage/enclavedb"

"github.com/ten-protocol/go-ten/go/common/storage/database/init/sqlite"
"github.com/ten-protocol/go-ten/go/enclave/storage/init/edgelessdb"

Expand All @@ -22,39 +20,29 @@ func CreateDBFromConfig(cfg *config.HostConfig, logger gethlog.Logger) (*sql.DB,
if cfg.UseInMemoryDB {
logger.Info("UseInMemoryDB flag is true, data will not be persisted. Creating in-memory database...")
// this creates a temporary sqlite sqldb
return sqlite.CreateTemporarySQLiteHostDB(cfg.HostID.String(), "mode=memory&cache=shared&_foreign_keys=on", logger, "001_init.sql")
return sqlite.CreateTemporarySQLiteHostDB(cfg.ID.String(), "mode=memory&cache=shared&_foreign_keys=on", logger, "001_init.sql")
}

// persistent and with attestation means connecting to edgeless DB in a trusted enclave from a secure enclave
logger.Info(fmt.Sprintf("Preparing Edgeless DB connection to %s...", cfg.EdgelessDBHost))
return nil, err
//return getEdgelessDB(cfg, logger)
logger.Info(fmt.Sprintf("Preparing Edgeless DB connection to %s...", cfg.MariaDBHost))
return getMariaDBHost(cfg, logger)
}

// validateDBConf high-level checks that you have a valid configuration for DB creation
func validateDBConf(cfg *config.HostConfig) error {
if cfg.UseInMemoryDB && cfg.EdgelessDBHost != "" {
return fmt.Errorf("invalid db config, useInMemoryDB=true so EdgelessDB host not expected, but EdgelessDBHost=%s", cfg.EdgelessDBHost)
}
if !cfg.WillAttest && cfg.EdgelessDBHost != "" {
return fmt.Errorf("invalid db config, willAttest=false so EdgelessDB host not supported, but EdgelessDBHost=%s", cfg.EdgelessDBHost)
}
if !cfg.UseInMemoryDB && cfg.WillAttest && cfg.EdgelessDBHost == "" {
return fmt.Errorf("useInMemoryDB=false, willAttest=true so expected an EdgelessDB host but none was provided")
if cfg.UseInMemoryDB && cfg.MariaDBHost != "" {
return fmt.Errorf("invalid db config, useInMemoryDB=true so MariaDB host not expected, but MariaDBHost=%s", cfg.MariaDBHost)
}
if cfg.SqliteDBPath != "" && cfg.UseInMemoryDB {
return fmt.Errorf("useInMemoryDB=true so sqlite database will not be used and no path is needed, but sqliteDBPath=%s", cfg.SqliteDBPath)
}
if cfg.SqliteDBPath != "" && cfg.WillAttest {
return fmt.Errorf("willAttest=true so sqlite database will not be used and no path is needed, but sqliteDBPath=%s", cfg.SqliteDBPath)
}
return nil
}

func getEdgelessDB(cfg *config.EnclaveConfig, logger gethlog.Logger) (enclavedb.EnclaveDB, error) {
if cfg.EdgelessDBHost == "" {
return nil, fmt.Errorf("failed to prepare EdgelessDB connection - EdgelessDBHost was not set on enclave config")
func getMariaDBHost(cfg *config.HostConfig, logger gethlog.Logger) (*sql.DB, error) {
if cfg.MariaDBHost == "" {
return nil, fmt.Errorf("failed to prepare MariaDB connection - MariaDBHost was not set on host config")
}
dbConfig := edgelessdb.Config{Host: cfg.EdgelessDBHost}
dbConfig := edgelessdb.Config{Host: cfg.MariaDBHost}
return edgelessdb.Connector(&dbConfig, logger)
}
192 changes: 192 additions & 0 deletions go/host/storage/hostdb/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package hostdb

import (
"database/sql"
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"math/big"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ten-protocol/go-ten/go/common"
)

// AddBatch adds a batch and its header to the DB
func AddBatch(db *sql.DB, batch *common.ExtBatch) error {
return BeginTx(db, func(tx *sql.Tx) error {

batchBodyStmt, err := db.Prepare("INSERT INTO batch_body (id, body) VALUES (?, ?)")
if err != nil {
return fmt.Errorf("failed to prepare body insert statement: %w", err)
}
defer batchBodyStmt.Close()

// BATCH INSERT
batchStmt, err := db.Prepare("INSERT INTO batches (sequence, full_hash, hash, height, tx_count, header_blob, body_id) VALUES (?, ?, ?, ?, ?, ?)")
if err != nil {
return fmt.Errorf("failed to prepare batch insert statement: %w", err)
}
defer batchStmt.Close()

//TX INSERT
txStmt, err := db.Prepare("INSERT INTO transactions (tx_hash_indexed, tx_hash_full, content, sender_address, nonce, idx, body_id) VALUES (?, ?, ?, ?, ?, ?, ?)")
if err != nil {
return fmt.Errorf("failed to prepare transaction insert statement: %w", err)
}
defer txStmt.Close()

// Encode batch data
batchBodyID := batch.Header.SequencerOrderNo.Uint64()
body, err := rlp.EncodeToBytes(batch.EncryptedTxBlob)
if err != nil {
return fmt.Errorf("could not encode L2 transactions: %w", err)
}
header, err := rlp.EncodeToBytes(batch.Header)
if err != nil {
return fmt.Errorf("could not encode batch header: %w", err)
}

// Execute body insert
_, err = batchBodyStmt.Exec(batchBodyID, body)
if err != nil {
return fmt.Errorf("failed to insert body: %w", err)
}

_, err = batchStmt.Exec(
batch.Header.SequencerOrderNo.Uint64(), // sequence
batch.Hash(), // full hash
truncTo16(batch.Hash()), // shortened hash
batch.Header.Number.Uint64(), // height
len(batch.TxHashes), // tx_count
header, // header blob
batchBodyID, // reference to the batch body
)

return nil
})
}

// GetHeadBatchHeader returns the header of the node's current head batch.
func GetHeadBatchHeader(db *sql.DB) (*common.BatchHeader, error) {
panic("implement me")
}

// GetBatchHeader returns the batch header given the hash.
func GetBatchHeader(db *sql.DB, hash gethcommon.Hash) (*common.BatchHeader, error) {
panic("implement me")
}

// GetBatchHash returns the hash of a batch given its number.
func GetBatchHash(db *sql.DB, number *big.Int) (*gethcommon.Hash, error) {
panic("implement me")
}

// GetBatchTxs returns the transaction hashes of the batch with the given hash.
func GetBatchTxs(db *sql.DB, batchHash gethcommon.Hash) ([]gethcommon.Hash, error) {
panic("implement me")
}

// GetBatchNumber returns the number of the batch containing the given transaction hash.
func GetBatchNumber(db *sql.DB, txHash gethcommon.Hash) (*big.Int, error) {
panic("implement me")
}

// GetTotalTransactions returns the total number of batched transactions.
func GetTotalTransactions(db *sql.DB) (*big.Int, error) {
panic("implement me")
}

// GetBatch returns the batch with the given hash.
func GetBatch(db *sql.DB, batchHash gethcommon.Hash) (*common.ExtBatch, error) {
panic("implement me")
}

// GetBatchBySequenceNumber returns the batch with the given sequence number.
func GetBatchBySequenceNumber(db *sql.DB, sequenceNumber *big.Int) (*common.ExtBatch, error) {
panic("implement me")
}

// GetBatchListing returns latest batches given a pagination.
// For example, page 0, size 10 will return the latest 10 batches.
func GetBatchListing(db *sql.DB, pagination *common.QueryPagination) (*common.BatchListingResponse, error) {
panic("implement me")
}

// Retrieves the batch header corresponding to the hash.
func readBatchHeader(db *sql.DB, hash gethcommon.Hash) (*common.BatchHeader, error) {
panic("implement me")
}

// Retrieves the hash of the head batch.
func readHeadBatchHash(db *sql.DB) (*gethcommon.Hash, error) {
panic("implement me")
}

// Stores a batch header into the database.
func writeBatchHeader(db *sql.DB, w ethdb.KeyValueWriter, header *common.BatchHeader) error {
panic("implement me")
}

// Stores the head batch header hash into the database.
func writeHeadBatchHash(db *sql.DB, w ethdb.KeyValueWriter, val gethcommon.Hash) error {
panic("implement me")
}

// Stores a batch's hash in the database, keyed by the batch's number.
func writeBatchHash(db *sql.DB, w ethdb.KeyValueWriter, header *common.BatchHeader) error {
panic("implement me")
}

// Stores a batch's hash in the database, keyed by the batch's sequencer number.
func writeBatchSeqNo(db *sql.DB, w ethdb.KeyValueWriter, header *common.BatchHeader) error {
panic("implement me")
}

// Retrieves the hash for the batch with the given number..
func readBatchHash(db *sql.DB, number *big.Int) (*gethcommon.Hash, error) {
panic("implement me")
}

// Returns the transaction hashes in the batch with the given hash.
func readBatchTxHashes(db *sql.DB, batchHash common.L2BatchHash) ([]gethcommon.Hash, error) {
panic("implement me")
}

// Stores a batch's number in the database, keyed by the hash of a transaction in that rollup.
func writeBatchNumber(db *sql.DB, w ethdb.KeyValueWriter, header *common.BatchHeader, txHash gethcommon.Hash) error {
panic("implement me")
}

// Writes the transaction hashes against the batch containing them.
func writeBatchTxHashes(db *sql.DB, w ethdb.KeyValueWriter, batchHash common.L2BatchHash, txHashes []gethcommon.Hash) error {
panic("implement me")
}

// Retrieves the number of the batch containing the transaction with the given hash.
func readBatchNumber(db *sql.DB, txHash gethcommon.Hash) (*big.Int, error) {
panic("implement me")
}

func readBatchHashBySequenceNumber(db *sql.DB, seqNum *big.Int) (*gethcommon.Hash, error) {
panic("implement me")
}

// Retrieves the total number of rolled-up transactions - returns 0 if no tx count is found
func readTotalTransactions(db *sql.DB) (*big.Int, error) {
panic("implement me")
}

// Stores the total number of transactions in the database.
func writeTotalTransactions(db *sql.DB, w ethdb.KeyValueWriter, newTotal *big.Int) error {
panic("implement me")
}

// Stores a batch into the database.
func writeBatch(db *sql.DB, w ethdb.KeyValueWriter, batch *common.ExtBatch) error {
panic("implement me")
}

// Retrieves the batch corresponding to the hash.
func readBatch(db *sql.DB, hash gethcommon.Hash) (*common.ExtBatch, error) {
panic("implement me")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package hostdb

import (
"errors"
Expand Down
Loading

0 comments on commit f5c7763

Please sign in to comment.