Search Apps Documentation Source Content File Folder Download Copy Actions Download

z2a_on_ack_packet_filetest.gno

8.49 Kb · 268 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"chain"
  6	"encoding/hex"
  7	"testing"
  8	"time"
  9
 10	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
 11	"gno.land/p/aib/ibc/types"
 12	"gno.land/p/aib/ics23"
 13	"gno.land/r/aib/ibc/apps/transfer"
 14	"gno.land/r/aib/ibc/core"
 15)
 16
 17// OnAcknowledgementPacket: success with native coins
 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("6f64efa36d97452b4555efc37596b7fc36ca80dff8a1784ca8fbf45a102e8e27")
 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	// Transfer the 100ugnot we want to ack
 33	testing.SetRealm(testing.NewUserRealm("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"))
 34	testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 100)))
 35	println("----------- assert render total_escrow/ugnot before Transfer")
 36	println(transfer.Render("total_escrow/ugnot"))
 37	sendPacket, sequence := transfer.Transfer(cross(cur), clientID, "atone1user", "ugnot", 100, uint64(time.Now().Add(time.Hour).Unix()), "memo")
 38	println("----------- assert render total_escrow/ugnot after Transfer")
 39	println(transfer.Render("total_escrow/ugnot"))
 40
 41	// Acknowledge the packet
 42	specs := ics23.IavlSpec()
 43	// Generate the proof of acknowledgement written during the RecvPacket of the
 44	// counterparty client.
 45	// NOTE code generated by:
 46	// go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'acknowledgement' '{"result":"AQ=="}'
 47	proof := []ics23.CommitmentProof{
 48
 49		// iavl proof
 50		ics23.CommitmentProof_Exist{
 51			Exist: &ics23.ExistenceProof{
 52				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\x03\x00\x00\x00\x00\x00\x00\x00\x01"),
 53				Value: []byte("\x84\x60\xe2\x1f\x73\xb5\x3d\x77\x9e\x4b\x32\x91\xcd\x35\x33\x8e\x92\xfa\xe9\x99\x87\x35\xf1\xa0\xb7\x15\x0c\x07\x4c\x07\x31\xa6"),
 54				Leaf: &ics23.LeafOp{
 55					Hash:         specs.LeafSpec.Hash,
 56					PrehashKey:   specs.LeafSpec.PrehashKey,
 57					PrehashValue: specs.LeafSpec.PrehashValue,
 58					Length:       specs.LeafSpec.Length,
 59					Prefix:       []byte("\x00\x02\x02"),
 60				},
 61				Path: []*ics23.InnerOp{
 62					{
 63						Hash:   specs.InnerSpec.Hash,
 64						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"),
 65						Suffix: []byte(""),
 66					},
 67					{
 68						Hash:   specs.InnerSpec.Hash,
 69						Prefix: []byte("\x04\x08\x02\x20"),
 70						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"),
 71					},
 72					{
 73						Hash:   specs.InnerSpec.Hash,
 74						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"),
 75						Suffix: []byte(""),
 76					},
 77				},
 78			},
 79		},
 80
 81		// rootmulti proof
 82		ics23.CommitmentProof_Exist{
 83			Exist: &ics23.ExistenceProof{
 84				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
 85				Value: []byte("\x0a\xbc\x9d\x9a\xd7\x2b\x69\x0b\x1c\xbc\xca\x2d\x8a\x35\xb0\xe5\x04\x73\x77\x6a\x1e\x28\x9b\x94\xc7\x3e\x1a\x67\x99\x56\xad\xdd"),
 86				Leaf: &ics23.LeafOp{
 87					Hash:         specs.LeafSpec.Hash,
 88					PrehashKey:   specs.LeafSpec.PrehashKey,
 89					PrehashValue: specs.LeafSpec.PrehashValue,
 90					Length:       specs.LeafSpec.Length,
 91					Prefix:       []byte("\x00"),
 92				},
 93				Path: []*ics23.InnerOp{},
 94			},
 95		},
 96	}
 97
 98	ackPacket := types.MsgAcknowledgement{
 99		Packet: types.Packet{
100			Sequence:          sequence,
101			SourceClient:      clientID,
102			DestinationClient: counterpartyID,
103			TimeoutTimestamp:  sendPacket.TimeoutTimestamp,
104			Payloads:          sendPacket.Payloads,
105		},
106		Acknowledgement: types.Acknowledgement{
107			AppAcknowledgements: [][]byte{[]byte(`{"result":"AQ=="}`)},
108		},
109		ProofAcked:  proof,
110		ProofHeight: trustedHeight,
111	}
112
113	res := core.Acknowledgement(cross(cur), ackPacket)
114
115	println("\nack res:", res)
116	println("----------- assert render total_escrow/ugnot after Ack")
117	println(transfer.Render("total_escrow/ugnot"))
118}
119
120// Output:
121// ----------- assert render total_escrow/ugnot before Transfer
122// {"denom":"ugnot","amount":0}
123// ----------- assert render total_escrow/ugnot after Transfer
124// {"denom":"ugnot","amount":100}
125//
126// ack res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
127// ----------- assert render total_escrow/ugnot after Ack
128// {"denom":"ugnot","amount":100}
129
130// Events:
131// [
132//   {
133//     "type": "create_client",
134//     "attrs": [
135//       {
136//         "key": "client_id",
137//         "value": "07-tendermint-1"
138//       },
139//       {
140//         "key": "client_type",
141//         "value": "07-tendermint"
142//       },
143//       {
144//         "key": "consensus_heights",
145//         "value": "2/2"
146//       }
147//     ],
148//     "pkg_path": "gno.land/r/aib/ibc/core"
149//   },
150//   {
151//     "type": "send_packet",
152//     "attrs": [
153//       {
154//         "key": "packet_source_client",
155//         "value": "07-tendermint-1"
156//       },
157//       {
158//         "key": "packet_dest_client",
159//         "value": "07-tendermint-42"
160//       },
161//       {
162//         "key": "packet_sequence",
163//         "value": "1"
164//       },
165//       {
166//         "key": "packet_timeout_timestamp",
167//         "value": "1234571490"
168//       },
169//       {
170//         "key": "encoded_packet_hex",
171//         "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a7f0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a480a0575676e6f7412033130301a28673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d220a61746f6e6531757365722a046d656d6f"
172//       }
173//     ],
174//     "pkg_path": "gno.land/r/aib/ibc/core"
175//   },
176//   {
177//     "type": "ibc_transfer",
178//     "attrs": [
179//       {
180//         "key": "sender",
181//         "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
182//       },
183//       {
184//         "key": "receiver",
185//         "value": "atone1user"
186//       },
187//       {
188//         "key": "denom",
189//         "value": "ugnot"
190//       },
191//       {
192//         "key": "amount",
193//         "value": "100"
194//       },
195//       {
196//         "key": "memo",
197//         "value": "memo"
198//       }
199//     ],
200//     "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
201//   },
202//   {
203//     "type": "acknowledge_packet",
204//     "attrs": [
205//       {
206//         "key": "packet_source_client",
207//         "value": "07-tendermint-1"
208//       },
209//       {
210//         "key": "packet_dest_client",
211//         "value": "07-tendermint-42"
212//       },
213//       {
214//         "key": "packet_sequence",
215//         "value": "1"
216//       },
217//       {
218//         "key": "packet_timeout_timestamp",
219//         "value": "1234571490"
220//       },
221//       {
222//         "key": "encoded_packet_hex",
223//         "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a7f0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a480a0575676e6f7412033130301a28673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d220a61746f6e6531757365722a046d656d6f"
224//       }
225//     ],
226//     "pkg_path": "gno.land/r/aib/ibc/core"
227//   },
228//   {
229//     "type": "fungible_token_packet",
230//     "attrs": [
231//       {
232//         "key": "sender",
233//         "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
234//       },
235//       {
236//         "key": "receiver",
237//         "value": "atone1user"
238//       },
239//       {
240//         "key": "denom",
241//         "value": "ugnot"
242//       },
243//       {
244//         "key": "amount",
245//         "value": "100"
246//       },
247//       {
248//         "key": "memo",
249//         "value": "memo"
250//       },
251//       {
252//         "key": "acknowledgement",
253//         "value": "{\"result\":\"AQ==\"}"
254//       }
255//     ],
256//     "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
257//   },
258//   {
259//     "type": "fungible_token_packet",
260//     "attrs": [
261//       {
262//         "key": "success",
263//         "value": "\u0001"
264//       }
265//     ],
266//     "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
267//   }
268// ]