Search Apps Documentation Source Content File Folder Download Copy Actions Download

z5a_acknowledgement_filetest.gno

8.36 Kb · 274 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"encoding/hex"
  6	"time"
  7
  8	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
  9	"gno.land/p/aib/ibc/types"
 10	"gno.land/p/aib/ics23"
 11	appstesting "gno.land/r/aib/ibc/apps/testing"
 12	"gno.land/r/aib/ibc/core"
 13)
 14
 15// Acknowledgement success
 16func main(cur realm) {
 17	var (
 18		chainID        = "chain-id-2"
 19		trustedHeight  = types.NewHeight(2, 2)
 20		clientState    = tmtesting.NewClientState(chainID, trustedHeight)
 21		apphash, _     = hex.DecodeString("a119217258843545bc79dd7df4c050c6c2a21b1de7c132dd065fd7463f743e2d")
 22		trustedValset  = tmtesting.GenValset()
 23		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 24		counterpartyID = "07-tendermint-42"
 25	)
 26	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 27	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
 28	// Register app
 29	var (
 30		app       = appstesting.NewApp(cross(cur))
 31		appPortID = "app"
 32	)
 33	core.RegisterApp(cross(cur), appPortID, app)
 34	// Send a packet
 35	sendPacket := types.MsgSendPacket{
 36		SourceClient:     clientID,
 37		TimeoutTimestamp: uint64(time.Now().Add(time.Hour).Unix()),
 38		Payloads: []types.Payload{{
 39			SourcePort:      appPortID,
 40			DestinationPort: "destinationPort",
 41			Encoding:        "application/json",
 42			Value:           []byte("{}"),
 43			Version:         "v1",
 44		}},
 45	}
 46
 47	sequence := core.SendPacket(cross(cur), sendPacket)
 48
 49	// Acknowledge the packet
 50	specs := ics23.IavlSpec()
 51	// Generate the proof of acknowledgement written during the RecvPacket of the
 52	// counterparty client.
 53	// NOTE proof generated by ./cmd/gen-proof 'prefix2' '07-tendermint-42' 'acknowledgement' '{"response":{"result":"BQ=="}}'
 54	proofAcked := []ics23.CommitmentProof{
 55
 56		// iavl proof
 57		ics23.CommitmentProof_Exist{
 58			Exist: &ics23.ExistenceProof{
 59				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"),
 60				Value: []byte("\x21\x29\x3f\x61\xea\x42\x91\xdc\x31\x88\xaa\x8b\xe6\xde\x66\xeb\x0e\xe0\x5b\x10\x03\x25\xb0\x31\x3b\x25\x84\xf2\xc0\x96\x11\xaf"),
 61				Leaf: &ics23.LeafOp{
 62					Hash:         specs.LeafSpec.Hash,
 63					PrehashKey:   specs.LeafSpec.PrehashKey,
 64					PrehashValue: specs.LeafSpec.PrehashValue,
 65					Length:       specs.LeafSpec.Length,
 66					Prefix:       []byte("\x00\x02\x02"),
 67				},
 68				Path: []*ics23.InnerOp{
 69					{
 70						Hash:   specs.InnerSpec.Hash,
 71						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"),
 72						Suffix: []byte(""),
 73					},
 74					{
 75						Hash:   specs.InnerSpec.Hash,
 76						Prefix: []byte("\x04\x08\x02\x20"),
 77						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"),
 78					},
 79					{
 80						Hash:   specs.InnerSpec.Hash,
 81						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"),
 82						Suffix: []byte(""),
 83					},
 84				},
 85			},
 86		},
 87
 88		// rootmulti proof
 89		ics23.CommitmentProof_Exist{
 90			Exist: &ics23.ExistenceProof{
 91				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
 92				Value: []byte("\x54\x67\x6b\x16\xcf\xa5\x41\x3e\x30\x2b\x0d\xff\x76\x7d\x1d\xa8\x11\x44\x0b\x38\x36\x08\xc7\xd8\xab\x6a\x1b\x36\x47\x82\x1f\x1d"),
 93				Leaf: &ics23.LeafOp{
 94					Hash:         specs.LeafSpec.Hash,
 95					PrehashKey:   specs.LeafSpec.PrehashKey,
 96					PrehashValue: specs.LeafSpec.PrehashValue,
 97					Length:       specs.LeafSpec.Length,
 98					Prefix:       []byte("\x00"),
 99				},
100				Path: []*ics23.InnerOp{},
101			},
102		},
103	}
104
105	ackPacket := types.MsgAcknowledgement{
106		Packet: types.Packet{
107			Sequence:          sequence,
108			SourceClient:      clientID,
109			DestinationClient: counterpartyID,
110			TimeoutTimestamp:  sendPacket.TimeoutTimestamp,
111			Payloads:          sendPacket.Payloads,
112		},
113		Acknowledgement: types.Acknowledgement{
114			AppAcknowledgements: [][]byte{[]byte(`{"response":{"result":"BQ=="}}`)},
115		},
116		ProofAcked:  proofAcked,
117		ProofHeight: trustedHeight,
118	}
119
120	res := core.Acknowledgement(cross(cur), ackPacket)
121
122	println("res:", res)
123	println("\n----------- assert render clients/07-tendermint-1/packet_commitments")
124	println(core.Render("clients/" + clientID + "/packet_commitments"))
125	println("----------- assert render clients/07-tendermint-1/packet_commitments/1 (cleared)")
126	println(core.Render("clients/" + clientID + "/packet_commitments/1"))
127	println("\n----------- app report")
128	println(app.Report())
129
130	// assert NOOP response (res=1) if ack is resubmitted
131	res = core.Acknowledgement(cross(cur), ackPacket)
132
133	println("res:", res)
134	println("\n----------- app report")
135	println(app.Report())
136}
137
138// Output:
139// res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
140//
141// ----------- assert render clients/07-tendermint-1/packet_commitments
142// {"items":[],"page":1,"total":1}
143// ----------- assert render clients/07-tendermint-1/packet_commitments/1 (cleared)
144// {"error":"sequence 1 not found"}
145//
146// ----------- app report
147// OnSendPacket (1)
148// - sourceClient: 07-tendermint-1
149// - destinationClient: 07-tendermint-42
150// - sequence: 1
151// - payload:
152//   - sourcePort: app
153//   - destinationPort: destinationPort
154//   - version: v1
155//   - encoding: application/json
156//   - value: {}
157//
158// OnRecvPacket (0)
159// OnTimeoutPacket (0)
160// OnAcknowledgementPacket (1)
161// - sourceClient: 07-tendermint-1
162// - destinationClient: 07-tendermint-42
163// - sequence: 1
164// - payload:
165//   - sourcePort: app
166//   - destinationPort: destinationPort
167//   - version: v1
168//   - encoding: application/json
169//   - value: {}
170// - ack: {"response":{"result":"BQ=="}}
171//
172//
173// res: (1 gno.land/p/aib/ibc/types.ResponseResultType)
174//
175// ----------- app report
176// OnSendPacket (1)
177// - sourceClient: 07-tendermint-1
178// - destinationClient: 07-tendermint-42
179// - sequence: 1
180// - payload:
181//   - sourcePort: app
182//   - destinationPort: destinationPort
183//   - version: v1
184//   - encoding: application/json
185//   - value: {}
186//
187// OnRecvPacket (0)
188// OnTimeoutPacket (0)
189// OnAcknowledgementPacket (1)
190// - sourceClient: 07-tendermint-1
191// - destinationClient: 07-tendermint-42
192// - sequence: 1
193// - payload:
194//   - sourcePort: app
195//   - destinationPort: destinationPort
196//   - version: v1
197//   - encoding: application/json
198//   - value: {}
199// - ack: {"response":{"result":"BQ=="}}
200
201// Events:
202// [
203//   {
204//     "type": "create_client",
205//     "attrs": [
206//       {
207//         "key": "client_id",
208//         "value": "07-tendermint-1"
209//       },
210//       {
211//         "key": "client_type",
212//         "value": "07-tendermint"
213//       },
214//       {
215//         "key": "consensus_heights",
216//         "value": "2/2"
217//       }
218//     ],
219//     "pkg_path": "gno.land/r/aib/ibc/core"
220//   },
221//   {
222//     "type": "send_packet",
223//     "attrs": [
224//       {
225//         "key": "packet_source_client",
226//         "value": "07-tendermint-1"
227//       },
228//       {
229//         "key": "packet_dest_client",
230//         "value": "07-tendermint-42"
231//       },
232//       {
233//         "key": "packet_sequence",
234//         "value": "1"
235//       },
236//       {
237//         "key": "packet_timeout_timestamp",
238//         "value": "1234571490"
239//       },
240//       {
241//         "key": "encoded_packet_hex",
242//         "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a300a03617070120f64657374696e6174696f6e506f72741a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
243//       }
244//     ],
245//     "pkg_path": "gno.land/r/aib/ibc/core"
246//   },
247//   {
248//     "type": "acknowledge_packet",
249//     "attrs": [
250//       {
251//         "key": "packet_source_client",
252//         "value": "07-tendermint-1"
253//       },
254//       {
255//         "key": "packet_dest_client",
256//         "value": "07-tendermint-42"
257//       },
258//       {
259//         "key": "packet_sequence",
260//         "value": "1"
261//       },
262//       {
263//         "key": "packet_timeout_timestamp",
264//         "value": "1234571490"
265//       },
266//       {
267//         "key": "encoded_packet_hex",
268//         "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a300a03617070120f64657374696e6174696f6e506f72741a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
269//       }
270//     ],
271//     "pkg_path": "gno.land/r/aib/ibc/core"
272//   }
273// ]
274