Skip to content

Commit

Permalink
added privval to avoid using cometbfts type
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl committed Aug 18, 2023
1 parent 6561cee commit 2bad9f5
Show file tree
Hide file tree
Showing 24 changed files with 862 additions and 372 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ require (
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/chzyer/readline v1.5.0 // indirect
github.com/cometbft/cometbft v0.37.1
github.com/cometbft/cometbft v0.37.2
github.com/containerd/containerd v1.6.19 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k=
github.com/cometbft/cometbft v0.37.1 h1:KLxkQTK2hICXYq21U2hn1W5hOVYUdQgDQ1uB+90xPIg=
github.com/cometbft/cometbft v0.37.1/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/cometbft/cometbft v0.37.2 h1:XB0yyHGT0lwmJlFmM4+rsRnczPlHoAKFX6K8Zgc2/Jc=
github.com/cometbft/cometbft v0.37.2/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/cometbft/cometbft-db v0.7.0 h1:uBjbrBx4QzU0zOEnU8KxoDl18dMNgDh+zZRUE0ucsbo=
github.com/cometbft/cometbft-db v0.7.0/go.mod h1:yiKJIm2WKrt6x8Cyxtq9YTEcIMPcEe4XPxhgX59Fzf0=
github.com/compose-spec/compose-go v1.13.4 h1:O6xAsPqaY1s9KXteiO7wRCDTJLahv1XP/z/eUO9EfbI=
Expand Down
11 changes: 4 additions & 7 deletions internal/app/kwild/config/variables.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package config

import (
"crypto/ecdsa"
"fmt"
"os"
"strings"

"github.com/kwilteam/kwil-db/pkg/crypto"
"github.com/kwilteam/kwil-db/pkg/log"

"github.com/kwilteam/kwil-db/pkg/config"

cmtCrypto "github.com/cometbft/cometbft/crypto"
"github.com/cstockton/go-conv"
"github.com/ethereum/go-ethereum/crypto"
)

const (
Expand All @@ -22,13 +20,12 @@ const (
type KwildConfig struct {
GrpcListenAddress string
HttpListenAddress string
PrivateKey *ecdsa.PrivateKey
PrivateKey *crypto.Ed25519PrivateKey
SqliteFilePath string
Log log.Config
ExtensionEndpoints []string
ArweaveConfig ArweaveConfig
BcRpcUrl string
BCPrivateKey cmtCrypto.PrivKey
WithoutGasCosts bool
WithoutNonces bool
}
Expand Down Expand Up @@ -60,15 +57,15 @@ var (
Setter: func(val any) (any, error) {
if val == nil {
fmt.Println("no private key provided, generating a new one...")
return crypto.GenerateKey()
return crypto.GenerateEd25519Key()
}

strVal, err := conv.String(val)
if err != nil {
return nil, err
}

return crypto.HexToECDSA(strVal)
return crypto.Ed25519PrivateKeyFromHex(strVal)
},
}

Expand Down
30 changes: 29 additions & 1 deletion internal/app/kwild/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ import (
"github.com/kwilteam/kwil-db/internal/pkg/healthcheck"
simple_checker "github.com/kwilteam/kwil-db/internal/pkg/healthcheck/simple-checker"
"github.com/kwilteam/kwil-db/pkg/abci"
"github.com/kwilteam/kwil-db/pkg/abci/cometbft"
"github.com/kwilteam/kwil-db/pkg/balances"
"github.com/kwilteam/kwil-db/pkg/engine"
"github.com/kwilteam/kwil-db/pkg/grpc/gateway"
"github.com/kwilteam/kwil-db/pkg/grpc/gateway/middleware/cors"
grpc "github.com/kwilteam/kwil-db/pkg/grpc/server"
"github.com/kwilteam/kwil-db/pkg/kv/badger"
"github.com/kwilteam/kwil-db/pkg/log"
"github.com/kwilteam/kwil-db/pkg/modules/datasets"
"github.com/kwilteam/kwil-db/pkg/modules/validators"
"github.com/kwilteam/kwil-db/pkg/sql"
vmgr "github.com/kwilteam/kwil-db/pkg/validators"

// CometBFT
abciTypes "github.com/cometbft/cometbft/abci/types"
cmtcfg "github.com/cometbft/cometbft/config"
cmtflags "github.com/cometbft/cometbft/libs/cli/flags"
cmtlog "github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -261,6 +263,32 @@ func buildCometBftClient(cometBftNode *nm.Node) *cmtlocal.Local {
return cmtlocal.New(cometBftNode)
}

func buildCometNode(d *coreDependencies, abciApp abciTypes.Application) *cometbft.CometBftNode {
// TODO: a lot of the filepaths and logging here are hardcoded. This should be cleaned up
// with a config

// for now, I'm just using a KV store for my atomic commit. This probably is not ideal; a file may be better
// I'm simply using this because we know it fsyncs the data to disk
db, err := badger.NewBadgerDB("abci/signing", &badger.Options{
GuaranteeFSync: true,
})
if err != nil {
failBuild(err, "failed to build comet node")
}

readWriter := &atomicReadWriter{
kv: db,
key: []byte("az"), // any key here will work
}

node, err := cometbft.NewCometBftNode(abciApp, d.cfg.PrivateKey.Bytes(), readWriter, "abci/data", "debug")
if err != nil {
failBuild(err, "failed to build comet node")
}

return node
}

// TODO: clean this up --> @jchappelow
// it seems some of this should be handled in ABCI package if we do not provide it as a package
func newCometNode(app *abci.AbciApp, cfg *config.KwildConfig) (*nm.Node, error) {
Expand Down
25 changes: 25 additions & 0 deletions internal/app/kwild/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cmttypes "github.com/cometbft/cometbft/types"
"github.com/kwilteam/kwil-db/pkg/engine"
"github.com/kwilteam/kwil-db/pkg/extensions"
"github.com/kwilteam/kwil-db/pkg/kv"
"github.com/kwilteam/kwil-db/pkg/log"
"github.com/kwilteam/kwil-db/pkg/sql"
"github.com/kwilteam/kwil-db/pkg/sql/client"
Expand Down Expand Up @@ -106,3 +107,27 @@ func (wc *wrappedCometBFTClient) BroadcastTxAsync(ctx context.Context, tx *trans

return err
}

// atomicReadWriter implements the CometBFt AtomicReadWriter interface.
// This should probably be done with a file instead of a KV store,
// but we already have a good implementation of an atomic KV store.
type atomicReadWriter struct {
kv kv.KVStore
key []byte
}

func (a *atomicReadWriter) Read() ([]byte, error) {
res, err := a.kv.Get(a.key)
if err == kv.ErrKeyNotFound {
return nil, nil
}
if err != nil {
return nil, err
}

return res, nil
}

func (a *atomicReadWriter) Write(val []byte) error {
return a.kv.Set(a.key, val)
}
7 changes: 6 additions & 1 deletion internal/pkg/nodecfg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

cmtCfg "github.com/cometbft/cometbft/config"
cmtos "github.com/cometbft/cometbft/libs/os"
cmtrand "github.com/cometbft/cometbft/libs/rand"
"github.com/cometbft/cometbft/p2p"

cmtrand "github.com/cometbft/cometbft/libs/rand"
"github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/types"
cmttime "github.com/cometbft/cometbft/types/time"
Expand Down Expand Up @@ -42,6 +43,8 @@ type TestnetGenerateConfig struct {
P2pPort int
}

// TODO: if we use our own keys for cosmos, this will not work
// privval.LoadFilePV will need to be replacew with something else
func GenerateNodeConfig(genCfg *NodeGenerateConfig) error {
cfg := cmtCfg.DefaultConfig()
cfg.SetRoot(genCfg.HomeDir)
Expand Down Expand Up @@ -210,6 +213,8 @@ func GenerateTestnetConfig(genCfg *TestnetGenerateConfig) error {
return nil
}

// TODO: we definitely want to get rid of this, or at least make it more understandable / move it
// It generates private keys, which we should not leave up to Comet
func initFilesWithConfig(cfg *cmtCfg.Config) error {
// private validator
privValKeyFile := cfg.PrivValidatorKeyFile()
Expand Down
70 changes: 33 additions & 37 deletions pkg/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@ func (fe FatalError) String() string {
}

func newFatalError(method string, request fmt.Stringer, message string) FatalError {
if request == nil {
request = nilStringer{}
}

return FatalError{
AppMethod: method,
Request: request,
Message: message,
}
}

type appState struct { // TODO
prevBlockHeight int64
prevAppHash []byte
type nilStringer struct{}

func (ds nilStringer) String() string {
return "no message"
}

func NewAbciApp(database DatasetsModule, vldtrs ValidatorModule, kv KVStore, committer AtomicCommitter, opts ...AbciOpt) *AbciApp {
Expand All @@ -58,6 +63,9 @@ func NewAbciApp(database DatasetsModule, vldtrs ValidatorModule, kv KVStore, com
kv: kv,
},

valAddrToKey: make(map[string][]byte),
valUpdates: make([]*validators.Validator, 0),

log: log.NewNoOp(),

commitWaiter: sync.WaitGroup{},
Expand Down Expand Up @@ -111,6 +119,8 @@ type AbciApp struct {
applicationVersion uint64
}

var _ abciTypes.Application = &AbciApp{}

func (a *AbciApp) ApplySnapshotChunk(p0 abciTypes.RequestApplySnapshotChunk) abciTypes.ResponseApplySnapshotChunk {
return abciTypes.ResponseApplySnapshotChunk{}
}
Expand All @@ -126,8 +136,7 @@ func (a *AbciApp) BeginBlock(req abciTypes.RequestBeginBlock) abciTypes.Response

err := a.committer.Begin(context.Background())
if err != nil {
a.log.Error("failed to begin atomic commit", zap.Error(err))
return abciTypes.ResponseBeginBlock{}
panic(newFatalError("BeginBlock", &req, err.Error()))
}

// Punish bad validators.
Expand All @@ -142,12 +151,12 @@ func (a *AbciApp) BeginBlock(req abciTypes.RequestBeginBlock) abciTypes.Response
// This is why we need the addr=>pubkey map. Why, comet, why?
pubkey, ok := a.valAddrToKey[addr]
if !ok {
panic(fmt.Sprintf("unknown validator address %v", addr))
panic(newFatalError("BeginBlock", &req, fmt.Sprintf("unknown validator address %v", addr)))
}
const punishDelta = 1
newPower := ev.Validator.Power - punishDelta
if err = a.validators.Punish(context.Background(), pubkey, newPower); err != nil {
panic(fmt.Sprintf("failed to punish validator %v: %v", addr, err))
panic(newFatalError("BeginBlock", &req, fmt.Sprintf("failed to punish validator %v", addr)))
}
}

Expand All @@ -171,13 +180,6 @@ func (a *AbciApp) CheckTx(incoming abciTypes.RequestCheckTx) abciTypes.ResponseC
return abciTypes.ResponseCheckTx{Code: 0}
}

// pubkeys in event attributes returned to comet as strings are base64 encoded,
// apparently.
// TODO: move this somewhere else in the file
func encodeBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}

func (a *AbciApp) DeliverTx(req abciTypes.RequestDeliverTx) abciTypes.ResponseDeliverTx {
ctx := context.Background()

Expand Down Expand Up @@ -354,32 +356,28 @@ func (a *AbciApp) Commit() abciTypes.ResponseCommit {
// generate the unique id for all changes occurred thus far
id, err := a.committer.ID(ctx)
if err != nil {
a.log.Error("failed to get committer id", zap.Error(err))
return abciTypes.ResponseCommit{}
panic(newFatalError("Commit", nil, fmt.Sprintf("failed to get commit id: %v", err)))
}

appHash, err := a.createNewAppHash(ctx, id)
if err != nil {
a.log.Error("failed to create new app hash", zap.Error(err))
return abciTypes.ResponseCommit{}
panic(newFatalError("Commit", nil, fmt.Sprintf("failed to create new app hash: %v", err)))
}

err = a.metadataStore.IncrementBlockHeight(ctx)
if err != nil {
a.log.Error("failed to increment block height", zap.Error(err))
return abciTypes.ResponseCommit{}
panic(newFatalError("Commit", nil, fmt.Sprintf("failed to increment block height: %v", err)))
}

err = a.committer.Commit(ctx, func(err error) {
if err != nil {
a.log.Error("failed to apply atomic commit", zap.Error(err))
panic(newFatalError("Commit", nil, fmt.Sprintf("failed to commit atomic commit: %v", err)))
}

a.commitWaiter.Done()
})
if err != nil {
a.log.Error("failed to commit atomic commit", zap.Error(err))
return abciTypes.ResponseCommit{}
panic(newFatalError("Commit", nil, fmt.Sprintf("failed to commit atomic commit: %v", err)))
}

// Update the validator address=>pubkey map used by Penalize.
Expand Down Expand Up @@ -420,19 +418,12 @@ func (a *AbciApp) Info(p0 abciTypes.RequestInfo) abciTypes.ResponseInfo {

height, err := a.metadataStore.GetBlockHeight(ctx)
if err != nil {
a.log.Error("failed to get block height", zap.Error(err))
return abciTypes.ResponseInfo{
AppVersion: a.applicationVersion,
}
panic(newFatalError("Info", &p0, fmt.Sprintf("failed to get block height: %v", err)))
}

appHash, err := a.metadataStore.GetAppHash(ctx)
if err != nil {
a.log.Error("failed to get app hash", zap.Error(err))
return abciTypes.ResponseInfo{
LastBlockHeight: height,
AppVersion: a.applicationVersion,
}
panic(newFatalError("Info", &p0, fmt.Sprintf("failed to get app hash: %v", err)))
}

return abciTypes.ResponseInfo{
Expand Down Expand Up @@ -472,11 +463,10 @@ func (a *AbciApp) InitChain(p0 abciTypes.RequestInitChain) abciTypes.ResponseIni

apphash, err := a.metadataStore.GetAppHash(ctx)
if err != nil {
a.log.Error("failed to get app hash", zap.Error(err))

panic(fmt.Sprintf("failed to get app hash: %v", err))
// TODO: should we initialize with a genesis hash instead if it fails
// TODO: apparently InitChain is only genesis, so yes it should only be genesis hash
apphash = []byte{}
// in fact, I don't think we should be getting it from this store at all
}

return abciTypes.ResponseInitChain{
Expand Down Expand Up @@ -536,8 +526,8 @@ func convertArgs(args [][]string) [][]any {
}

var (
appHashKey = []byte("appHash")
blockHeightKey = []byte("blockHeight")
appHashKey = []byte("a")
blockHeightKey = []byte("b")
)

type metadataStore struct {
Expand Down Expand Up @@ -576,3 +566,9 @@ func (m *metadataStore) IncrementBlockHeight(ctx context.Context) error {

return m.SetBlockHeight(ctx, height+1)
}

// pubkeys in event attributes returned to comet as strings are base64 encoded,
// apparently.
func encodeBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
6 changes: 0 additions & 6 deletions pkg/abci/cometbft/comet_test.go

This file was deleted.

Loading

0 comments on commit 2bad9f5

Please sign in to comment.