Search Apps Documentation Source Content File Folder Download Copy Actions Download

z4c_on_recv_packet_filetest.gno

8.02 Kb · 241 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"encoding/base64"
  6	"encoding/hex"
  7	"time"
  8
  9	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
 10	"gno.land/p/aib/ibc/types"
 11	"gno.land/p/aib/ics23"
 12	"gno.land/r/aib/ibc/apps/transfer"
 13	"gno.land/r/aib/ibc/core"
 14)
 15
 16// OnRecvPacket: fails because payload is bad encoded
 17// NOTE: RecvPacket does not fail if OnRecvPacket fails.
 18func main(cur realm) {
 19	var (
 20		chainID       = "chain-id-2"
 21		trustedHeight = types.NewHeight(2, 2)
 22		clientState   = tmtesting.NewClientState(chainID, trustedHeight)
 23		// NOTE this apphash was provided by the gen-proof command below.
 24		apphash, _     = hex.DecodeString("f8deb7729dcfb65c12864c2a4a07af5a761cb28da6982fc1389600591fcb325b")
 25		trustedValset  = tmtesting.GenValset()
 26		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 27		counterpartyID = "07-tendermint-42"
 28	)
 29	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 30	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
 31
 32	// Generate the proof of existence of the packet commitment for the
 33	// counterparty chain.
 34	specs := ics23.IavlSpec()
 35	// NOTE code generated by:
 36	// go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'packet' '{"sequence":1,"source_client":"07-tendermint-42","destination_client":"07-tendermint-1","timeout_timestamp":1234571490,"payloads":[{"source_port":"transfer","destination_port":"transfer","encoding":"application/x-protobuf", "value":"d3JvbmcgdmFsdWU=","version":"ics20-1"}]}'
 37	proof := []ics23.CommitmentProof{
 38
 39		// iavl proof
 40		ics23.CommitmentProof_Exist{
 41			Exist: &ics23.ExistenceProof{
 42				Key:   []byte("\x70\x72\x65\x66\x69\x78\x32\x30\x37\x2d\x74\x65\x6e\x64\x65\x72\x6d\x69\x6e\x74\x2d\x34\x32\x01\x00\x00\x00\x00\x00\x00\x00\x01"),
 43				Value: []byte("\xbf\xcd\xaf\xa5\x5a\x79\x24\x40\xdf\x2d\x7c\x01\x32\x65\xcc\x59\x6d\x4c\x88\x14\xa1\x53\x79\xe3\xc2\x9a\xfd\x36\xa8\x6b\x0d\xbb"),
 44				Leaf: &ics23.LeafOp{
 45					Hash:         specs.LeafSpec.Hash,
 46					PrehashKey:   specs.LeafSpec.PrehashKey,
 47					PrehashValue: specs.LeafSpec.PrehashValue,
 48					Length:       specs.LeafSpec.Length,
 49					Prefix:       []byte("\x00\x02\x02"),
 50				},
 51				Path: []*ics23.InnerOp{
 52					{
 53						Hash:   specs.InnerSpec.Hash,
 54						Prefix: []byte("\x02\x04\x02\x20\x35\xf8\xea\x80\x53\x90\xe0\x84\x85\x4f\x39\x9b\x42\xcc\xde\xae\xa3\x3a\x1d\xed\xc1\x15\x63\x8a\xc4\x8d\x06\x00\x63\x7d\xba\x1f\x20"),
 55						Suffix: []byte(""),
 56					},
 57					{
 58						Hash:   specs.InnerSpec.Hash,
 59						Prefix: []byte("\x04\x08\x02\x20"),
 60						Suffix: []byte("\x20\x79\x8e\x2c\xaa\x96\xfd\xfb\xa3\x76\xdd\xeb\x47\x99\x99\x54\xd2\xf4\x7e\x65\x16\x22\x64\xb0\x53\x6a\xb5\xdf\xf7\xfc\x0a\x2e\x07"),
 61					},
 62					{
 63						Hash:   specs.InnerSpec.Hash,
 64						Prefix: []byte("\x06\x0c\x02\x20\x9a\xf3\x7d\xd5\x95\xa0\x19\x08\x03\xb5\xe0\x5a\xae\xf4\x2a\xe3\xfa\xd4\x99\xe4\xfb\xe3\x7f\x7c\xd3\x1c\xad\xff\x22\xa9\xee\x74\x20"),
 65						Suffix: []byte(""),
 66					},
 67				},
 68			},
 69		},
 70
 71		// rootmulti proof
 72		ics23.CommitmentProof_Exist{
 73			Exist: &ics23.ExistenceProof{
 74				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
 75				Value: []byte("\x25\xd3\x94\x2d\x04\x81\x40\x15\xa3\xc2\x7b\xe2\xf0\x78\x4e\xfe\xfc\xe5\x5f\x7e\xb0\x46\x17\x51\xb3\x6a\x5e\x63\x35\xec\xc4\x53"),
 76				Leaf: &ics23.LeafOp{
 77					Hash:         specs.LeafSpec.Hash,
 78					PrehashKey:   specs.LeafSpec.PrehashKey,
 79					PrehashValue: specs.LeafSpec.PrehashValue,
 80					Length:       specs.LeafSpec.Length,
 81					Prefix:       []byte("\x00"),
 82				},
 83				Path: []*ics23.InnerOp{},
 84			},
 85		},
 86	}
 87
 88	recvPacket := types.MsgRecvPacket{
 89		Packet: types.Packet{
 90			Sequence:          1,
 91			SourceClient:      counterpartyID,
 92			DestinationClient: clientID,
 93			TimeoutTimestamp:  uint64(time.Now().Add(time.Hour).Unix()),
 94			Payloads: []types.Payload{{
 95				SourcePort:      transfer.PortID,
 96				DestinationPort: transfer.PortID,
 97				Encoding:        transfer.EncodingProtobuf,
 98				Value:           []byte("wrong value"),
 99				Version:         transfer.V1,
100			}},
101		},
102		ProofCommitment: proof,
103		ProofHeight:     trustedHeight,
104	}
105
106	res := core.RecvPacket(cross(cur), recvPacket)
107
108	println("res:", res)
109	println("\n----------- assert render clients/07-tendermint-1/packet_receipts")
110	println(core.Render("clients/" + clientID + "/packet_receipts"))
111	println("\n----------- assert render clients/07-tendermint-1/packet_acknowledgements")
112	// The 2 acknowledgements below must match
113	println("Expected commitment", base64.StdEncoding.EncodeToString(
114		types.CommitAcknowledgement(types.Acknowledgement{
115			AppAcknowledgements: [][]byte{types.UniversalErrorAcknowledgement()},
116		}),
117	))
118	println(core.Render("clients/" + clientID + "/packet_acknowledgements"))
119}
120
121// Output:
122// res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
123//
124// ----------- assert render clients/07-tendermint-1/packet_receipts
125// {"items":[{"sequence":"1","data":"Ag=="}],"page":1,"total":1}
126//
127// ----------- assert render clients/07-tendermint-1/packet_acknowledgements
128// Expected commitment 4vsw3796verKgtQmU00rOp1URN0qh/oW04t3uhoTztc=
129// {"items":[{"sequence":"1","data":"4vsw3796verKgtQmU00rOp1URN0qh/oW04t3uhoTztc="}],"page":1,"total":1}
130
131// Events:
132// [
133//   {
134//     "type": "create_client",
135//     "attrs": [
136//       {
137//         "key": "client_id",
138//         "value": "07-tendermint-1"
139//       },
140//       {
141//         "key": "client_type",
142//         "value": "07-tendermint"
143//       },
144//       {
145//         "key": "consensus_heights",
146//         "value": "2/2"
147//       }
148//     ],
149//     "pkg_path": "gno.land/r/aib/ibc/core"
150//   },
151//   {
152//     "type": "recv_packet",
153//     "attrs": [
154//       {
155//         "key": "packet_source_client",
156//         "value": "07-tendermint-42"
157//       },
158//       {
159//         "key": "packet_dest_client",
160//         "value": "07-tendermint-1"
161//       },
162//       {
163//         "key": "packet_sequence",
164//         "value": "1"
165//       },
166//       {
167//         "key": "packet_timeout_timestamp",
168//         "value": "1234571490"
169//       },
170//       {
171//         "key": "encoded_packet_hex",
172//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a420a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a0b77726f6e672076616c7565"
173//       }
174//     ],
175//     "pkg_path": "gno.land/r/aib/ibc/core"
176//   },
177//   {
178//     "type": "fungible_token_packet",
179//     "attrs": [
180//       {
181//         "key": "sender",
182//         "value": ""
183//       },
184//       {
185//         "key": "receiver",
186//         "value": ""
187//       },
188//       {
189//         "key": "denom",
190//         "value": ""
191//       },
192//       {
193//         "key": "amount",
194//         "value": ""
195//       },
196//       {
197//         "key": "memo",
198//         "value": ""
199//       },
200//       {
201//         "key": "success",
202//         "value": "false"
203//       },
204//       {
205//         "key": "error",
206//         "value": "decoding FungibleTokenPacketData: unexpected wire type 7 for field 14"
207//       }
208//     ],
209//     "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
210//   },
211//   {
212//     "type": "write_acknowledgement",
213//     "attrs": [
214//       {
215//         "key": "packet_source_client",
216//         "value": "07-tendermint-42"
217//       },
218//       {
219//         "key": "packet_dest_client",
220//         "value": "07-tendermint-1"
221//       },
222//       {
223//         "key": "packet_sequence",
224//         "value": "1"
225//       },
226//       {
227//         "key": "packet_timeout_timestamp",
228//         "value": "1234571490"
229//       },
230//       {
231//         "key": "encoded_packet_hex",
232//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a420a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a0b77726f6e672076616c7565"
233//       },
234//       {
235//         "key": "encoded_acknowledgement_hex",
236//         "value": "0a204774d4a575993f963b1c06573736617a457abef8589178db8d10c94b4ab511ab"
237//       }
238//     ],
239//     "pkg_path": "gno.land/r/aib/ibc/core"
240//   }
241// ]