Skip to content

Commit

Permalink
migrate export genesis and remove stf.RunWithCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Sep 15, 2024
1 parent a977338 commit 30814ac
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/simapp/v2/simdv2",
"args": ["start"],
"args": ["genesis", "export"],
"buildFlags": ""
},
{
Expand Down
12 changes: 11 additions & 1 deletion runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,17 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
return genesisState, err
},
ExportGenesis: func(ctx context.Context, version uint64) ([]byte, error) {
genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx)
_, state, err := a.app.db.StateLatest()
if err != nil {
return nil, fmt.Errorf("unable to get latest state: %w", err)
}
genesisCtx := makeGenesisContext(a.branch(state))

var genesisJson map[string]json.RawMessage
_, err = genesisCtx.Run(ctx, func(ctx context.Context) error {
genesisJson, err = a.app.moduleManager.ExportGenesisForModules(ctx)
return err
})
if err != nil {
return nil, fmt.Errorf("failed to export genesis: %w", err)
}
Expand Down
24 changes: 6 additions & 18 deletions runtime/v2/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,32 @@ func (g *genesisContext) Run(
}

type GenesisKVStoreServie struct {
genesisCapable bool
actor []byte
execution store.KVStoreService
actor []byte
executionService store.KVStoreService
}

func NewGenesisKVService(
actor []byte,
execution store.KVStoreService,
) *GenesisKVStoreServie {
return &GenesisKVStoreServie{
genesisCapable: true,
actor: actor,
execution: execution,
actor: actor,
executionService: execution,
}
}

// OpenKVStore implements store.KVStoreService.
func (g *GenesisKVStoreServie) OpenKVStore(ctx context.Context) store.KVStore {
if !g.genesisCapable {
return g.execution.OpenKVStore(ctx)
}
v := ctx.Value(genesisContextKey)
if v == nil {
return g.execution.OpenKVStore(ctx)
return g.executionService.OpenKVStore(ctx)
}
genCtx, ok := v.(*genesisContext)
if !ok {
panic(fmt.Errorf("unexpected genesis context type: %T", v))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}
if genCtx.didRun {
g.genesisCapable = false
return g.execution.OpenKVStore(ctx)
return g.executionService.OpenKVStore(ctx)
}
state, err := genCtx.state.GetWriter(g.actor)
if err != nil {
Expand All @@ -80,15 +74,11 @@ func (g *GenesisKVStoreServie) OpenKVStore(ctx context.Context) store.KVStore {
}

type GenesisHeaderService struct {
genesisCapable bool
executionService header.Service
}

// HeaderInfo implements header.Service.
func (g *GenesisHeaderService) HeaderInfo(ctx context.Context) header.Info {
if !g.genesisCapable {
return g.executionService.HeaderInfo(ctx)
}
v := ctx.Value(genesisContextKey)
if v == nil {
return g.executionService.HeaderInfo(ctx)
Expand All @@ -98,15 +88,13 @@ func (g *GenesisHeaderService) HeaderInfo(ctx context.Context) header.Info {
panic(fmt.Errorf("unexpected genesis context type: %T", v))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}
if genCtx.didRun {
g.genesisCapable = false
return g.executionService.HeaderInfo(ctx)
}
return header.Info{}
}

func NewGenesisHeaderService(executionService header.Service) *GenesisHeaderService {
return &GenesisHeaderService{
genesisCapable: true,
executionService: executionService,
}
}
Expand Down
24 changes: 3 additions & 21 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,11 @@ func (a AppManager[T]) InitGenesis(

// ExportGenesis exports the genesis state of the application.
func (a AppManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byte, error) {
zeroState, err := a.db.StateAt(version)
if err != nil {
return nil, fmt.Errorf("unable to get latest state: %w", err)
}

bz := make([]byte, 0)
_, err = a.stf.RunWithCtx(ctx, zeroState, func(ctx context.Context) error {
if a.exportGenesis == nil {
return errors.New("export genesis function not set")
}

bz, err = a.exportGenesis(ctx, version)
if err != nil {
return fmt.Errorf("failed to export genesis state: %w", err)
}

return nil
})
if err != nil {
return nil, fmt.Errorf("failed to export genesis state: %w", err)
if a.exportGenesis == nil {
return nil, errors.New("export genesis function not set")
}

return bz, nil
return a.exportGenesis(ctx, version)
}

func (a AppManager[T]) DeliverBlock(
Expand Down
8 changes: 0 additions & 8 deletions server/v2/appmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,4 @@ type StateTransitionFunction[T transaction.Tx] interface {
gasLimit uint64,
req transaction.Msg,
) (transaction.Msg, error)

// RunWithCtx executes the provided closure within a context.
// TODO: remove
RunWithCtx(
ctx context.Context,
state store.ReaderMap,
closure func(ctx context.Context) error,
) (store.WriterMap, error)
}
39 changes: 23 additions & 16 deletions server/v2/stf/stf.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,14 @@ func (s STF[T]) deliverTx(
}
}

execResp, execGas, execEvents, err := s.execTx(ctx, state, gasLimit-validateGas, tx, execMode, hi)
execResp, execGas, execEvents, err := s.execTx(
ctx,
state,
gasLimit-validateGas,
tx,
execMode,
hi,
)
return server.TxResult{
Events: append(validationEvents, execEvents...),
GasUsed: execGas + validateGas,
Expand Down Expand Up @@ -250,7 +257,14 @@ func (s STF[T]) execTx(
) ([]transaction.Msg, uint64, []event.Event, error) {
execState := s.branchFn(state)

msgsResp, gasUsed, runTxMsgsEvents, txErr := s.runTxMsgs(ctx, execState, gasLimit, tx, execMode, hi)
msgsResp, gasUsed, runTxMsgsEvents, txErr := s.runTxMsgs(
ctx,
execState,
gasLimit,
tx,
execMode,
hi,
)
if txErr != nil {
// in case of error during message execution, we do not apply the exec state.
// instead we run the post exec handler in a new branchFn from the initial state.
Expand Down Expand Up @@ -430,7 +444,13 @@ func (s STF[T]) ValidateTx(
tx T,
) server.TxResult {
validationState := s.branchFn(state)
gasUsed, events, err := s.validateTx(ctx, validationState, gasLimit, tx, transaction.ExecModeCheck)
gasUsed, events, err := s.validateTx(
ctx,
validationState,
gasLimit,
tx,
transaction.ExecModeCheck,
)
return server.TxResult{
Events: events,
GasUsed: gasUsed,
Expand All @@ -456,19 +476,6 @@ func (s STF[T]) Query(
return s.queryRouter.Invoke(queryCtx, req)
}

// RunWithCtx is made to support genesis, if genesis was just the execution of messages instead
// of being something custom then we would not need this. PLEASE DO NOT USE.
// TODO: Remove
func (s STF[T]) RunWithCtx(
ctx context.Context,
state store.ReaderMap,
closure func(ctx context.Context) error,
) (store.WriterMap, error) {
branchedState := s.branchFn(state)
stfCtx := s.makeContext(ctx, nil, branchedState, internal.ExecModeFinalize)
return branchedState, closure(stfCtx)
}

// clone clones STF.
func (s STF[T]) clone() STF[T] {
return STF[T]{
Expand Down

0 comments on commit 30814ac

Please sign in to comment.