chore: migrate core to gogoproto.Message (#20781)

This commit is contained in:
Marko
2024-06-28 09:34:23 +00:00
committed by GitHub
parent 5d54274775
commit c7375f7cd3
27 changed files with 183 additions and 153 deletions
+7 -23
View File
@@ -41,8 +41,8 @@ type Consensus[T transaction.Tx] struct {
// this is only available after this node has committed a block (in FinalizeBlock),
// otherwise it will be empty and we will need to query the app for the last
// committed block. TODO(tip): check if concurrency is really needed
lastCommittedBlock atomic.Pointer[BlockData]
// committed block.
lastCommittedHeight atomic.Int64
prepareProposalHandler handlers.PrepareHandler[T]
processProposalHandler handlers.ProcessHandler[T]
@@ -90,15 +90,6 @@ func (c *Consensus[T]) RegisterExtensions(extensions ...snapshots.ExtensionSnaps
}
}
// BlockData is used to keep some data about the last committed block. Currently
// we only use the height, the rest is not needed right now and might get removed
// in the future.
type BlockData struct {
Height int64
Hash []byte
StateChanges []store.StateChanges
}
// CheckTx implements types.Application.
// It is called by cometbft to verify transaction validity
func (c *Consensus[T]) CheckTx(ctx context.Context, req *abciproto.CheckTxRequest) (*abciproto.CheckTxResponse, error) {
@@ -407,10 +398,7 @@ func (c *Consensus[T]) FinalizeBlock(
if err != nil {
return nil, fmt.Errorf("unable to commit the changeset: %w", err)
}
c.lastCommittedBlock.Store(&BlockData{
Height: req.Height,
Hash: appHash,
})
c.lastCommittedHeight.Store(req.Height)
return &abciproto.FinalizeBlockResponse{
AppHash: appHash,
}, nil
@@ -482,11 +470,7 @@ func (c *Consensus[T]) FinalizeBlock(
return nil, fmt.Errorf("unable to remove txs: %w", err)
}
c.lastCommittedBlock.Store(&BlockData{
Height: req.Height,
Hash: appHash,
StateChanges: stateChanges,
})
c.lastCommittedHeight.Store(req.Height)
cp, err := c.GetConsensusParams(ctx) // we get the consensus params from the latest state because we committed state above
if err != nil {
@@ -499,9 +483,9 @@ func (c *Consensus[T]) FinalizeBlock(
// Commit implements types.Application.
// It is called by cometbft to notify the application that a block was committed.
func (c *Consensus[T]) Commit(ctx context.Context, _ *abciproto.CommitRequest) (*abciproto.CommitResponse, error) {
lastCommittedBlock := c.lastCommittedBlock.Load()
lastCommittedHeight := c.lastCommittedHeight.Load()
c.snapshotManager.SnapshotIfApplicable(lastCommittedBlock.Height)
c.snapshotManager.SnapshotIfApplicable(lastCommittedHeight)
cp, err := c.GetConsensusParams(ctx)
if err != nil {
@@ -509,7 +493,7 @@ func (c *Consensus[T]) Commit(ctx context.Context, _ *abciproto.CommitRequest) (
}
return &abci.CommitResponse{
RetainHeight: c.GetBlockRetentionHeight(cp, lastCommittedBlock.Height),
RetainHeight: c.GetBlockRetentionHeight(cp, lastCommittedHeight),
}, nil
}
+2 -2
View File
@@ -5,8 +5,8 @@ import (
"fmt"
"testing"
gogotypes "github.com/cosmos/gogoproto/types"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/wrapperspb"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/server/v2/stf/branch"
@@ -38,7 +38,7 @@ func TestBranchService(t *testing.T) {
makeGasMeter: gas.DefaultGasMeter,
makeGasMeteredState: gas.DefaultWrapWithGasMeter,
}
addMsgHandlerToSTF(t, s, func(ctx context.Context, msg *wrapperspb.BoolValue) (*wrapperspb.BoolValue, error) {
addMsgHandlerToSTF(t, s, func(ctx context.Context, msg *gogotypes.BoolValue) (*gogotypes.BoolValue, error) {
kvSet(t, ctx, "exec")
return nil, nil
})
+2 -3
View File
@@ -7,7 +7,6 @@ import (
gogoproto "github.com/cosmos/gogoproto/proto"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/runtime/protoiface"
"cosmossdk.io/core/event"
)
@@ -31,7 +30,7 @@ type eventManager struct {
// Emit emits an typed event that is defined in the protobuf file.
// In the future these events will be added to consensus.
func (em *eventManager) Emit(tev protoiface.MessageV1) error {
func (em *eventManager) Emit(tev gogoproto.Message) error {
res, err := TypedEventToEvent(tev)
if err != nil {
return err
@@ -49,7 +48,7 @@ func (em *eventManager) EmitKV(eventType string, attrs ...event.Attribute) error
// EmitNonConsensus emits an typed event that is defined in the protobuf file.
// These events will not be added to consensus.
func (em *eventManager) EmitNonConsensus(event protoiface.MessageV1) error {
func (em *eventManager) EmitNonConsensus(event gogoproto.Message) error {
return em.Emit(event)
}
+6 -6
View File
@@ -3,7 +3,7 @@ package stf
import (
"context"
"google.golang.org/protobuf/runtime/protoiface"
gogoproto "github.com/cosmos/gogoproto/proto"
"cosmossdk.io/core/router"
"cosmossdk.io/core/transaction"
@@ -31,12 +31,12 @@ func (m msgRouterService) CanInvoke(ctx context.Context, typeURL string) error {
// InvokeTyped execute a message and fill-in a response.
// The response must be known and passed as a parameter.
// Use InvokeUntyped if the response type is not known.
func (m msgRouterService) InvokeTyped(ctx context.Context, msg, resp protoiface.MessageV1) error {
func (m msgRouterService) InvokeTyped(ctx context.Context, msg, resp gogoproto.Message) error {
return ctx.(*executionContext).msgRouter.InvokeTyped(ctx, msg, resp)
}
// InvokeUntyped execute a message and returns a response.
func (m msgRouterService) InvokeUntyped(ctx context.Context, msg protoiface.MessageV1) (protoiface.MessageV1, error) {
func (m msgRouterService) InvokeUntyped(ctx context.Context, msg gogoproto.Message) (gogoproto.Message, error) {
return ctx.(*executionContext).msgRouter.InvokeUntyped(ctx, msg)
}
@@ -59,7 +59,7 @@ func (m queryRouterService) CanInvoke(ctx context.Context, typeURL string) error
// Use InvokeUntyped if the response type is not known.
func (m queryRouterService) InvokeTyped(
ctx context.Context,
req, resp protoiface.MessageV1,
req, resp gogoproto.Message,
) error {
return ctx.(*executionContext).queryRouter.InvokeTyped(ctx, req, resp)
}
@@ -67,7 +67,7 @@ func (m queryRouterService) InvokeTyped(
// InvokeUntyped execute a message and returns a response.
func (m queryRouterService) InvokeUntyped(
ctx context.Context,
req protoiface.MessageV1,
) (protoiface.MessageV1, error) {
req gogoproto.Message,
) (gogoproto.Message, error) {
return ctx.(*executionContext).queryRouter.InvokeUntyped(ctx, req)
}
+1 -1
View File
@@ -10,7 +10,6 @@ require (
github.com/stretchr/testify v1.9.0
github.com/tidwall/btree v1.7.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
google.golang.org/protobuf v1.34.2
)
require (
@@ -18,5 +17,6 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+3 -4
View File
@@ -7,7 +7,6 @@ import (
"reflect"
gogoproto "github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/runtime/protoiface"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/router"
@@ -155,7 +154,7 @@ func (r Router) CanInvoke(_ context.Context, typeURL string) error {
return nil
}
func (r Router) InvokeTyped(ctx context.Context, req, resp protoiface.MessageV1) error {
func (r Router) InvokeTyped(ctx context.Context, req, resp gogoproto.Message) error {
handlerResp, err := r.InvokeUntyped(ctx, req)
if err != nil {
return err
@@ -164,11 +163,11 @@ func (r Router) InvokeTyped(ctx context.Context, req, resp protoiface.MessageV1)
return nil
}
func merge(src, dst protoiface.MessageV1) {
func merge(src, dst gogoproto.Message) {
reflect.Indirect(reflect.ValueOf(dst)).Set(reflect.Indirect(reflect.ValueOf(src)))
}
func (r Router) InvokeUntyped(ctx context.Context, req protoiface.MessageV1) (res protoiface.MessageV1, err error) {
func (r Router) InvokeUntyped(ctx context.Context, req gogoproto.Message) (res gogoproto.Message, err error) {
typeName := msgTypeURL(req)
handler, exists := r.handlers[typeName]
if !exists {
+7 -7
View File
@@ -8,8 +8,8 @@ import (
"time"
"github.com/cosmos/gogoproto/proto"
gogotypes "github.com/cosmos/gogoproto/types"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/wrapperspb"
appmanager "cosmossdk.io/core/app"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
@@ -57,7 +57,7 @@ func TestSTF(t *testing.T) {
state := mock.DB()
mockTx := mock.Tx{
Sender: []byte("sender"),
Msg: wrapperspb.Bool(true),
Msg: &gogotypes.BoolValue{Value: true},
GasLimit: 100_000,
}
@@ -87,7 +87,7 @@ func TestSTF(t *testing.T) {
makeGasMeteredState: gas.DefaultWrapWithGasMeter,
}
addMsgHandlerToSTF(t, s, func(ctx context.Context, msg *wrapperspb.BoolValue) (*wrapperspb.BoolValue, error) {
addMsgHandlerToSTF(t, s, func(ctx context.Context, msg *gogotypes.BoolValue) (*gogotypes.BoolValue, error) {
kvSet(t, ctx, "exec")
return nil, nil
})
@@ -128,8 +128,8 @@ func TestSTF(t *testing.T) {
mockTx := mock.Tx{
Sender: []byte("sender"),
Msg: wrapperspb.Bool(true), // msg does not matter at all because our handler does nothing.
GasLimit: 0, // NO GAS!
Msg: &gogotypes.BoolValue{Value: true}, // msg does not matter at all because our handler does nothing.
GasLimit: 0, // NO GAS!
}
// this handler will propagate the storage error back, we expect
@@ -157,7 +157,7 @@ func TestSTF(t *testing.T) {
t.Run("fail exec tx", func(t *testing.T) {
// update the stf to fail on the handler
s := s.clone()
addMsgHandlerToSTF(t, &s, func(ctx context.Context, msg *wrapperspb.BoolValue) (*wrapperspb.BoolValue, error) {
addMsgHandlerToSTF(t, &s, func(ctx context.Context, msg *gogotypes.BoolValue) (*gogotypes.BoolValue, error) {
return nil, fmt.Errorf("failure")
})
@@ -200,7 +200,7 @@ func TestSTF(t *testing.T) {
t.Run("tx failed and post tx failed", func(t *testing.T) {
s := s.clone()
addMsgHandlerToSTF(t, &s, func(ctx context.Context, msg *wrapperspb.BoolValue) (*wrapperspb.BoolValue, error) {
addMsgHandlerToSTF(t, &s, func(ctx context.Context, msg *gogotypes.BoolValue) (*gogotypes.BoolValue, error) {
return nil, fmt.Errorf("exec failure")
})
s.postTxExec = func(ctx context.Context, tx mock.Tx, success bool) error { return fmt.Errorf("post tx failure") }