Skip to content

feat: use datastore directly for state store #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
cmtypes "github.com/cometbft/cometbft/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
ds "github.com/ipfs/go-datastore"

"github.com/rollkit/rollkit/core/execution"
rollkitp2p "github.com/rollkit/rollkit/pkg/p2p"
Expand All @@ -37,10 +38,11 @@ func LoadGenesisDoc(cfg *config.Config) (*cmtypes.GenesisDoc, error) {

// Adapter is a struct that will contain an ABCI Application, and will implement the go-execution interface
type Adapter struct {
App servertypes.ABCI
Store store.Store
Mempool mempool.Mempool
MempoolIDs *mempoolIDs
App servertypes.ABCI
Store ds.Batching
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personal preference: this is not used somewhere else and can be private

not sure if this is the same idea as @tac0turtle mentioned below but if you would replace batch with a custom state store type, you could encapsulate load/store and avoid the verbose db type.
This may even enable features like caching in the future

RollkitStore store.Store
Mempool mempool.Mempool
MempoolIDs *mempoolIDs

P2PClient *rollkitp2p.Client
TxGossiper *p2p.Gossiper
Expand All @@ -58,7 +60,7 @@ type Adapter struct {
// The Adapter wraps the provided ABCI application and delegates execution-related operations to it.
func NewABCIExecutor(
app servertypes.ABCI,
store store.Store,
store ds.Batching,
p2pClient *rollkitp2p.Client,
p2pMetrics *rollkitp2p.Metrics,
logger log.Logger,
Expand Down Expand Up @@ -391,7 +393,7 @@ func (a *Adapter) GetTxs(ctx context.Context) ([][]byte, error) {
txsBytes[i] = tx
}

currentHeight, err := a.Store.Height(ctx)
currentHeight, err := a.RollkitStore.Height(ctx)
if err != nil {
return nil, err
}
Comment on lines +402 to 405
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Guard against nil RollkitStore before calling Height

Even with the constructor fix, defensive code is advisable:

- currentHeight, err := a.RollkitStore.Height(ctx)
+ if a.RollkitStore == nil {
+     return nil, fmt.Errorf("RollkitStore not initialised")
+ }
+ currentHeight, err := a.RollkitStore.Height(ctx)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
currentHeight, err := a.RollkitStore.Height(ctx)
if err != nil {
return nil, err
}
if a.RollkitStore == nil {
return nil, fmt.Errorf("RollkitStore not initialised")
}
currentHeight, err := a.RollkitStore.Height(ctx)
if err != nil {
return nil, err
}

Expand All @@ -417,7 +419,7 @@ func (a *Adapter) SetFinal(ctx context.Context, blockHeight uint64) error {
return nil
}

func NewAdapter(store store.Store) *Adapter {
func NewAdapter(store ds.Batching) *Adapter {
return &Adapter{
Store: store,
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/adapter/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
cmtstateproto "github.com/cometbft/cometbft/proto/tendermint/state"
cmtstate "github.com/cometbft/cometbft/state"
proto "github.com/cosmos/gogoproto/proto"

"github.com/rollkit/rollkit/pkg/store"
ds "github.com/ipfs/go-datastore"
)

const stateKey = "abci-s"

// loadState loads the state from disk
func loadState(ctx context.Context, s store.Store) (*cmtstate.State, error) {
data, err := s.GetMetadata(ctx, stateKey)
func loadState(ctx context.Context, s ds.Batching) (*cmtstate.State, error) {
data, err := s.Get(ctx, ds.NewKey(stateKey))
if err != nil {
return nil, fmt.Errorf("failed to get state metadata: %w", err)
}
Expand All @@ -32,7 +31,7 @@ func loadState(ctx context.Context, s store.Store) (*cmtstate.State, error) {
}

// saveState saves the state to disk
func saveState(ctx context.Context, s store.Store, state *cmtstate.State) error {
func saveState(ctx context.Context, s ds.Batching, state *cmtstate.State) error {
stateProto, err := state.ToProto()
if err != nil {
return fmt.Errorf("failed to convert state to proto: %w", err)
Expand All @@ -43,5 +42,5 @@ func saveState(ctx context.Context, s store.Store, state *cmtstate.State) error
return fmt.Errorf("failed to marshal state: %w", err)
}

return s.SetMetadata(ctx, stateKey, data)
return s.Put(ctx, ds.NewKey(stateKey), data)
}
Loading