Refactor x/bank according to ADR 031 (#7520)
* Refactor x/bank according to ADR 031 * Add comment * Update comment * Add comment * Add tests, address edge cases * Imports Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot]
parent
45f5df7ea5
commit
9be15a42b9
@@ -7,8 +7,11 @@ import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
@@ -263,3 +266,30 @@ func (r TxResponse) GetTx() Tx {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WrapServiceResult wraps a result from a protobuf RPC service method call in
|
||||
// a Result object or error. This method takes care of marshaling the res param to
|
||||
// protobuf and attaching any events on the ctx.EventManager() to the Result.
|
||||
func WrapServiceResult(ctx Context, res proto.Message, err error) (*Result, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var data []byte
|
||||
if res != nil {
|
||||
data, err = proto.Marshal(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var events []abci.Event
|
||||
if evtMgr := ctx.EventManager(); evtMgr != nil {
|
||||
events = evtMgr.ABCIEvents()
|
||||
}
|
||||
|
||||
return &Result{
|
||||
Data: data,
|
||||
Events: events,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,13 @@ package types_test
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
@@ -12,6 +16,7 @@ import (
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
@@ -180,3 +185,33 @@ func (s *resultTestSuite) TestResponseFormatBroadcastTxCommit() {
|
||||
s.Require().Equal(want, sdk.NewResponseFormatBroadcastTxCommit(checkTxResult))
|
||||
s.Require().Equal(want, sdk.NewResponseFormatBroadcastTxCommit(deliverTxResult))
|
||||
}
|
||||
|
||||
func TestWrapServiceResult(t *testing.T) {
|
||||
ctx := sdk.Context{}
|
||||
|
||||
res, err := sdk.WrapServiceResult(ctx, nil, fmt.Errorf("test"))
|
||||
require.Nil(t, res)
|
||||
require.NotNil(t, err)
|
||||
|
||||
res, err = sdk.WrapServiceResult(ctx, nil, nil)
|
||||
require.NotNil(t, res)
|
||||
require.Nil(t, err)
|
||||
require.Empty(t, res.Events)
|
||||
|
||||
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
||||
ctx.EventManager().EmitEvent(sdk.NewEvent("test"))
|
||||
res, err = sdk.WrapServiceResult(ctx, nil, nil)
|
||||
require.NotNil(t, res)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, res.Events, 1)
|
||||
|
||||
spot := testdata.Dog{Name: "spot"}
|
||||
res, err = sdk.WrapServiceResult(ctx, &spot, nil)
|
||||
require.NotNil(t, res)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, res.Events, 1)
|
||||
var spot2 testdata.Dog
|
||||
err = proto.Unmarshal(res.Data, &spot2)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, spot, spot2)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user