Skip to content

Commit

Permalink
chore: clean
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed Jun 10, 2024
1 parent 8fa79be commit e4dd0f6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 48 deletions.
12 changes: 10 additions & 2 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ func PanicOnFailingTxHandler(ctx sdk.Context, tx std.Tx, res sdk.Result) {
// InitChainer returns a function that can initialize the chain with genesis.
func InitChainer(baseApp *sdk.BaseApp, acctKpr auth.AccountKeeperI, bankKpr bank.BankKeeperI, resHandler GenesisTxHandler) func(sdk.Context, abci.RequestInitChain) abci.ResponseInitChain {
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
txResponses := []abci.ResponseDeliverTx{}

Check warning on line 160 in gno.land/pkg/gnoland/app.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoland/app.go#L160

Added line #L160 was not covered by tests

if req.AppState != nil {
// Get genesis state
genState := req.AppState.(GnoGenesisState)
Expand All @@ -183,14 +185,20 @@ func InitChainer(baseApp *sdk.BaseApp, acctKpr auth.AccountKeeperI, bankKpr bank
)
}

txResponses = append(txResponses, abci.ResponseDeliverTx{
ResponseBase: res.ResponseBase,
GasWanted: res.GasWanted,
GasUsed: res.GasUsed,
})

Check warning on line 192 in gno.land/pkg/gnoland/app.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoland/app.go#L188-L192

Added lines #L188 - L192 were not covered by tests

resHandler(ctx, tx, res)
}
}

// Done!
return abci.ResponseInitChain{
Validators: req.Validators,
// TxResponses: txResponses,
Validators: req.Validators,
TxResponses: txResponses,

Check warning on line 201 in gno.land/pkg/gnoland/app.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoland/app.go#L200-L201

Added lines #L200 - L201 were not covered by tests
}
}
}
Expand Down
17 changes: 3 additions & 14 deletions tm2/pkg/bft/consensus/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
"reflect"
"time"

"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/appconn"
cstypes "github.com/gnolang/gno/tm2/pkg/bft/consensus/types"
"github.com/gnolang/gno/tm2/pkg/bft/mempool/mock"
"github.com/gnolang/gno/tm2/pkg/bft/proxy"
sm "github.com/gnolang/gno/tm2/pkg/bft/state"
statelib "github.com/gnolang/gno/tm2/pkg/bft/state"
"github.com/gnolang/gno/tm2/pkg/bft/types"
walm "github.com/gnolang/gno/tm2/pkg/bft/wal"
dbm "github.com/gnolang/gno/tm2/pkg/db"
Expand Down Expand Up @@ -308,11 +306,11 @@ func (h *Handshaker) ReplayBlocks(
if err != nil {
return nil, err
}
fmt.Println("got", len(res.TxResponses), "responses after init")

// save responses
abciResponse := sm.NewABCIResponsesFromNum(len(res.TxResponses))
copy(abciResponse.DeliverTxs, res.TxResponses)
statelib.SaveABCIResponses(h.stateDB, 0, abciResponse)
saveGenesisRes(h.stateDB, &res)
sm.SaveABCIResponses(h.stateDB, 0, abciResponse)

if stateBlockHeight == 0 { // we only update state when we are in initial state
// If the app returned validators or consensus params, update the state.
Expand Down Expand Up @@ -400,15 +398,6 @@ func (h *Handshaker) ReplayBlocks(
appBlockHeight, storeBlockHeight, stateBlockHeight))
}

// panics if failed to marshal the given genesis response
func saveGenesisRes(db dbm.DB, res *abci.ResponseInitChain) {
b, err := amino.MarshalJSON(res)
if err != nil {
panic(fmt.Sprintf("Failed to save genesis response due to marshaling error: %v", err))
}
db.SetSync([]byte("genesisRes"), b)
}

func (h *Handshaker) replayBlocks(state sm.State, proxyApp appconn.AppConns, appBlockHeight, storeBlockHeight int64, mutateState bool) ([]byte, error) {
// App is further behind than it should be, so we need to replay blocks.
// We replay all blocks from appBlockHeight+1.
Expand Down
11 changes: 4 additions & 7 deletions tm2/pkg/bft/consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"log/slog"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"testing"
Expand Down Expand Up @@ -1174,8 +1173,6 @@ func (ica *initChainApp) InitChain(req abci.RequestInitChain) abci.ResponseInitC
}
}

const numInitResponses = 3

func TestHandshakeGenesisResponseDeliverTx(t *testing.T) {
t.Parallel()

Expand All @@ -1188,8 +1185,6 @@ func TestHandshakeGenesisResponseDeliverTx(t *testing.T) {

// now start the app using the handshake - it should sync
genDoc, _ := sm.MakeGenesisDocFromFile(genesisFile)
tt := reflect.TypeOf(genDoc.AppState)
fmt.Println("genDoc.AppState type", tt)
handshaker := NewHandshaker(stateDB, state, store, genDoc)
proxyApp := appconn.NewAppConns(clientCreator)
if err := proxyApp.Start(); err != nil {
Expand All @@ -1203,14 +1198,16 @@ func TestHandshakeGenesisResponseDeliverTx(t *testing.T) {
// check that the genesis transaction results are saved
res, err := sm.LoadABCIResponses(stateDB, 0)
if err != nil {
t.Fatalf("Failed to load ABCI responses: %v", err)
t.Fatalf("Failed to load genesis ABCI responses: %v", err)
}

if len(res.DeliverTxs) != numInitResponses {
t.Fatalf("Expected %d responses, got %d", numInitResponses, len(res.DeliverTxs))
t.Fatalf("Expected %d genesis tx responses, got %d", numInitResponses, len(res.DeliverTxs))
}
}

const numInitResponses = 42

type initTxsApp struct {
abci.BaseApplication
}
Expand Down
24 changes: 1 addition & 23 deletions tm2/pkg/bft/rpc/core/net.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package core

import (
"fmt"

"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
rpctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/lib/types"
dbm "github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/errors"
)

Expand Down Expand Up @@ -256,22 +251,5 @@ func UnsafeDialPeers(ctx *rpctypes.Context, peers []string, persistent bool) (*c
//
// ```
func Genesis(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
res, err := loadGenesisRes(stateDB)
if err != nil {
return nil, err
}
return &ctypes.ResultGenesis{Genesis: genDoc, Response: res}, nil
}

func loadGenesisRes(db dbm.DB) (*abci.ResponseInitChain, error) {
b := db.Get([]byte("genesisRes"))
if len(b) == 0 {
return nil, errors.New("Genesis res not found")
}
var genRes *abci.ResponseInitChain
err := amino.UnmarshalJSON(b, &genRes)
if err != nil {
panic(fmt.Sprintf("Failed to load genesis res due to unmarshaling error: %v (bytes: %X)", err, b))
}
return genRes, nil
return &ctypes.ResultGenesis{Genesis: genDoc}, nil
}
3 changes: 1 addition & 2 deletions tm2/pkg/bft/rpc/core/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ type ResultBlockchainInfo struct {

// Genesis file
type ResultGenesis struct {
Genesis *types.GenesisDoc `json:"genesis"`
Response *abci.ResponseInitChain `json:"response"`
Genesis *types.GenesisDoc `json:"genesis"`
}

// Single block (with meta)
Expand Down

0 comments on commit e4dd0f6

Please sign in to comment.