Skip to content

Commit

Permalink
rename extensions interfaces and types
Browse files Browse the repository at this point in the history
This changes sever extension interfaces to be more intuitive / accurate to what they are
doing. It also wraps the comments describing the interfaces, to match the formatting
with what is in the docs.

It does not change any logic.
  • Loading branch information
brennanjl authored Mar 11, 2024
1 parent e0f33c1 commit 8323dd3
Show file tree
Hide file tree
Showing 42 changed files with 656 additions and 589 deletions.
9 changes: 1 addition & 8 deletions cmd/kwil-admin/nodecfg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ var (
genesisValidatorGas, _ = big.NewInt(0).SetString("10000000000000000000000", 10)
)

type EthDepositOracle struct {
Enabled bool
Endpoint string
EscrowAddress string
RequiredConfirmations string
}

type NodeGenerateConfig struct {
ChainID string
BlockInterval time.Duration
Expand Down Expand Up @@ -350,7 +343,7 @@ func GenerateTestnetConfig(genCfg *TestnetGenerateConfig, opts *ConfigOpts) erro

cfg.AppCfg.PrivateKeyPath = config.PrivateKeyFileName // not abs/rooted because this might be run in a container

// oracle config
// extension config
if i < genCfg.NValidators {
if genCfg.Extensions != nil {
if len(genCfg.Extensions) != genCfg.NValidators {
Expand Down
27 changes: 14 additions & 13 deletions cmd/kwild/server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/kwilteam/kwil-db/internal/events"
"github.com/kwilteam/kwil-db/internal/events/broadcast"
"github.com/kwilteam/kwil-db/internal/kv/badger"
"github.com/kwilteam/kwil-db/internal/oracles"
"github.com/kwilteam/kwil-db/internal/listeners"
admSvc "github.com/kwilteam/kwil-db/internal/services/grpc/admin/v0"
functionSvc "github.com/kwilteam/kwil-db/internal/services/grpc/function/v0"
"github.com/kwilteam/kwil-db/internal/services/grpc/healthsvc/v0"
Expand Down Expand Up @@ -104,8 +104,9 @@ func buildServer(d *coreDependencies, closers *closeFuncs) *Server {

eventBroadcaster := buildEventBroadcaster(d, ev, wrappedCmtClient, txApp)
abciApp.SetEventBroadcaster(eventBroadcaster.RunBroadcast)
// oracle manager
om := buildOracleManager(d, ev, cometBftNode, txApp)

// listener manager
listeners := buildListenerManager(d, ev, cometBftNode, txApp)

// tx service and grpc server
txsvc := buildTxSvc(d, db, e, wrappedCmtClient, txApp)
Expand All @@ -116,14 +117,14 @@ func buildServer(d *coreDependencies, closers *closeFuncs) *Server {
adminTCPServer := buildAdminService(d, closers, admsvc, txsvc)

return &Server{
grpcServer: grpcServer,
adminTPCServer: adminTCPServer,
gateway: buildGatewayServer(d),
cometBftNode: cometBftNode,
oracleMgr: om,
log: *d.log.Named("server"),
closers: closers,
cfg: d.cfg,
grpcServer: grpcServer,
adminTPCServer: adminTCPServer,
gateway: buildGatewayServer(d),
cometBftNode: cometBftNode,
listenerManager: listeners,
log: *d.log.Named("server"),
closers: closers,
cfg: d.cfg,
}
}

Expand Down Expand Up @@ -656,6 +657,6 @@ func failBuild(err error, msg string) {
}
}

func buildOracleManager(d *coreDependencies, ev *events.EventStore, node *cometbft.CometBftNode, txapp *txapp.TxApp) *oracles.OracleMgr {
return oracles.NewOracleMgr(d.cfg.AppCfg.Extensions, ev, node, d.privKey.PubKey().Bytes(), txapp, *d.log.Named("oracle-manager"))
func buildListenerManager(d *coreDependencies, ev *events.EventStore, node *cometbft.CometBftNode, txapp *txapp.TxApp) *listeners.ListenerManager {
return listeners.NewListenerManager(d.cfg.AppCfg.Extensions, ev, node, d.privKey.PubKey().Bytes(), txapp, *d.log.Named("listener-manager"))
}
26 changes: 13 additions & 13 deletions cmd/kwild/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/kwilteam/kwil-db/core/crypto"
"github.com/kwilteam/kwil-db/core/log"
"github.com/kwilteam/kwil-db/internal/abci/cometbft"
"github.com/kwilteam/kwil-db/internal/oracles"
"github.com/kwilteam/kwil-db/internal/listeners"
gateway "github.com/kwilteam/kwil-db/internal/services/grpc_gateway"
grpc "github.com/kwilteam/kwil-db/internal/services/grpc_server"
"github.com/kwilteam/kwil-db/internal/sql/pg"
Expand All @@ -29,13 +29,13 @@ import (

// Server controls the gRPC server and http gateway.
type Server struct {
grpcServer *grpc.Server
gateway *gateway.GatewayServer
adminTPCServer *grpc.Server
cometBftNode *cometbft.CometBftNode
oracleMgr *oracles.OracleMgr
closers *closeFuncs
log log.Logger
grpcServer *grpc.Server
gateway *gateway.GatewayServer
adminTPCServer *grpc.Server
cometBftNode *cometbft.CometBftNode
listenerManager *listeners.ListenerManager
closers *closeFuncs
log log.Logger

cfg *config.KwildConfig

Expand Down Expand Up @@ -165,16 +165,16 @@ func (s *Server) Start(ctx context.Context) error {
})
s.log.Info("grpc server started", zap.String("address", s.cfg.AppCfg.AdminListenAddress))

// Start oracle manager only after node caught up
// Start listener manager only after node caught up
group.Go(func() error {
go func() {
<-groupCtx.Done()
s.log.Info("stop oracle manager")
s.oracleMgr.Stop()
s.log.Info("stop listeners")
s.listenerManager.Stop()
}()
return s.oracleMgr.Start()
return s.listenerManager.Start()
})
s.log.Info("oracle manager started")
s.log.Info("listener manager started")

group.Go(func() error {
// The CometBFT services do not block on Start().
Expand Down
8 changes: 4 additions & 4 deletions cmd/kwild/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

types "github.com/kwilteam/kwil-db/core/types/admin"
"github.com/kwilteam/kwil-db/extensions/actions"
"github.com/kwilteam/kwil-db/extensions/precompiles"
"github.com/kwilteam/kwil-db/internal/abci"
"github.com/kwilteam/kwil-db/internal/abci/cometbft/privval"
"github.com/kwilteam/kwil-db/internal/extensions"
Expand All @@ -24,10 +24,10 @@ import (

// getExtensions returns both the local and remote extensions. Remote extensions are identified by
// connecting to the specified extension URLs.
func getExtensions(ctx context.Context, urls []string) (map[string]actions.ExtensionInitializer, error) {
exts := make(map[string]actions.ExtensionInitializer)
func getExtensions(ctx context.Context, urls []string) (map[string]precompiles.Initializer, error) {
exts := make(map[string]precompiles.Initializer)

for name, ext := range actions.RegisteredExtensions() {
for name, ext := range precompiles.RegisteredPrecompiles() {
_, ok := exts[name]
if ok {
return nil, fmt.Errorf("duplicate extension name: %s", name)
Expand Down
41 changes: 25 additions & 16 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ import (
"github.com/kwilteam/kwil-db/core/types"
)

// Service provides access to general application information to extensions.
// Service provides access to general application information to
// extensions.
type Service struct {
// Logger is a logger for the application
Logger log.SugaredLogger
// ExtensionConfigs is a map of the nodes extensions and local configurations. It maps:
// extension_name -> config_key -> config_value
// ExtensionConfigs is a map of the nodes extensions and local
// configurations.
// It maps: extension_name -> config_key -> config_value
ExtensionConfigs map[string]map[string]string
}

// App is an application that can modify and query the local database instance.
// App is an application that can modify and query the local database
// instance.
type App struct {
// Service is the base application
Service *Service
// DB is a connection to the underlying Postgres database
DB sql.DB
// Engine is the underlying KwilDB engine, capable of storing and executing against
// Engine is the underlying KwilDB engine, capable of storing and
// executing against
// Kuneiform schemas
Engine Engine
}
Expand All @@ -35,10 +39,11 @@ type Engine interface {
// DeleteDataset deletes a dataset.
// The caller must be the owner of the dataset.
DeleteDataset(ctx context.Context, tx sql.DB, dbid string, caller []byte) error
// Execute executes a procedure in a dataset.
// It can be given either a readwrite or readonly database transaction.
// If it is given a read-only transaction, it will not be able to execute any procedures that are not `view`.
Call(ctx context.Context, tx sql.DB, options *ExecutionData) (*sql.ResultSet, error)
// Procedure executes a procedure in a dataset. It can be given
// either a readwrite or readonly database transaction. If it is
// given a read-only transaction, it will not be able to execute
// any procedures that are not `view`.
Procedure(ctx context.Context, tx sql.DB, options *ExecutionData) (*sql.ResultSet, error)
// GetSchema returns the schema of a dataset.
// It will return an error if the dataset does not exist.
GetSchema(ctx context.Context, dbid string) (*Schema, error)
Expand All @@ -49,22 +54,26 @@ type Engine interface {
Execute(ctx context.Context, tx sql.DB, dbid, query string, values map[string]any) (*sql.ResultSet, error)
}

// ExecutionOptions is contextual data that is passed to a procedure during call / execution.
// It is scoped to the lifetime of a single execution.
// ExecutionOptions is contextual data that is passed to a procedure
// during call / execution. It is scoped to the lifetime of a single
// execution.
type ExecutionData struct {
// Dataset is the DBID of the dataset that was called.
// Even if a procedure in another dataset is called, this will always be the original dataset.
// Even if a procedure in another dataset is called, this will
// always be the original dataset.
Dataset string

// Procedure is the original procedure that was called.
// Even if a nested procedure is called, this will always be the original procedure.
// Even if a nested procedure is called, this will always be the
// original procedure.
Procedure string

// Args are the arguments that were passed to the procedure. Currently these
// are all string or untyped nil values.
// Args are the arguments that were passed to the procedure.
// Currently these are all string or untyped nil values.
Args []any

// Signer is the address of public key that signed the incoming transaction.
// Signer is the address of public key that signed the incoming
// transaction.
Signer []byte

// Caller is a string identifier for the signer.
Expand Down
32 changes: 19 additions & 13 deletions common/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ var Accounts accounting
// accounting is a namespace for account specific functions.
type accounting struct{}

// Credit credits an account with the given amount. If the account does not exist, it will be created.
// A negative amount will be treated as a debit. Accounts cannot have negative balances, and will
// return an error if the amount would cause the balance to go negative.
// Credit credits an account with the given amount. If the account
// does not exist, it will be created. A negative amount will be
// treated as a debit. Accounts cannot have negative balances, and
// will return an error if the amount would cause the balance to go
// negative.
func (accounting) Credit(ctx context.Context, tx sql.DB, account []byte, amt *big.Int) error {
return accounts.Credit(ctx, tx, account, amt)
}

// Transfer transfers an amount from one account to another. If the from account does not have enough funds to transfer the amount,
// it will fail. If the to account does not exist, it will be created. The amount must be greater than 0.
// Transfer transfers an amount from one account to another. If the
// from account does not have enough funds to transfer the amount,
// it will fail. If the to account does not exist, it will be
// created. The amount must be greater than 0.
func (accounting) Transfer(ctx context.Context, tx sql.DB, from, to []byte, amt *big.Int) error {
return accounts.Transfer(ctx, tx, from, to, amt)
}

// GetAccount retrieves the account with the given identifier. If the account does not exist, it will
// return an account with a balance of 0 and a nonce of 0.
// GetAccount retrieves the account with the given identifier. If the
// account does not exist, it will return an account with a balance
// of 0 and a nonce of 0.
func (accounting) GetAccount(ctx context.Context, tx sql.DB, account []byte) (*types.Account, error) {
return accounts.GetAccount(ctx, tx, account)
}
Expand All @@ -40,8 +45,8 @@ var Validators validators

type validators struct{}

// GetValidatorPower retrieves the power of the given validator. If the validator does not exist, it will
// return 0.
// GetValidatorPower retrieves the power of the given validator. If
// the validator does not exist, it will return 0.
func (validators) GetValidatorPower(ctx context.Context, tx sql.DB, validator []byte) (int64, error) {
return voting.GetValidatorPower(ctx, tx, validator)
}
Expand All @@ -51,10 +56,11 @@ func (validators) GetValidators(ctx context.Context, tx sql.DB) ([]*types.Valida
return voting.GetValidators(ctx, tx)
}

// SetValidatorPower sets the power of a validator.
// If the target validator does not exist, it will be created with the given power.
// If set to 0, the target validator will be deleted, and will no longer be considered a validator.
// It will return an error if a negative power is given.
// SetValidatorPower sets the power of a validator. If the target
// validator does not exist, it will be created with the given power.
// If set to 0, the target validator will be deleted, and will no
// longer be considered a validator. It will return an error if a
// negative power is given.
func (validators) SetValidatorPower(ctx context.Context, tx sql.DB, validator []byte, power int64) error {
return voting.SetValidatorPower(ctx, tx, validator, power)
}
Loading

0 comments on commit 8323dd3

Please sign in to comment.