Search Apps Documentation Source Content File Folder Download Copy Actions Download

z4aa_on_recv_packet_filetest.gno

9.04 Kb · 256 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"chain"
  6	"encoding/base64"
  7	"encoding/hex"
  8	"testing"
  9	"time"
 10
 11	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
 12	"gno.land/p/aib/ibc/types"
 13	"gno.land/p/aib/ics23"
 14	"gno.land/p/nt/testutils/v0"
 15	"gno.land/r/aib/ibc/apps/transfer"
 16	"gno.land/r/aib/ibc/core"
 17)
 18
 19// OnRecvPacket: success w/ IBC token
 20func main(cur realm) {
 21	var (
 22		chainID       = "chain-id-2"
 23		trustedHeight = types.NewHeight(2, 2)
 24		clientState   = tmtesting.NewClientState(chainID, trustedHeight)
 25		// NOTE this apphash was provided by the gen-proof command below.
 26		apphash, _     = hex.DecodeString("efb8069a70149ec2985f044957bcb3a9aefa81122db1c3dbc286f6f4c6709821")
 27		trustedValset  = tmtesting.GenValset()
 28		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 29		counterpartyID = "07-tendermint-42"
 30	)
 31	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 32	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
 33
 34	// The transfer app must have 100ugnot escrowed for this test to work
 35	// NOTE: The setup has only send 100ugnot to the transfer app, but these
 36	// tokens are not stored as escrowed like they should be after a real
 37	// transfer. As a result the escrowed ugnot will be negative at the end of
 38	// the test.
 39	transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer")
 40	testing.IssueCoins(transferAppAddr, chain.NewCoins(chain.NewCoin("ugnot", 100)))
 41
 42	receiver := testutils.TestAddress("receiver")
 43	payload := transfer.NewFungibleTokenPacketData(
 44		"transfer/"+counterpartyID+"/ugnot", // expect transfer/07-tendermint-42/ugnot
 45		"100",
 46		"atone1user",
 47		receiver.String(),
 48		"",
 49	)
 50	payloadBz := payload.ProtoMarshal()
 51	// NOTE this base64 value is used in payload.value in the gen-proof command below.
 52	println("Payload proto:", base64.StdEncoding.EncodeToString(payloadBz))
 53
 54	// Generate the proof of existence of the packet commitment for the
 55	// counterparty chain.
 56	specs := ics23.IavlSpec()
 57	// NOTE code generated by:
 58	// 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":"Ch90cmFuc2Zlci8wNy10ZW5kZXJtaW50LTQyL3Vnbm90EgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw=","version":"ics20-1"}]}'
 59	proof := []ics23.CommitmentProof{
 60
 61		// iavl proof
 62		ics23.CommitmentProof_Exist{
 63			Exist: &ics23.ExistenceProof{
 64				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"),
 65				Value: []byte("\x0d\x99\x21\xf7\xdb\xea\xfe\x29\x2d\x78\x15\xd3\x20\x61\x12\xbf\x5f\xce\x28\x0d\x26\x23\x62\xb6\xfb\xe0\x4f\x9b\x28\x24\x92\xf7"),
 66				Leaf: &ics23.LeafOp{
 67					Hash:         specs.LeafSpec.Hash,
 68					PrehashKey:   specs.LeafSpec.PrehashKey,
 69					PrehashValue: specs.LeafSpec.PrehashValue,
 70					Length:       specs.LeafSpec.Length,
 71					Prefix:       []byte("\x00\x02\x02"),
 72				},
 73				Path: []*ics23.InnerOp{
 74					{
 75						Hash:   specs.InnerSpec.Hash,
 76						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"),
 77						Suffix: []byte(""),
 78					},
 79					{
 80						Hash:   specs.InnerSpec.Hash,
 81						Prefix: []byte("\x04\x08\x02\x20"),
 82						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"),
 83					},
 84					{
 85						Hash:   specs.InnerSpec.Hash,
 86						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"),
 87						Suffix: []byte(""),
 88					},
 89				},
 90			},
 91		},
 92
 93		// rootmulti proof
 94		ics23.CommitmentProof_Exist{
 95			Exist: &ics23.ExistenceProof{
 96				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
 97				Value: []byte("\xdc\x5b\xb5\x1b\x61\xfe\xee\x3e\x93\x07\x16\x1a\xf3\xa3\x89\xd6\x6a\x3c\xf1\x44\x1f\xef\x41\x85\xd1\xb0\x47\x55\xa5\x61\x19\xa6"),
 98				Leaf: &ics23.LeafOp{
 99					Hash:         specs.LeafSpec.Hash,
100					PrehashKey:   specs.LeafSpec.PrehashKey,
101					PrehashValue: specs.LeafSpec.PrehashValue,
102					Length:       specs.LeafSpec.Length,
103					Prefix:       []byte("\x00"),
104				},
105				Path: []*ics23.InnerOp{},
106			},
107		},
108	}
109
110	recvPacket := types.MsgRecvPacket{
111		Packet: types.Packet{
112			Sequence:          1,
113			SourceClient:      counterpartyID,
114			DestinationClient: clientID,
115			TimeoutTimestamp:  uint64(time.Now().Add(time.Hour).Unix()),
116			Payloads: []types.Payload{{
117				SourcePort:      transfer.PortID,
118				DestinationPort: transfer.PortID,
119				Encoding:        transfer.EncodingProtobuf,
120				Value:           payloadBz,
121				Version:         transfer.V1,
122			}},
123		},
124		ProofCommitment: proof,
125		ProofHeight:     trustedHeight,
126	}
127	// NOTE the following will show 0ugnot since there was no previous escrowed
128	// ugnot before that test.
129	println("----------- assert render total_escrow/ugnot before RecvPacket")
130	println(transfer.Render("total_escrow/ugnot"))
131
132	res := core.RecvPacket(cross(cur), recvPacket)
133
134	println("\nack res:", res)
135	// NOTE the following will show -100ugnot since there was no previous
136	// escrowed ugnot before that test.
137	println("----------- assert render total_escrow/ugnot after RecvPacket")
138	println(transfer.Render("total_escrow/ugnot"))
139}
140
141// Output:
142// Payload proto: Ch90cmFuc2Zlci8wNy10ZW5kZXJtaW50LTQyL3Vnbm90EgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw=
143// ----------- assert render total_escrow/ugnot before RecvPacket
144// {"denom":"ugnot","amount":0}
145//
146// ack res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
147// ----------- assert render total_escrow/ugnot after RecvPacket
148// {"denom":"ugnot","amount":-100}
149
150// Events:
151// [
152//   {
153//     "type": "create_client",
154//     "attrs": [
155//       {
156//         "key": "client_id",
157//         "value": "07-tendermint-1"
158//       },
159//       {
160//         "key": "client_type",
161//         "value": "07-tendermint"
162//       },
163//       {
164//         "key": "consensus_heights",
165//         "value": "2/2"
166//       }
167//     ],
168//     "pkg_path": "gno.land/r/aib/ibc/core"
169//   },
170//   {
171//     "type": "recv_packet",
172//     "attrs": [
173//       {
174//         "key": "packet_source_client",
175//         "value": "07-tendermint-42"
176//       },
177//       {
178//         "key": "packet_dest_client",
179//         "value": "07-tendermint-1"
180//       },
181//       {
182//         "key": "packet_sequence",
183//         "value": "1"
184//       },
185//       {
186//         "key": "packet_timeout_timestamp",
187//         "value": "1234571490"
188//       },
189//       {
190//         "key": "encoded_packet_hex",
191//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a93010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a5c0a1f7472616e736665722f30372d74656e6465726d696e742d34322f75676e6f7412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
192//       }
193//     ],
194//     "pkg_path": "gno.land/r/aib/ibc/core"
195//   },
196//   {
197//     "type": "fungible_token_packet",
198//     "attrs": [
199//       {
200//         "key": "sender",
201//         "value": "atone1user"
202//       },
203//       {
204//         "key": "receiver",
205//         "value": "g1wfjkxetfwejhyh6lta047h6lta047h6llm82yl"
206//       },
207//       {
208//         "key": "denom",
209//         "value": "ugnot"
210//       },
211//       {
212//         "key": "amount",
213//         "value": "100"
214//       },
215//       {
216//         "key": "memo",
217//         "value": ""
218//       },
219//       {
220//         "key": "success",
221//         "value": "true"
222//       }
223//     ],
224//     "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
225//   },
226//   {
227//     "type": "write_acknowledgement",
228//     "attrs": [
229//       {
230//         "key": "packet_source_client",
231//         "value": "07-tendermint-42"
232//       },
233//       {
234//         "key": "packet_dest_client",
235//         "value": "07-tendermint-1"
236//       },
237//       {
238//         "key": "packet_sequence",
239//         "value": "1"
240//       },
241//       {
242//         "key": "packet_timeout_timestamp",
243//         "value": "1234571490"
244//       },
245//       {
246//         "key": "encoded_packet_hex",
247//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a93010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a5c0a1f7472616e736665722f30372d74656e6465726d696e742d34322f75676e6f7412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
248//       },
249//       {
250//         "key": "encoded_acknowledgement_hex",
251//         "value": "0a117b22726573756c74223a2241513d3d227d"
252//       }
253//     ],
254//     "pkg_path": "gno.land/r/aib/ibc/core"
255//   }
256// ]