Skip to content

Commit

Permalink
Revert "Revert "Move out the hook modification from BaseApp""
Browse files Browse the repository at this point in the history
This reverts commit 56dfb9b.
  • Loading branch information
zivkovicmilos committed Oct 11, 2024
1 parent 56dfb9b commit 0a08d81
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
32 changes: 32 additions & 0 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/config"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
dbm "github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/events"
Expand Down Expand Up @@ -287,6 +288,37 @@ func (cfg InitChainerConfig) loadAppState(ctx sdk.Context, appState any) ([]abci
return nil, fmt.Errorf("invalid AppState of type %T", appState)
}

// TODO move to the initchainer implementation
superBeginTxHook := cfg.baseApp.BeginTxHook()
beginTxHook := func(ctx sdk.Context) sdk.Context {
retCtx := ctx

if superBeginTxHook != nil {
// Run the initially-set hook first
retCtx = superBeginTxHook(ctx)
}

if true { // TODO change to check if there is a genesis context
return retCtx
}

// TODO Modify the beginTx hook to give you the transaction,
// so metadata will be extracted

// Create a copy of the header, in
// which only the timestamp information is modified
header := retCtx.BlockHeader().(*bft.Header).Copy()
header.Time = time.Now().Add(time.Hour * 72) // TODO modify based on tx metadata

// Save the modified header
return retCtx.WithBlockHeader(header)
}

cfg.baseApp.SetBeginTxHook(beginTxHook)

// Restore the original beginTx hook
defer cfg.baseApp.SetBeginTxHook(superBeginTxHook)

// Parse and set genesis state balances
for _, bal := range state.Balances {
acc := cfg.acctKpr.NewAccountWithAddress(ctx, bal.Address)
Expand Down
36 changes: 4 additions & 32 deletions tm2/pkg/sdk/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sort"
"strings"
"syscall"
"time"

"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
Expand Down Expand Up @@ -337,39 +336,8 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
app.deliverState.ctx = app.deliverState.ctx.
WithBlockGasMeter(store.NewInfiniteGasMeter())

// Inject a custom
superBeginTxHook := app.beginTxHook
app.beginTxHook = func(ctx Context) Context {
retCtx := ctx

if superBeginTxHook != nil {
// Run the initially-set hook first
retCtx = superBeginTxHook(ctx)
}

if true { // TODO change to check if there is a genesis context
return retCtx
}

// TODO Modify the beginTx hook to give you the transaction,
// so metadata will be extracted

// Create a copy of the header, in
// which only the timestamp information is modified
header := initHeader.Copy()
header.Time = time.Now().Add(time.Hour * 72) // TODO modify based on tx metadata

// Save the modified header
retCtx.header = header

return retCtx
}

res = app.initChainer(app.deliverState.ctx, req)

// Restore the original beginTx hook
app.beginTxHook = superBeginTxHook

// sanity check
if len(req.Validators) > 0 {
if len(req.Validators) != len(res.Validators) {
Expand Down Expand Up @@ -979,6 +947,10 @@ func (app *BaseApp) Close() error {
return nil // XXX
}

func (app *BaseApp) BeginTxHook() BeginTxHook {
return app.beginTxHook
}

// ----------------------------------------------------------------------------
// State

Expand Down

0 comments on commit 0a08d81

Please sign in to comment.