Skip to content

Commit

Permalink
fix: no ack package in multi-message if no need
Browse files Browse the repository at this point in the history
  • Loading branch information
cosinlink committed Apr 2, 2024
1 parent 540704c commit 45938c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
35 changes: 22 additions & 13 deletions x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
)

const (
ChannelIdLength = 1
AckRelayFeeLength = 32
ChannelIdLength = 1
AckRelayFeeLength = 32
SoliditySelectorLength = 4
)

type msgServer struct {
Expand All @@ -48,6 +49,9 @@ var (

MessagesAbiDefinition = `[{ "name" : "method", "type": "function", "outputs": [{"type": "bytes[]"}]}]`
MessagesAbi, _ = abi.JSON(strings.NewReader(MessagesAbiDefinition))

AckMessagesAbiDefinition = `[{ "name" : "method", "type": "function", "inputs": [{"type": "bytes[]"}]}]`
AckMessagesAbi, _ = abi.JSON(strings.NewReader(AckMessagesAbiDefinition))
)

// NewMsgServerImpl returns an implementation of the oracle MsgServer interface
Expand Down Expand Up @@ -212,7 +216,7 @@ func (k Keeper) handleMultiMessagePackage(
}
}()

messages, err := DecodeMultiMessage(pack.Payload)
messages, err := DecodeMultiMessage(pack.Payload[sdk.SynPackageHeaderLength+sdk.PackageTypeLength:])
if err != nil {
return true, sdk.ExecuteResult{
Err: err,
Expand All @@ -221,7 +225,7 @@ func (k Keeper) handleMultiMessagePackage(

crash = false
result = sdk.ExecuteResult{}
ackMessages := make([][]byte, len(messages))
ackMessages := make([][]byte, 0)
for i, message := range messages {
channelId, msgBytes, ackRelayFee, err := DecodeMessage(message)
if err != nil {
Expand Down Expand Up @@ -250,13 +254,17 @@ func (k Keeper) handleMultiMessagePackage(
return true, resultSingleMsg
}

ackMessages[i] = EncodeAckMessage(channelId, ackRelayFee, resultSingleMsg.Payload)
if len(resultSingleMsg.Payload) != 0 {
ackMessages = append(ackMessages, EncodeAckMessage(channelId, ackRelayFee, resultSingleMsg.Payload))
}
}

result.Payload, err = EncodeMultiAckMessage(ackMessages)
if err != nil {
return true, sdk.ExecuteResult{
Err: sdkerrors.Wrapf(types.ErrInvalidMessagesResult, "messages result pack failed, payloads=%v, error=%s", ackMessages, err),
if len(ackMessages) > 0 {
result.Payload, err = EncodeMultiAckMessage(ackMessages)
if err != nil {
return true, sdk.ExecuteResult{
Err: sdkerrors.Wrapf(types.ErrInvalidMessagesResult, "messages result pack failed, payloads=%v, error=%s", ackMessages, err),
}
}
}

Expand Down Expand Up @@ -403,7 +411,7 @@ func executeClaim(
func DecodeMultiMessage(multiMessagePayload []byte) (messages [][]byte, err error) {
out, err := MessagesAbi.Unpack("method", multiMessagePayload)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidMultiMessage, "messages unpack failed, payload=%v", multiMessagePayload)
return nil, sdkerrors.Wrapf(types.ErrInvalidMultiMessage, "messages unpack failed, payload=%s", hex.EncodeToString(multiMessagePayload))
}

unpacked := abi.ConvertType(out[0], MessagesType{})
Expand Down Expand Up @@ -467,9 +475,10 @@ func EncodeAckMessage(channelId uint8, ackRelayFee *big.Int, result []byte) (ack
}

func EncodeMultiAckMessage(ackMessages [][]byte) (encoded []byte, err error) {
encoded, err = MessagesAbi.Pack("method", ackMessages)
encoded, err = AckMessagesAbi.Pack("method", ackMessages)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidMessagesResult, "messages result pack failed, payloads=%v, error=%s", ackMessages, err)
return nil, sdkerrors.Wrapf(types.ErrInvalidMessagesResult, "ack messages pack failed, payloads=%v, error=%s", ackMessages, err)
}
return encoded, nil

return encoded[SoliditySelectorLength:], nil
}
9 changes: 9 additions & 0 deletions x/oracle/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,12 @@ func (s *TestSuite) TestMultiMessageDecode() {
}
}
}

func (s *TestSuite) TestMultiAckMessageEncode() {
msg1, _ := hexutil.Decode("000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000e35fa931a00000000000000000000000000000000000000000000000000001626218b45860000000000000000000000000007fa9385be102ac3eac297483dd6233d62b3e149600000000000000000000000000000000000000000000000000000000000000e10200000000000000000000000000000000000000000000000000000000000000200000000000000000000000007fa9385be102ac3eac297483dd6233d62b3e1496000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000057465737431000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
msg2, _ := hexutil.Decode("000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000e35fa931a00000000000000000000000000000000000000000000000000001626218b45860000000000000000000000000007fa9385be102ac3eac297483dd6233d62b3e149600000000000000000000000000000000000000000000000000000000000000e10200000000000000000000000000000000000000000000000000000000000000200000000000000000000000007fa9385be102ac3eac297483dd6233d62b3e1496000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000057465737432000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
data := [][]byte{msg1, msg2}

_, err := keeper.EncodeMultiAckMessage(data)
s.Require().Nil(err, "EncodeMultiAckMessage error")
}

0 comments on commit 45938c7

Please sign in to comment.