Search Apps Documentation Source Content File Folder Download Copy Actions Download

z4d_on_recv_packet_filetest.gno

8.07 Kb · 215 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/p/nt/testutils/v0"
 13	"gno.land/r/aib/ibc/apps/transfer"
 14	"gno.land/r/aib/ibc/core"
 15)
 16
 17// OnRecvPacket: fails because ports mismatch
 18// NOTE: RecvPacket does not fail if OnRecvPacket fails.
 19func main(cur realm) {
 20	var (
 21		chainID       = "chain-id-2"
 22		trustedHeight = types.NewHeight(2, 2)
 23		clientState   = tmtesting.NewClientState(chainID, trustedHeight)
 24		// NOTE this apphash was provided by the gen-proof command below.
 25		apphash, _     = hex.DecodeString("76da827f6454237bacbbf9787813f07b688c912e2c838ef65062451795769541")
 26		trustedValset  = tmtesting.GenValset()
 27		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 28		counterpartyID = "07-tendermint-42"
 29	)
 30	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 31	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
 32
 33	receiver := testutils.TestAddress("receiver")
 34	payload := transfer.NewFungibleTokenPacketData("uphoton", "100", "atone1user", receiver.String(), "")
 35	payloadBz := payload.ProtoMarshal()
 36	// NOTE this base64 value is used in payload.value in the gen-proof command below.
 37	println("Payload proto:", base64.StdEncoding.EncodeToString(payloadBz))
 38
 39	// Generate the proof of existence of the packet commitment for the
 40	// counterparty chain.
 41	specs := ics23.IavlSpec()
 42	// NOTE code generated by:
 43	// 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":"other-port","destination_port":"transfer","encoding":"application/x-protobuf", "value":"Cgd1cGhvdG9uEgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw=","version":"ics20-1"}]}'
 44	proof := []ics23.CommitmentProof{
 45
 46		// iavl proof
 47		ics23.CommitmentProof_Exist{
 48			Exist: &ics23.ExistenceProof{
 49				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"),
 50				Value: []byte("\xc0\xaf\x37\xa2\xd7\x94\x30\x4c\x5e\x72\x20\xf1\x5b\xae\xf0\x46\x4e\xfb\x5f\x5a\x69\x86\xdf\xa4\x2b\x4c\xbb\x25\xb3\x9a\x87\xa0"),
 51				Leaf: &ics23.LeafOp{
 52					Hash:         specs.LeafSpec.Hash,
 53					PrehashKey:   specs.LeafSpec.PrehashKey,
 54					PrehashValue: specs.LeafSpec.PrehashValue,
 55					Length:       specs.LeafSpec.Length,
 56					Prefix:       []byte("\x00\x02\x02"),
 57				},
 58				Path: []*ics23.InnerOp{
 59					{
 60						Hash:   specs.InnerSpec.Hash,
 61						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"),
 62						Suffix: []byte(""),
 63					},
 64					{
 65						Hash:   specs.InnerSpec.Hash,
 66						Prefix: []byte("\x04\x08\x02\x20"),
 67						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"),
 68					},
 69					{
 70						Hash:   specs.InnerSpec.Hash,
 71						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"),
 72						Suffix: []byte(""),
 73					},
 74				},
 75			},
 76		},
 77
 78		// rootmulti proof
 79		ics23.CommitmentProof_Exist{
 80			Exist: &ics23.ExistenceProof{
 81				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
 82				Value: []byte("\x4e\xc0\xe1\xbf\x09\x79\xac\x2d\x45\x23\x34\x99\xd8\xb0\x94\x6f\x51\xd2\xbc\xcd\xc5\xb3\x75\x70\x22\x4f\xac\x7c\x02\x3b\x61\xc4"),
 83				Leaf: &ics23.LeafOp{
 84					Hash:         specs.LeafSpec.Hash,
 85					PrehashKey:   specs.LeafSpec.PrehashKey,
 86					PrehashValue: specs.LeafSpec.PrehashValue,
 87					Length:       specs.LeafSpec.Length,
 88					Prefix:       []byte("\x00"),
 89				},
 90				Path: []*ics23.InnerOp{},
 91			},
 92		},
 93	}
 94
 95	recvPacket := types.MsgRecvPacket{
 96		Packet: types.Packet{
 97			Sequence:          1,
 98			SourceClient:      counterpartyID,
 99			DestinationClient: clientID,
100			TimeoutTimestamp:  uint64(time.Now().Add(time.Hour).Unix()),
101			Payloads: []types.Payload{{
102				SourcePort:      "other-port",
103				DestinationPort: transfer.PortID,
104				Encoding:        transfer.EncodingProtobuf,
105				Value:           payloadBz,
106				Version:         transfer.V1,
107			}},
108		},
109		ProofCommitment: proof,
110		ProofHeight:     trustedHeight,
111	}
112
113	res := core.RecvPacket(cross(cur), recvPacket)
114
115	println("res:", res)
116	println("\n----------- assert render clients/07-tendermint-1/packet_receipts")
117	println(core.Render("clients/" + clientID + "/packet_receipts"))
118	println("\n----------- assert render clients/07-tendermint-1/packet_acknowledgements")
119	// The 2 acknowledgements below must match
120	println("Expected commitment", base64.StdEncoding.EncodeToString(
121		types.CommitAcknowledgement(types.Acknowledgement{
122			AppAcknowledgements: [][]byte{types.UniversalErrorAcknowledgement()},
123		}),
124	))
125	println(core.Render("clients/" + clientID + "/packet_acknowledgements"))
126}
127
128// Output:
129// Payload proto: Cgd1cGhvdG9uEgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw=
130// res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
131//
132// ----------- assert render clients/07-tendermint-1/packet_receipts
133// {"items":[{"sequence":"1","data":"Ag=="}],"page":1,"total":1}
134//
135// ----------- assert render clients/07-tendermint-1/packet_acknowledgements
136// Expected commitment 4vsw3796verKgtQmU00rOp1URN0qh/oW04t3uhoTztc=
137// {"items":[{"sequence":"1","data":"4vsw3796verKgtQmU00rOp1URN0qh/oW04t3uhoTztc="}],"page":1,"total":1}
138
139// Events:
140// [
141//   {
142//     "type": "create_client",
143//     "attrs": [
144//       {
145//         "key": "client_id",
146//         "value": "07-tendermint-1"
147//       },
148//       {
149//         "key": "client_type",
150//         "value": "07-tendermint"
151//       },
152//       {
153//         "key": "consensus_heights",
154//         "value": "2/2"
155//       }
156//     ],
157//     "pkg_path": "gno.land/r/aib/ibc/core"
158//   },
159//   {
160//     "type": "recv_packet",
161//     "attrs": [
162//       {
163//         "key": "packet_source_client",
164//         "value": "07-tendermint-42"
165//       },
166//       {
167//         "key": "packet_dest_client",
168//         "value": "07-tendermint-1"
169//       },
170//       {
171//         "key": "packet_sequence",
172//         "value": "1"
173//       },
174//       {
175//         "key": "packet_timeout_timestamp",
176//         "value": "1234571490"
177//       },
178//       {
179//         "key": "encoded_packet_hex",
180//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7d0a0a6f746865722d706f727412087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
181//       }
182//     ],
183//     "pkg_path": "gno.land/r/aib/ibc/core"
184//   },
185//   {
186//     "type": "write_acknowledgement",
187//     "attrs": [
188//       {
189//         "key": "packet_source_client",
190//         "value": "07-tendermint-42"
191//       },
192//       {
193//         "key": "packet_dest_client",
194//         "value": "07-tendermint-1"
195//       },
196//       {
197//         "key": "packet_sequence",
198//         "value": "1"
199//       },
200//       {
201//         "key": "packet_timeout_timestamp",
202//         "value": "1234571490"
203//       },
204//       {
205//         "key": "encoded_packet_hex",
206//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7d0a0a6f746865722d706f727412087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
207//       },
208//       {
209//         "key": "encoded_acknowledgement_hex",
210//         "value": "0a204774d4a575993f963b1c06573736617a457abef8589178db8d10c94b4ab511ab"
211//       }
212//     ],
213//     "pkg_path": "gno.land/r/aib/ibc/core"
214//   }
215// ]