Skip to content

Commit

Permalink
AppHash bug fixes, Account update fix, initialize modules correctly a…
Browse files Browse the repository at this point in the history
…nd update

the node role based on the validator updates
  • Loading branch information
charithabandi committed Dec 10, 2024
1 parent 88d912c commit bc87c5c
Show file tree
Hide file tree
Showing 38 changed files with 776 additions and 563 deletions.
7 changes: 4 additions & 3 deletions app/node/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func buildServer(ctx context.Context, d *coreDependencies) *server {
mp := mempool.New()

// accounts
accounts := buildAccountStore(ctx, db)
accounts := buildAccountStore(ctx, d, db)

// eventstore, votestore
_, vs := buildVoteStore(ctx, d, closers) // ev, vs
Expand Down Expand Up @@ -165,8 +165,9 @@ func buildBlockStore(d *coreDependencies, closers *closeFuncs) *store.BlockStore
return bs
}

func buildAccountStore(ctx context.Context, db *pg.DB) *accounts.Accounts {
accounts, err := accounts.InitializeAccountStore(ctx, db)
func buildAccountStore(ctx context.Context, d *coreDependencies, db *pg.DB) *accounts.Accounts {
logger := d.logger.New("ACCOUNTS")
accounts, err := accounts.InitializeAccountStore(ctx, db, logger)
if err != nil {
failBuild(err, "failed to initialize account store")
}
Expand Down
134 changes: 126 additions & 8 deletions app/setup/reset.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package setup

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/kwilteam/kwil-db/app/shared/bind"
"github.com/kwilteam/kwil-db/node"
"github.com/kwilteam/kwil-db/node/pg"

"github.com/spf13/cobra"
)
Expand All @@ -31,24 +33,140 @@ func ResetCmd() *cobra.Command {
return fmt.Errorf("root directory %s does not exist", rootDir)
}

// TODO: reset app DB

if !all {
return nil
pgConf, err := getPostgresFlags(cmd)
if err != nil {
return err
}

// remove the blockstore if all is set
chainDir := filepath.Join(rootDir, "blockstore")
if err := os.RemoveAll(chainDir); err != nil {
err = resetPGState(cmd.Context(), pgConf)
if err != nil {
return err
}
fmt.Println("blockstore removed")
fmt.Printf("Postgres state reset. Host: %s; Port: %s; Database: %s\n", pgConf.Host, pgConf.Port, pgConf.DBName)

if all {
// remove the blockstore if all is set
chainDir := filepath.Join(rootDir, "blockstore")
if err := os.RemoveAll(chainDir); err != nil {
return err
}
fmt.Printf("Blockstore directory removed: %s\n", chainDir)

// remove rcvd_snaps if exists
snapDir := filepath.Join(rootDir, "rcvd_snaps")
if err := os.RemoveAll(snapDir); err != nil {
return err
}
fmt.Printf("Statesync snapshots directory removed: %s\n", snapDir)

// remove snapshots if exists
snapDir = filepath.Join(rootDir, "snapshots")
if err := os.RemoveAll(snapDir); err != nil {
return err
}
fmt.Println("Snapshots directory removed", snapDir)
}

return nil
},
}

cmd.Flags().BoolVar(&all, "all", false, "reset all data, if this is not set, only the app state will be reset")
bindPostgresFlags(cmd)

return cmd
}

// resetPGState drops and creates the database.
func resetPGState(ctx context.Context, conf *pg.ConnConfig) error {
dropDB := conf.DBName
conf.DBName = "postgres"
defer func() { conf.DBName = dropDB }()

conn, err := pg.NewPool(ctx, &pg.PoolConfig{
ConnConfig: *conf,
MaxConns: 2, // requires 2 connections
})
if err != nil {
return err
}
defer conn.Close()

_, err = conn.Execute(ctx, "DROP DATABASE "+dropDB)
if err != nil {
return err
}

_, err = conn.Execute(ctx, "CREATE DATABASE "+dropDB+" OWNER kwild")
if err != nil {
return err
}

return nil
}

// bindPostgresFlags binds flags to connect to a postgres database.
func bindPostgresFlags(cmd *cobra.Command) {
cmd.Flags().String("dbname", "kwild", "Name of the database in the PostgreSQL server")
cmd.Flags().String("user", "postgres", "User with administrative privileges on the database")
cmd.Flags().String("password", "", "Password for the database user")
cmd.Flags().String("host", "localhost", "Host of the database")
cmd.Flags().String("port", "5432", "Port of the database")
}

// getPostgresFlags returns the postgres flags from the given command.
func getPostgresFlags(cmd *cobra.Command) (*pg.ConnConfig, error) {
return mergePostgresFlags(defaultPostgresConnConfig(), cmd)
}

// mergePostgresFlags merges the given connection config with the flags from the given command.
// It only sets the fields that are set in the flags.
func mergePostgresFlags(conf *pg.ConnConfig, cmd *cobra.Command) (*pg.ConnConfig, error) {
var err error
if cmd.Flags().Changed("dbname") {
conf.DBName, err = cmd.Flags().GetString("dbname")
if err != nil {
return nil, err
}
}

if cmd.Flags().Changed("user") {
conf.User, err = cmd.Flags().GetString("user")
if err != nil {
return nil, err
}
}

if cmd.Flags().Changed("password") {
conf.Pass, err = cmd.Flags().GetString("password")
if err != nil {
return nil, err
}
}

if cmd.Flags().Changed("host") {
conf.Host, err = cmd.Flags().GetString("host")
if err != nil {
return nil, err
}
}

if cmd.Flags().Changed("port") {
conf.Port, err = cmd.Flags().GetString("port")
if err != nil {
return nil, err
}
}

return conf, nil
}

// DefaultPostgresConnConfig returns a default connection config for a postgres database.
func defaultPostgresConnConfig() *pg.ConnConfig {
return &pg.ConnConfig{
DBName: "kwild",
User: "postgres",
Host: "localhost",
Port: "5432",
}
}
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ type GenesisConfig struct {
// MaxVotesPerTx is the maximum number of votes that can be included in a
// single transaction.
MaxVotesPerTx int64 `json:"max_votes_per_tx"`

// TODO: AppHash is the hash of the genesis state.
// StateHash is the hash of the initial state of the chain, used when bootstrapping
// the chain with a network snapshot.
StateHash []byte `json:"state_hash"`
}

func (nc *GenesisConfig) SaveAs(filename string) error {
Expand Down
Loading

0 comments on commit bc87c5c

Please sign in to comment.