Skip to content

Commit

Permalink
more refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpointer0x00 committed Oct 6, 2023
1 parent 175712e commit 3d666ce
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ func New(
app.IBCHooksKeeper.ContractKeeper = app.ContractKeeper
app.Ics20MarkerHooks.MarkerKeeper = &app.MarkerKeeper

app.IbcHooks.PreSendPacketDataProcessingFns = []ibchookstypes.PreSendPacketDataProcessingFn{app.Ics20MarkerHooks.ProcessMarkerMemoFn, app.Ics20WasmHooks.GetPreSendPacketDataProcessingFns}
app.IbcHooks.SendPacketPreProcessors = []ibchookstypes.PreSendPacketDataProcessingFn{app.Ics20MarkerHooks.ProcessMarkerMemoFn, app.Ics20WasmHooks.GetWasmSendPacketPreProcessor}

app.ScopedOracleKeeper = scopedOracleKeeper
app.OracleKeeper = *oraclekeeper.NewKeeper(
Expand Down
6 changes: 3 additions & 3 deletions x/ibchooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

type Hooks interface{}

// GetPreSendPacketDataProcessingFns returns a list of ordered functions to be executed before ibc's SendPacket function in middleware
type GetPreSendPacketDataProcessingFns interface {
GetPreSendPacketDataProcessingFns() []types.PreSendPacketDataProcessingFn
// SendPacketPreProcessors returns a list of ordered functions to be executed before ibc's SendPacket function in middleware
type SendPacketPreProcessors interface {
GetSendPacketPreProcessors() []types.PreSendPacketDataProcessingFn
}

type OnChanOpenInitOverrideHooks interface {
Expand Down
32 changes: 17 additions & 15 deletions x/ibchooks/ibc_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ import (
)

type IbcHooks struct {
cdc codec.BinaryCodec
ibcKeeper *ibckeeper.Keeper
ibcHooksKeeper *keeper.Keeper
wasmHooks *WasmHooks
markerHooks *MarkerHooks
PreSendPacketDataProcessingFns []types.PreSendPacketDataProcessingFn
cdc codec.BinaryCodec
ibcKeeper *ibckeeper.Keeper
ibcHooksKeeper *keeper.Keeper
wasmHooks *WasmHooks
markerHooks *MarkerHooks
SendPacketPreProcessors []types.PreSendPacketDataProcessingFn
}

func NewIbcHooks(cdc codec.BinaryCodec, ibcHooksKeeper *keeper.Keeper, ibcKeeper *ibckeeper.Keeper, wasmHooks *WasmHooks, markerHooks *MarkerHooks, preSendPacketDataProcessingFns []types.PreSendPacketDataProcessingFn) IbcHooks {
return IbcHooks{
cdc: cdc,
ibcKeeper: ibcKeeper,
ibcHooksKeeper: ibcHooksKeeper,
wasmHooks: wasmHooks,
markerHooks: markerHooks,
PreSendPacketDataProcessingFns: preSendPacketDataProcessingFns,
cdc: cdc,
ibcKeeper: ibcKeeper,
ibcHooksKeeper: ibcHooksKeeper,
wasmHooks: wasmHooks,
markerHooks: markerHooks,
SendPacketPreProcessors: preSendPacketDataProcessingFns,
}
}

Expand All @@ -38,9 +38,9 @@ func (h IbcHooks) ProperlyConfigured() bool {
return h.wasmHooks.ProperlyConfigured() && h.markerHooks.ProperlyConfigured() && h.ibcHooksKeeper != nil
}

// GetPreSendPacketDataProcessingFns returns a list of ordered functions to be executed before ibc's SendPacket function in middleware
func (h IbcHooks) GetPreSendPacketDataProcessingFns() []types.PreSendPacketDataProcessingFn {
return h.PreSendPacketDataProcessingFns
// GetSendPacketPreProcessors returns a list of ordered functions to be executed before ibc's SendPacket function in middleware
func (h IbcHooks) GetSendPacketPreProcessors() []types.PreSendPacketDataProcessingFn {
return h.SendPacketPreProcessors
}

// OnRecvPacketOverride executes wasm or marker hooks for Ics20 packets, if not ics20 packet it will continue to process packet with no override
Expand Down Expand Up @@ -76,10 +76,12 @@ func (h IbcHooks) SendPacketAfterHook(ctx sdktypes.Context,
h.wasmHooks.SendPacketAfterHook(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data, sequence, err, processData)
}

// OnTimeoutPacketOverride returns impl of wasm hook for OnTimeoutPacketOverride
func (h IbcHooks) OnTimeoutPacketOverride(im IBCMiddleware, ctx sdktypes.Context, packet channeltypes.Packet, relayer sdktypes.AccAddress) error {
return h.wasmHooks.OnTimeoutPacketOverride(im, ctx, packet, relayer)

Check warning on line 81 in x/ibchooks/ibc_hooks.go

View check run for this annotation

Codecov / codecov/patch

x/ibchooks/ibc_hooks.go#L80-L81

Added lines #L80 - L81 were not covered by tests
}

// OnAcknowledgementPacketOverride returns impl of wasm OnAcknowledgementPacketOverride
func (h IbcHooks) OnAcknowledgementPacketOverride(im IBCMiddleware, ctx sdktypes.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdktypes.AccAddress) error {
return h.wasmHooks.OnAcknowledgementPacketOverride(im, ctx, packet, acknowledgement, relayer)
}
4 changes: 2 additions & 2 deletions x/ibchooks/ics4_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (i ICS4Middleware) SendPacket(
}

processingStateData := make(map[string]interface{})
if hook, ok := i.Hooks.(GetPreSendPacketDataProcessingFns); ok {
for _, packetDataProcessingFn := range hook.GetPreSendPacketDataProcessingFns() {
if hook, ok := i.Hooks.(SendPacketPreProcessors); ok {
for _, packetDataProcessingFn := range hook.GetSendPacketPreProcessors() {
data, err = packetDataProcessingFn(ctx, data, processingStateData)
if err != nil {
return 0, err
Expand Down
6 changes: 3 additions & 3 deletions x/ibchooks/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (k Keeper) EmitIBCAck(ctx sdk.Context, sender, channel string, packetSequen
newAck = channeltypes.NewResultAcknowledgement(jsonAck)
case "ack_error":
packet = ack.AckError.Packet
newAck = NewSuccessAckRepresentingAnError(ctx, types.ErrAckFromContract, []byte(ack.AckError.ErrorResponse), ack.AckError.ErrorDescription)
newAck = NewSuccessAckError(ctx, types.ErrAckFromContract, []byte(ack.AckError.ErrorResponse), ack.AckError.ErrorDescription)
default:
return nil, sdkerrors.Wrap(err, "could not unmarshal into IBCAckResponse or IBCAckError")
}
Expand Down Expand Up @@ -274,10 +274,10 @@ func hashPacket(packet channeltypes.Packet) (string, error) {
return hex.EncodeToString(packetHash), nil
}

// NewSuccessAckRepresentingAnError creates a new success acknowledgement that represents an error.
// NewSuccessAckError creates a new success acknowledgement that represents an error.
// This is useful for notifying the sender that an error has occurred in a way that does not allow
// the received tokens to be reverted (which means they shouldn't be released by the sender's ics20 escrow)
func NewSuccessAckRepresentingAnError(ctx sdk.Context, err error, errorContent []byte, errorContexts ...string) channeltypes.Acknowledgement {
func NewSuccessAckError(ctx sdk.Context, err error, errorContent []byte, errorContexts ...string) channeltypes.Acknowledgement {
logger := ctx.Logger().With("module", "ibc-acknowledgement-error")

attributes := make([]sdk.Attribute, len(errorContexts)+1)
Expand Down
10 changes: 10 additions & 0 deletions x/ibchooks/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type IBCAckError struct {
ErrorResponse string `json:"error_response"`
}

// IBCAck is the parent IBC ack response structure
type IBCAck struct {
Type string `json:"type"`
Content json.RawMessage `json:"content"`
Expand All @@ -63,6 +64,7 @@ type IBCAck struct {
AckError *IBCAckError `json:"error,omitempty"`
}

// UnmarshalIBCAck unmashals Ack to either response or error type
func UnmarshalIBCAck(bz []byte) (*IBCAck, error) {
var ack IBCAck
if err := json.Unmarshal(bz, &ack); err != nil {
Expand All @@ -85,38 +87,46 @@ func UnmarshalIBCAck(bz []byte) (*IBCAck, error) {
return &ack, nil
}

// IbcAck ibc ack struct with json fields defined
type IbcAck struct {
Channel string `json:"channel"`
Sequence uint64 `json:"sequence"`
Ack JSONBytes `json:"ack"`
Success bool `json:"success"`
}

// IbcLifecycleCompleteAck ibc lifcycle complete ack with json fields defined
type IbcLifecycleCompleteAck struct {
IbcAck IbcAck `json:"ibc_ack"`
}

// IbcTimeout ibc timeout struct with json fields defined
type IbcTimeout struct {
Channel string `json:"channel"`
Sequence uint64 `json:"sequence"`
}

// IbcLifecycleCompleteTimeout ibc lifecycle complete struct with json fields defined
type IbcLifecycleCompleteTimeout struct {
IbcTimeout IbcTimeout `json:"ibc_timeout"`
}

// IbcLifecycleComplete ibc lifecycle complete struct with json fields defined
type IbcLifecycleComplete struct {
IbcLifecycleComplete interface{} `json:"ibc_lifecycle_complete"`
}

// MarkerMemo parent marker struct for memo json
type MarkerMemo struct {
Marker MarkerPayload `json:"marker"`
}

// MarkerPayload child structure for marker memo
type MarkerPayload struct {
TransferAuths []string `json:"transfer-auths"`
}

// NewMarkerPayload returns a marker payload with transfer authorities
func NewMarkerPayload(transferAuthAddrs []sdk.AccAddress) MarkerPayload {
addresses := make([]string, len(transferAuthAddrs))
for i := 0; i < len(transferAuthAddrs); i++ {
Expand Down
13 changes: 6 additions & 7 deletions x/ibchooks/wasm_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (h WasmHooks) SendPacketOverride(
return seq, nil
}

func (h WasmHooks) GetPreSendPacketDataProcessingFns(
func (h WasmHooks) GetWasmSendPacketPreProcessor(
_ sdktypes.Context,
data []byte,
processData map[string]interface{},
Expand Down Expand Up @@ -513,25 +513,24 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string {
panic("unable to unmarshal ICS20 packet data")

Check warning on line 513 in x/ibchooks/wasm_hook.go

View check run for this annotation

Codecov / codecov/patch

x/ibchooks/wasm_hook.go#L513

Added line #L513 was not covered by tests
}

var denom string
if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom) {
// remove prefix added by sender chain
voucherPrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel())

unprefixedDenom := data.Denom[len(voucherPrefix):]

// coin denomination used in sending from the escrow address
denom = unprefixedDenom
denom := unprefixedDenom

// The denomination used to send the coins is either the native denom or the hash of the path
// if the denomination is not native.
denomTrace := transfertypes.ParseDenomTrace(unprefixedDenom)
if denomTrace.Path != "" {
denom = denomTrace.IBCDenom()
}

Check warning on line 530 in x/ibchooks/wasm_hook.go

View check run for this annotation

Codecov / codecov/patch

x/ibchooks/wasm_hook.go#L529-L530

Added lines #L529 - L530 were not covered by tests
} else {
prefixedDenom := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) + data.Denom
denom = transfertypes.ParseDenomTrace(prefixedDenom).IBCDenom()
return denom
}
return denom

prefixedDenom := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) + data.Denom
return transfertypes.ParseDenomTrace(prefixedDenom).IBCDenom()
}

0 comments on commit 3d666ce

Please sign in to comment.