8000 feat(packet): scaffold an acknowledgment type for each new packet by lumtis · Pull Request #800 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(packet): scaffold an acknowledgment type for each new packet #800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require (
golang.org/x/mod v0.4.1
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 // indirect
golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
google.golang.org/grpc v1.35.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 h1:8qxJSnu+7dRq6upnbntrmriWByIakBuct5OM/MdQC1M=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b h1:kHlr0tATeLRMEiZJu5CknOw/E8V6h69sXXQFGoPtjcc=
golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
Expand Down
2 changes: 1 addition & 1 deletion integration/cmd_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestCreateIBCPacket(t *testing.T) {

env.Must(env.Exec("create a packet",
step.NewSteps(step.New(
step.Exec("starport", "packet", "bar", "text", "--module", "foo"),
step.Exec("starport", "packet", "bar", "text", "--module", "foo", "--ack", "ack1:string,ack2:int,ack3:bool"),
step.Workdir(path),
)),
))
Expand Down
16 changes: 13 additions & 3 deletions starport/interface/cli/starport/cmd/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/tendermint/starport/starport/services/scaffolder"
)

const (
ackFlag = "ack"
)

// NewIBCPacket creates a new packet in the module
func NewIBCPacket() *cobra.Command {
c := &cobra.Command{
Expand All @@ -19,6 +23,7 @@ func NewIBCPacket() *cobra.Command {
RunE: createPacketHandler,
}

c.Flags().StringSlice(ackFlag, []string{}, "Custom acknowledgment type (field1,field2,...)")
c.Flags().String(moduleFlag, "", "IBC Module to add the packet into")

return c
Expand All @@ -29,8 +34,8 @@ func createPacketHandler(cmd *cobra.Command, args []string) error {
defer s.Stop()

var (
packet = args[0]
fields = args[1:]
packet = args[0]
packetFields = args[1:]
)

module, err := cmd.Flags().GetString(moduleFlag)
Expand All @@ -41,8 +46,13 @@ func createPacketHandler(cmd *cobra.Command, args []string) error {
return errors.New("please specify a module to create the packet into: --module <module_name>")
}

ackFields, err := cmd.Flags().GetStringSlice(ackFlag)
if err != nil {
return err
}

sc := scaffolder.New(appPath)
if err := sc.AddPacket(module, packet, fields...); err != nil {
if err := sc.AddPacket(module, packet, packetFields, ackFields); err != nil {
return err
}

Expand Down
17 changes: 12 additions & 5 deletions starport/services/scaffolder/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
)

// AddType adds a new type stype to scaffolded app by using optional type fields.
func (s *Scaffolder) AddPacket(moduleName string, packetName string, fields ...string) error {
func (s *Scaffolder) AddPacket(moduleName string, packetName string, packetFields []string, ackFields []string) error {
version, err := s.version()
if err != nil {
return err
Expand Down Expand Up @@ -62,8 +62,14 @@ func (s *Scaffolder) AddPacket(moduleName string, packetName string, fields ...s
return fmt.Errorf("the packet %s already exist", packetName)
}

// Parse provided field
tFields, err := parseFields(fields)
// Parse packet fields
parsedPacketFields, err := parseFields(packetFields)
if err != nil {
return err
}

// Parse acknowledgment fields
parsedAcksFields, err := parseFields(ackFields)
if err != nil {
return err
}
Expand All @@ -77,10 +83,11 @@ func (s *Scaffolder) AddPacket(moduleName string, packetName string, fields ...s
ModuleName: moduleName,
OwnerName: owner(path.RawPath),
PacketName: packetName,
Fields: tFields,
Fields: parsedPacketFields,
AckFields: parsedAcksFields,
}
)
g, err = ibc.NewIBC(opts)
g, err = ibc.NewPacket(opts)
if err != nil {
return err
}
Expand Down
34 changes: 27 additions & 7 deletions starport/templates/ibc/new_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type PacketOptions struct {
OwnerName string
PacketName string
Fields []typed.Field
AckFields []typed.Field
}

// New ...
func NewIBC(opts *PacketOptions) (*genny.Generator, error) {
func NewPacket(opts *PacketOptions) (*genny.Generator, error) {
g := genny.New()

g.RunFn(moduleModify(opts))
Expand All @@ -53,6 +54,7 @@ func NewIBC(opts *PacketOptions) (*genny.Generator, error) {
ctx.Set("packetName", opts.PacketName)
ctx.Set("ownerName", opts.OwnerName)
ctx.Set("fields", opts.Fields)
ctx.Set("ackFields", opts.AckFields)
ctx.Set("title", strings.Title)

ctx.Set("nodash", func(s string) string {
Expand All @@ -76,9 +78,16 @@ func moduleModify(opts *PacketOptions) genny.RunFn {
// Recv packet dispatch
templateRecv := `%[1]v
case *types.%[2]vPacketData_%[3]vPacket:
err := am.keeper.OnRecv%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket)
packetAck, err := am.keeper.OnRecv%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket)
if err != nil {
acknowledgement = channeltypes.NewErrorAcknowledgement(err.Error())
ack = channeltypes.NewErrorAcknowledgement(err.Error())
} else {
// Encode packet acknowledgment
packetAckBytes, err := packetAck.Marshal()
if err != nil {
return nil, []byte{}, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
ack = channeltypes.NewResultAcknowledgement(packetAckBytes)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
Expand Down Expand Up @@ -155,21 +164,32 @@ func protoModify(opts *PacketOptions) genny.RunFn {
)
content = strings.Replace(content, PlaceholderIBCPacketProtoField, replacementField, 1)

// Add the message definition
var messageFields string
// Add the message definition for packet and acknowledgment
var packetFields string
for i, field := range opts.Fields {
messageFields += fmt.Sprintf(" %s %s = %d;\n", field.Datatype, field.Name, i+1)
packetFields += fmt.Sprintf(" %s %s = %d;\n", field.Datatype, field.Name, i+1)
}

var ackFields string
for i, field := range opts.AckFields {
ackFields += fmt.Sprintf(" %s %s = %d;\n", field.Datatype, field.Name, i+1)
}

templateMessage := `%[1]v
// %[2]vPacketData defines a struct for the packet payload
message %[2]vPacketData {
%[3]v}

// %[2]vPacketAck defines a struct for the packet acknowledgment
message %[2]vPacketAck {
%[4]v}
`
replacementMessage := fmt.Sprintf(
templateMessage,
PlaceholderIBCPacketProtoMessage,
strings.Title(opts.PacketName),
messageFields,
packetFields,
ackFields,
)
content = strings.Replace(content, PlaceholderIBCPacketProtoMessage, replacementMessage, 1)

Expand Down
F438
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"<%= modulePath %>/x/<%= moduleName %>/types"
Expand Down Expand Up @@ -65,31 +67,42 @@ func (k Keeper) Transmit<%= title(packetName) %>Packet(
}

// OnRecv<%= title(packetName) %>Packet processes packet reception
func (k Keeper) OnRecv<%= title(packetName) %>Packet(ctx sdk.Context, packet channeltypes.Packet, data types.<%= title(packetName) %>PacketData) error {
func (k Keeper) OnRecv<%= title(packetName) %>Packet(ctx sdk.Context, packet channeltypes.Packet, data types.<%= title(packetName) %>PacketData) (packetAck types.<%= title(packetName) %>PacketAck, err error) {
// validate packet data upon receiving
if err := data.ValidateBasic(); err != nil {
return err
return packetAck, err
}

// TODO: packet reception logic

return nil
return packetAck, nil
}

// OnAcknowledgement<%= title(packetName) %>Packet responds to the the success or failure of a packet
// acknowledgement written on the receiving chain.
func (k Keeper) OnAcknowledgement<%= title(packetName) %>Packet(ctx sdk.Context, packet channeltypes.Packet, data types.<%= title(packetName) %>PacketData, ack channeltypes.Acknowledgement) error {
switch ack.Response.(type) {
switch dispatchedAck := ack.Response.(type) {
case *channeltypes.Acknowledgement_Error:

// TODO: failed acknowledgement logic
_ = dispatchedAck.Error

return nil
default:
case *channeltypes.Acknowledgement_Result:
// Decode the packet acknowledgment
var packetAck types.<%= title(packetName) %>PacketAck
err := packetAck.Unmarshal(dispatchedAck.Result)
if err != nil {
// The counter-party module doesn't implement the correct acknowledgment format
return errors.New("cannot unmarshal acknowledgment")
}

// TODO: successful acknowledgement logic

return nil
default:
// The counter-party module doesn't implement the correct acknowledgment format
return errors.New("invalid acknowledgment format")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (am AppModule) OnRecvPacket(
return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error())
}

acknowledgement := channeltypes.NewResultAcknowledgement([]byte{byte(1)})
var ack channeltypes.Acknowledgement

// Dispatch packet
switch packet := modulePacketData.Packet.(type) {
Expand All @@ -151,7 +151,7 @@ func (am AppModule) OnRecvPacket(
}

// Encode acknowledgement
ackBytes, err := acknowledgement.Marshal()
ackBytes, err := ack.Marshal()
if err != nil {
return nil, []byte{}, sdkerrors.Wrap(sdkerrors.ErrInvalidType, err.Error())
}
Expand Down
0