Skip to content

Commit

Permalink
Support genesis and endblock hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
charithabandi committed Dec 13, 2024
1 parent 00329ee commit 5225cac
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 4 deletions.
104 changes: 104 additions & 0 deletions extensions/hooks/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package hooks

import (
"context"
"fmt"

"github.com/kwilteam/kwil-db/common"
"github.com/kwilteam/kwil-db/core/utils/order"
)

// GenesisHook is a function that is run exactly once, at network genesis.
// It can be used to create initial state or perform other setup tasks.
// If it returns an error, the network will immediately halt. Any state
// changed or error returned should be deterministic, as all nodes will
// run the same GenesisHooks in the same order.
type GenesisHook func(ctx context.Context, app *common.App, chain *common.ChainContext) error

// RegisterGenesisHook registers a GenesisHook to be run at network genesis.
// The name can be anything, as long as it is unique. It is used to deterministically
// order the hooks.
func RegisterGenesisHook(name string, hook GenesisHook) error {
_, ok := genesisHooks[name]
if ok {
return fmt.Errorf("genesis hook with name %s already exists", name)
}

genesisHooks[name] = hook
return nil
}

var genesisHooks map[string]GenesisHook

// ListGenesisHooks deterministically returns a list of all registered GenesisHooks.
func ListGenesisHooks() []struct {
Name string
Hook GenesisHook
} {
hooks := make([]struct {
Name string
Hook GenesisHook
}, 0, len(genesisHooks))
for _, hook := range order.OrderMap(genesisHooks) {
hooks = append(hooks, struct {
Name string
Hook GenesisHook
}{
Name: hook.Key,
Hook: hook.Value,
})
}

return hooks
}

// EndBlockHook is a function that is run at the end of each block, after
// all of the transactions in the block have been processed, but before the
// any state has been committed. It is meant to be used to alter state, send
// data to external services, or perform cleanup tasks for other extensions.
// An error returned will halt the local node. All state changes and errors
// should be deterministic, as all nodes will run the same EndBlockHooks in
// the same order.
type EndBlockHook func(ctx context.Context, app *common.App, block *common.BlockContext) error

// RegisterEndBlockHook registers an EndBlockHook to be run at the end of each block.
// The name can be anything, as long as it is unique. It is used to deterministically
// order the hooks.
func RegisterEndBlockHook(name string, hook EndBlockHook) error {
_, ok := endBlockHooks[name]
if ok {
return fmt.Errorf("end block hook with name %s already exists", name)
}

endBlockHooks[name] = hook
return nil
}

var endBlockHooks map[string]EndBlockHook

// ListEndBlockHooks deterministically returns a list of all registered EndBlockHooks.
func ListEndBlockHooks() []struct {
Name string
Hook EndBlockHook
} {
var hooks []struct {
Name string
Hook EndBlockHook
}
for _, hook := range order.OrderMap(endBlockHooks) {
hooks = append(hooks, struct {
Name string
Hook EndBlockHook
}{
Name: hook.Key,
Hook: hook.Value,
})
}

return hooks
}

func init() {
genesisHooks = make(map[string]GenesisHook)
endBlockHooks = make(map[string]EndBlockHook)
}
19 changes: 18 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,29 @@ require (
)

require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/certgen v1.2.0 // indirect
github.com/decred/dcrd/crypto/rand v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/decred/slog v1.2.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand All @@ -68,14 +79,20 @@ require (
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
gonum.org/v1/gonum v0.15.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)

require (
Expand All @@ -90,7 +107,7 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/elastic/gosigar v0.14.3 // indirect
github.com/ethereum/go-ethereum v1.14.11 // indirect
github.com/ethereum/go-ethereum v1.14.12
github.com/flynn/noise v1.1.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
Expand Down
Loading

0 comments on commit 5225cac

Please sign in to comment.