Skip to content

Commit

Permalink
cleanup main and related files
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed Nov 25, 2024
1 parent c726c34 commit 7bc68e2
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 91 deletions.
5 changes: 5 additions & 0 deletions sequencer/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
version: '3'

tasks:
run-seq:
dotenv: [".env"]
cmds:
- go run main.go run --cfg cfg.toml

test-historydb:
dotenv: [".env"]
cmds:
Expand Down
1 change: 1 addition & 0 deletions sequencer/cfg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ NameWrite = "postgres"
[Web3]
## Url of the web3 ethereum-node RPC server. Only geth is officially supported
URL = "http://localhost:8545"
## URL = "https://ethereum-sepolia-rpc.publicnode.com"

[Synchronizer]
### Interval between attempts to synchronize a new block from an ethereum node
Expand Down
10 changes: 5 additions & 5 deletions sequencer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ type LogConf struct {
}

// LoadNode loads the Node configuration from path.
func LoadNode(path string, coordinator bool) (*Node, error) {
func LoadNode(path string /*, coordinator bool*/) (*Node, error) {
var cfg, aux Node
err := SourceParamsNode(path, &cfg, &aux)
if err != nil {
Expand All @@ -421,10 +421,10 @@ func LoadNode(path string, coordinator bool) (*Node, error) {
if err := validate.Struct(cfg); err != nil {
return nil, common.Wrap(fmt.Errorf("error validating configuration file: %w", err))
}
if coordinator {
if err := validate.Struct(cfg.Coordinator); err != nil {
return nil, common.Wrap(fmt.Errorf("error validating configuration file: %w", err))
}
// if coordinator {
if err := validate.Struct(cfg.Coordinator); err != nil {
return nil, common.Wrap(fmt.Errorf("error validating configuration file: %w", err))
// }
}
log.Printf("Loaded Configuration: %+v", cfg)
return &cfg, nil
Expand Down
4 changes: 2 additions & 2 deletions sequencer/config/internalNodeConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package config

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"

Expand All @@ -19,7 +19,7 @@ func loadDefault(defaultValues string, cfg interface{}) error {
}

func loadFile(path string, cfg interface{}) error {
bs, err := ioutil.ReadFile(filepath.Clean(path))
bs, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions sequencer/database/historydb/historydb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

var historyDB *HistoryDB
var historyDBWithACC *HistoryDB

// var historyDBWithACC *HistoryDB

// Block0 represents Ethereum's genesis block,
// which is stored by default at HistoryDB
Expand Down Expand Up @@ -47,9 +48,9 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
historyDB = NewHistoryDB(db, db, nil)
apiConnCon := database.NewAPIConnectionController(1, time.Second)
historyDBWithACC = NewHistoryDB(db, db, apiConnCon)
historyDB = NewHistoryDB(db, db /*, nil*/)
// apiConnCon := database.NewAPIConnectionController(1, time.Second)
// historyDBWithACC = NewHistoryDB(db, db, apiConnCon)

test.MigrationsDownTest(historyDB.DB())

Expand Down
4 changes: 2 additions & 2 deletions sequencer/database/l2db/l2db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/require"
)

var decimals = uint64(3)
// var decimals = uint64(3)
var l2DB *L2DB
var l2DBWithACC *L2DB
var historyDB *historydb.HistoryDB
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestMain(m *testing.M) {
apiConnCon := database.NewAPIConnectionController(1, time.Second)
l2DBWithACC = NewL2DB(db, db, 10, 1000, 0.0, 1000.0, 24*time.Hour, apiConnCon)
WipeDB(l2DB.DB())
historyDB = historydb.NewHistoryDB(db, db, nil)
historyDB = historydb.NewHistoryDB(db, db /*, nil*/)
// Run tests
result := m.Run()
// Close DB
Expand Down
68 changes: 34 additions & 34 deletions sequencer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
)

const (
flagCfg = "cfg"
flagMode = "mode"
flagSK = "privatekey"
flagYes = "yes"
flagBlock = "block"
modeSync = "sync"
modeCoord = "coord"
nMigrations = "nMigrations"
flagAccount = "account"
flagPath = "path"
flagCfg = "cfg"
// flagMode = "mode"
// flagSK = "privatekey"
// flagYes = "yes"
// flagBlock = "block"
// modeSync = "sync"
// modeCoord = "coord"
// nMigrations = "nMigrations"
// flagAccount = "account"
// flagPath = "path"
)

// Config is the configuration of the node execution
type Config struct {
mode node.Mode
// mode node.Mode
node *config.Node
}

Expand All @@ -45,25 +45,25 @@ func parseCli(c *cli.Context) (*Config, error) {

func getConfig(c *cli.Context) (*Config, error) {
var cfg Config
mode := c.String(flagMode)
// mode := c.String(flagMode)
nodeCfgPath := c.String(flagCfg)
var err error
switch mode {
case modeSync:
cfg.mode = node.ModeSynchronizer
cfg.node, err = config.LoadNode(nodeCfgPath, false)
if err != nil {
return nil, common.Wrap(err)
}
case modeCoord:
cfg.mode = node.ModeCoordinator
cfg.node, err = config.LoadNode(nodeCfgPath, true)
if err != nil {
return nil, common.Wrap(err)
}
default:
return nil, common.Wrap(fmt.Errorf("invalid mode \"%v\"", mode))
// switch mode {
// case modeSync:
// // cfg.mode = node.ModeSynchronizer
// cfg.node, err = config.LoadNode(nodeCfgPath, false)
// if err != nil {
// return nil, common.Wrap(err)
// }
// case modeCoord:
// cfg.mode = node.ModeCoordinator
cfg.node, err = config.LoadNode(nodeCfgPath /*, true*/)
if err != nil {
return nil, common.Wrap(err)
}
// default:
// return nil, common.Wrap(fmt.Errorf("invalid mode \"%v\"", mode))
// }

return &cfg, nil
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func cmdRun(c *cli.Context) error {
}
// TODO: Initialize lof library
// log.Init(cfg.node.Log.Level, cfg.node.Log.Out)
innerNode, err := node.NewNode(cfg.mode, cfg.node, c.App.Version)
innerNode, err := node.NewNode( /*cfg.mode, */ cfg.node, c.App.Version)
if err != nil {
return common.Wrap(fmt.Errorf("error starting node: %w", err))
}
Expand All @@ -115,11 +115,11 @@ func main() {
app.Version = "v1"

flags := []cli.Flag{
&cli.StringFlag{
Name: flagMode,
Usage: fmt.Sprintf("Set node `MODE` (can be \"%v\" or \"%v\")", modeSync, modeCoord),
Required: true,
},
// &cli.StringFlag{
// Name: flagMode,
// Usage: fmt.Sprintf("Set node `MODE` (can be \"%v\" or \"%v\")", modeSync, modeCoord),
// Required: true,
// },
&cli.StringFlag{
Name: flagCfg,
Usage: "Node configuration `FILE`",
Expand All @@ -131,7 +131,7 @@ func main() {
{
Name: "run",
Aliases: []string{},
Usage: "Run the tokamak-node in the indicated mode",
Usage: "Run the tokamak-node", // in the indicated mode",
Action: cmdRun,
Flags: flags,
},
Expand Down
76 changes: 38 additions & 38 deletions sequencer/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package node

import (
"context"
"encoding/hex"
"fmt"
"sync"
"tokamak-sybil-resistance/batchbuilder"
Expand All @@ -33,7 +32,6 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/jmoiron/sqlx"
"github.com/russross/meddler"
)
Expand Down Expand Up @@ -334,27 +332,27 @@ func NewNode( /*mode Mode, */ cfg *config.Node, version string) (*Node, error) {
// if mode == ModeCoordinator {
// Unlock FeeAccount EthAddr in the keystore to generate the
// account creation authorization
if !keyStore.HasAddress(cfg.Coordinator.FeeAccount.Address) {
return nil, common.Wrap(fmt.Errorf(
"ethereum keystore doesn't have the key for address %v",
cfg.Coordinator.FeeAccount.Address))
}
feeAccount := accounts.Account{
Address: cfg.Coordinator.FeeAccount.Address,
}
if err := keyStore.Unlock(feeAccount,
cfg.Coordinator.EthClient.Keystore.Password); err != nil {
return nil, common.Wrap(err)
}
//Swap bjj endianness
decodedBjjPubKey, err := hex.DecodeString(cfg.Coordinator.FeeAccount.BJJ.String())
if err != nil {
log.Error("Error decoding BJJ public key from config file. Error: ", err.Error())
return nil, common.Wrap(err)
}
bSwapped := common.SwapEndianness(decodedBjjPubKey)
var bjj babyjub.PublicKeyComp
copy(bjj[:], bSwapped[:])
// if !keyStore.HasAddress(cfg.Coordinator.FeeAccount.Address) {
// return nil, common.Wrap(fmt.Errorf(
// "ethereum keystore doesn't have the key for address %v",
// cfg.Coordinator.FeeAccount.Address))
// }
// feeAccount := accounts.Account{
// Address: cfg.Coordinator.FeeAccount.Address,
// }
// if err := keyStore.Unlock(feeAccount,
// cfg.Coordinator.EthClient.Keystore.Password); err != nil {
// return nil, common.Wrap(err)
// }
// //Swap bjj endianness
// decodedBjjPubKey, err := hex.DecodeString(cfg.Coordinator.FeeAccount.BJJ.String())
// if err != nil {
// log.Error("Error decoding BJJ public key from config file. Error: ", err.Error())
// return nil, common.Wrap(err)
// }
// bSwapped := common.SwapEndianness(decodedBjjPubKey)
// var bjj babyjub.PublicKeyComp
// copy(bjj[:], bSwapped[:])

// auth := &common.AccountCreationAuth{
// EthAddr: cfg.Coordinator.FeeAccount.Address,
Expand Down Expand Up @@ -628,18 +626,20 @@ func (n *Node) Start() {
}

// Stop the node
// func (n *Node) Stop() {
// log.Infow("Stopping node...")
// n.cancel()
// n.wg.Wait()
// if n.mode == ModeCoordinator {
// log.Info("Stopping Coordinator...")
// n.coord.Stop()
// }
// // Close kv DBs
// n.sync.StateDB().Close()
// if n.mode == ModeCoordinator {
// n.coord.TxSelector().LocalAccountsDB().Close()
// n.coord.BatchBuilder().LocalStateDB().Close()
// }
// }
func (n *Node) Stop() {
log.Infow("Stopping node...")
n.cancel()
n.wg.Wait()
// if n.mode == ModeCoordinator {
// log.Info("Stopping Coordinator...")
// n.coord.Stop()
// }
//
// // Close kv DBs
// n.sync.StateDB().Close()
//
// if n.mode == ModeCoordinator {
// n.coord.TxSelector().LocalAccountsDB().Close()
// n.coord.BatchBuilder().LocalStateDB().Close()
// }
}
18 changes: 12 additions & 6 deletions sequencer/synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func newTestModules(t *testing.T) (*statedb.StateDB, *historydb.HistoryDB, *l2db
// Init History DB
db, err := dbUtils.InitTestSQLDB()
require.NoError(t, err)
historyDB := historydb.NewHistoryDB(db, db, nil)
historyDB := historydb.NewHistoryDB(db, db /*, nil*/)

// Init L2 DB
l2DB := l2db.NewL2DB(db, db, 10, 100, 0.0, 1000.0, 24*time.Hour, nil)
Expand All @@ -285,7 +285,7 @@ func newBigInt(s string) *big.Int {
}

func TestSyncGeneral(t *testing.T) {
stateDB, historyDB, l2DB := newTestModules(t)
stateDB, historyDB, _ := newTestModules(t)

// Init eth client
var timer timer
Expand All @@ -295,10 +295,16 @@ func TestSyncGeneral(t *testing.T) {
client := test.NewClient(true, &timer, &ethCommon.Address{}, clientSetup)

// Create Synchronizer
s, err := NewSynchronizer(client, historyDB, l2DB, stateDB, Config{
StatsUpdateBlockNumDiffThreshold: 100,
StatsUpdateFrequencyDivider: 100,
})
s, err := NewSynchronizer(
client,
historyDB,
// l2DB,
stateDB,
Config{
StatsUpdateBlockNumDiffThreshold: 100,
StatsUpdateFrequencyDivider: 100,
},
)
require.NoError(t, err)

ctx := context.Background()
Expand Down

0 comments on commit 7bc68e2

Please sign in to comment.