Skip to content

Commit

Permalink
decouple and refer to the respective modules in the snapshot create c…
Browse files Browse the repository at this point in the history
…ommand
  • Loading branch information
charithabandi committed Jan 24, 2025
1 parent 9597f9b commit 97d3375
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 54 deletions.
40 changes: 40 additions & 0 deletions app/node/namespace_manager.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package node

import (
"context"
"fmt"
"sync"

"github.com/kwilteam/kwil-db/core/utils/order"
"github.com/kwilteam/kwil-db/node/engine"
"github.com/kwilteam/kwil-db/node/types/sql"
)

func newNamespaceManager() *namespaceManager {
Expand Down Expand Up @@ -87,3 +90,40 @@ func (n *namespaceManager) ListPostgresSchemasToDump() []string {

return res
}

type DB interface {
sql.ReadTxMaker
}

func UserNamespaces(ctx context.Context, db DB) ([]string, error) {
tx, err := db.BeginReadTx(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)

res, err := tx.Execute(ctx, "select name from kwild_engine.namespaces where type='USER'")
if err != nil {
return nil, fmt.Errorf("failed to execute query: %w", err)
}

if len(res.Columns) != 1 {
return nil, fmt.Errorf("unexpected number of columns: %d", len(res.Columns))
}

var userNamespaces []string
for _, row := range res.Rows {
if len(row) != 1 {
return nil, fmt.Errorf("unexpected number of columns in row: %d", len(row))
}

ns, ok := row[0].(string)
if !ok {
return nil, fmt.Errorf("failed to convert namespace to string: %v", row[0])
}

userNamespaces = append(userNamespaces, ns)
}

return userNamespaces, nil
}
16 changes: 10 additions & 6 deletions app/setup/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,23 @@ func mergeGenesisFlags(conf *config.GenesisConfig, cmd *cobra.Command, flagCfg *
return nil, fmt.Errorf("invalid format for alloc, expected id#keyType:balance, received: %s", a)
}

// <id#keyType>, keyType is optional for 0x addresses
// 0x addresses: <address>:<balance>
// others: <pubkey#keyType>:<balance>
keyParts := strings.Split(parts[0], "#")
var keyType crypto.KeyType
var keyStr string
var err error
if len(keyParts) != 2 {
// check if the address is a 0x address
if !strings.HasPrefix(keyParts[0], "0x") {
return nil, fmt.Errorf("invalid address for alloc: %s", keyParts[0])

if strings.HasPrefix(parts[1], "0x") {
if len(keyParts) != 1 {
return nil, fmt.Errorf("invalid address for alloc: %s, expected format <address:balance>", parts[0])
}
keyStr = strings.TrimPrefix(keyParts[0], "0x")
keyStr = strings.TrimPrefix(parts[0], "0x")
keyType = crypto.KeyTypeSecp256k1
} else {
if len(keyParts) != 2 {
return nil, fmt.Errorf("invalid address for alloc: %s, expected format <key#keyType:balance>", parts[0])
}
keyType, err = crypto.ParseKeyType(keyParts[1])
if err != nil {
return nil, fmt.Errorf("invalid key type for validator: %s", keyParts[1])
Expand Down
56 changes: 13 additions & 43 deletions app/snapshot/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (

"github.com/spf13/cobra"

appNode "github.com/kwilteam/kwil-db/app/node"
"github.com/kwilteam/kwil-db/app/shared/bind"
"github.com/kwilteam/kwil-db/app/shared/display"
"github.com/kwilteam/kwil-db/config"
"github.com/kwilteam/kwil-db/core/crypto"
"github.com/kwilteam/kwil-db/core/types"
"github.com/kwilteam/kwil-db/node"
"github.com/kwilteam/kwil-db/node/meta"
"github.com/kwilteam/kwil-db/node/pg"
"github.com/kwilteam/kwil-db/node/voting"
)

var (
Expand Down Expand Up @@ -144,16 +145,6 @@ func PGDump(ctx context.Context, dbName, dbUser, dbPass, dbHost, dbPort string,
gzipWriter := gzip.NewWriter(outputFile)
defer gzipWriter.Close()

namespaces, err := namespaces(ctx, dbName, dbUser, dbPass, dbHost, dbPort)
if err != nil {
return -1, nil, "", nil, fmt.Errorf("failed to get namespaces: %w", err)
}

schemaArgs := make([]string, 0)
for _, ns := range namespaces { // user schemas
schemaArgs = append(schemaArgs, "--schema", ns)
}

args := []string{
"--dbname", dbName,
"--format", "plain",
Expand Down Expand Up @@ -189,7 +180,15 @@ func PGDump(ctx context.Context, dbName, dbUser, dbPass, dbHost, dbPort string,
"--port", dbPort,
"--no-password",
}
args = append(args, schemaArgs...)

namespaces, err := namespaces(ctx, dbName, dbUser, dbPass, dbHost, dbPort)
if err != nil {
return -1, nil, "", nil, fmt.Errorf("failed to get namespaces: %w", err)
}

for _, ns := range namespaces { // user schemas
args = append(args, "--schema", ns)
}

pgDumpCmd := exec.CommandContext(ctx, "pg_dump", args...)
pgDumpCmd.Env = append(os.Environ(), "PGPASSWORD="+dbPass)
Expand Down Expand Up @@ -253,7 +252,7 @@ func PGDump(ctx context.Context, dbName, dbUser, dbPass, dbHost, dbPort string,
}

// voterID is the encoded public key
keyType, pubkey, err := crypto.WireDecodeKeyType(voterID)
pubkey, keyType, err := voting.DecodePubKey(voterID)
if err != nil {
return -1, nil, "", nil, fmt.Errorf("failed to decode public key: %w", err)
}
Expand Down Expand Up @@ -406,34 +405,5 @@ func namespaces(ctx context.Context, dbName, dbUser, dbPass, dbHost, dbPort stri
}
defer pool.Close()

tx, err := pool.BeginReadTx(ctx)
if err != nil {
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback(ctx)

res, err := tx.Execute(ctx, "select name from kwild_engine.namespaces where type='USER'")
if err != nil {
return nil, fmt.Errorf("failed to execute query: %w", err)
}

if len(res.Columns) != 1 {
return nil, fmt.Errorf("unexpected number of columns: %d", len(res.Columns))
}

var userNamespaces []string
for _, row := range res.Rows {
if len(row) != 1 {
return nil, fmt.Errorf("unexpected number of columns in row: %d", len(row))
}

ns, ok := row[0].(string)
if !ok {
return nil, fmt.Errorf("failed to convert namespace to string: %v", row[0])
}

userNamespaces = append(userNamespaces, ns)
}

return userNamespaces, nil
return appNode.UserNamespaces(ctx, pool)
}
10 changes: 5 additions & 5 deletions node/voting/voting.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func fromRow(ctx context.Context, db sql.Executor, row []any) (*resolutions.Reso
return nil, fmt.Errorf("invalid type for voteBodyProposer (%T)", row[6])
}

pubKey, keyType, err := decodePubKey(vProposer)
pubKey, keyType, err := DecodePubKey(vProposer)
if err != nil {
return nil, fmt.Errorf("failed to decode pubKey from voteBodyProposer: %w", err)
}
Expand Down Expand Up @@ -346,7 +346,7 @@ func fromRow(ctx context.Context, db sql.Executor, row []any) (*resolutions.Reso
return nil, fmt.Errorf("failed to read bigendian int64 from voter: %w", err)
}

pubKey, keyType, err := decodePubKey(voter[8:])
pubKey, keyType, err := DecodePubKey(voter[8:])
if err != nil {
return nil, fmt.Errorf("failed to decode pubKey from voter: %w", err)
}
Expand Down Expand Up @@ -694,7 +694,7 @@ func getValidators(ctx context.Context, db sql.Executor) ([]*types.Validator, er
return nil, errors.New("invalid type for voter")
}

pubkey, keyType, err := decodePubKey(voterBts)
pubkey, keyType, err := DecodePubKey(voterBts)
if err != nil {
return nil, fmt.Errorf("failed to decode pubKey from voter: %w", err)
}
Expand Down Expand Up @@ -744,7 +744,7 @@ func getValidator(ctx context.Context, db sql.Executor, pubKey []byte, keyType c
return nil, errors.New("query returned a different voter")
}

pubkey, keyType, err := decodePubKey(voterBts)
pubkey, keyType, err := DecodePubKey(voterBts)
if err != nil {
return nil, fmt.Errorf("failed to decode pubKey from voter: %w", err)
}
Expand Down Expand Up @@ -804,7 +804,7 @@ func encodePubKey(pubKey []byte, keyType crypto.KeyType) []byte {
}

// decodePubKey decodes a byte slice into a public key and key type.
func decodePubKey(b []byte) ([]byte, crypto.KeyType, error) {
func DecodePubKey(b []byte) ([]byte, crypto.KeyType, error) {
keyType, b, err := crypto.WireDecodeKeyType(b)
if err != nil {
return nil, "", err
Expand Down

0 comments on commit 97d3375

Please sign in to comment.