Search Apps Documentation Source Content File Folder Download Copy Actions Download

z6a_timeout_filetest.gno

11.97 Kb · 360 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"encoding/hex"
  6	"testing"
  7	"time"
  8
  9	"gno.land/p/aib/ibc/lightclient/tendermint"
 10	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
 11	"gno.land/p/aib/ibc/types"
 12	"gno.land/p/aib/ics23"
 13	appstesting "gno.land/r/aib/ibc/apps/testing"
 14	"gno.land/r/aib/ibc/core"
 15)
 16
 17// Timeout success
 18func main(cur realm) {
 19	var (
 20		chainID       = "atomone-1"
 21		trustedHeight = types.NewHeight(1, 2)
 22		clientState   = tmtesting.NewClientState(chainID, trustedHeight)
 23		apphash       = tmtesting.Hash("apphash-1")
 24		// priv=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==
 25		val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=",
 26			"VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 1)
 27		// priv=nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 28		val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=",
 29			"NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 1)
 30		trustedValset  = tendermint.NewValset(val1, val2)
 31		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 32		counterpartyID = "07-tendermint-42"
 33	)
 34	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 35	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
 36	// Register app
 37	var (
 38		app       = appstesting.NewApp(cross(cur))
 39		appPortID = "app"
 40	)
 41	core.RegisterApp(cross(cur), appPortID, app)
 42	// Send a packet
 43	sendPacket := types.MsgSendPacket{
 44		SourceClient:     clientID,
 45		TimeoutTimestamp: uint64(time.Now().Add(10 * time.Minute).Unix()),
 46		Payloads: []types.Payload{{
 47			SourcePort:      appPortID,
 48			DestinationPort: "destinationPort",
 49			Encoding:        "application/json",
 50			Value:           []byte("{}"),
 51			Version:         "v1",
 52		}},
 53	}
 54	sequence := core.SendPacket(cross(cur), sendPacket)
 55	// Change block time to after the timeout
 56	ctx := testing.GetContext()
 57	ctx.Time = time.Now().Add(12 * time.Minute)
 58	testing.SetContext(ctx)
 59	// Update client with a block created after the timeout
 60	// NOTE code generated by:
 61	// go run -C ./cmd/gen-block-signatures . -apphash-hex=82ea91f259352674360c621c4b0a1975cc076e3792b6d22a8bfbe8e7f7d0d2d7 -chainid=atomone-1 -header-time-shift=12 -height=12 -privkeys=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==,nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 62	// NOTE this apphash was provided by the gen-proof command below.
 63	apphash, _ = hex.DecodeString("82ea91f259352674360c621c4b0a1975cc076e3792b6d22a8bfbe8e7f7d0d2d7")
 64	trustedHeight = clientState.LatestHeight
 65	var (
 66		newHeight       = uint64(12)
 67		commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
 68		newTimestamp    = consensusState.Timestamp.Add(time.Minute * time.Duration(12))
 69		valset          = tendermint.NewValset(val1, val2)
 70		nextValset      = tendermint.NewValset(val1, val2)
 71
 72		signatures = []tendermint.CommitSig{
 73			{
 74				BlockIDFlag:      tendermint.BlockIDFlagCommit,
 75				ValidatorAddress: valset.Validators[0].Address,
 76				Timestamp:        commitTimestamp,
 77				Signature:        []byte("\x90\x76\xf4\x27\xaf\x23\xd0\xcb\x97\xb1\x6d\xfa\x86\xae\x6d\x46\xe9\xe4\x65\x76\xf2\x6a\x1d\x5f\x83\xa6\x9e\x4a\x28\x55\x6e\x74\x84\x02\xff\x15\xc0\x41\xed\x15\xb7\xef\x3a\x3f\xe1\xe6\xa0\x8f\x34\x83\x9c\x2e\x27\x26\x69\x00\x73\x45\x87\xd2\x3c\xe6\x12\x07"),
 78			},
 79			{
 80				BlockIDFlag:      tendermint.BlockIDFlagCommit,
 81				ValidatorAddress: valset.Validators[1].Address,
 82				Timestamp:        commitTimestamp,
 83				Signature:        []byte("\x45\x61\x3b\x34\x58\xa1\xf5\x75\x16\x6f\xb3\xad\xda\x12\xa8\xef\xb6\x64\x3f\x6b\x80\x73\x74\xe3\x15\x6f\xe1\x3b\x3f\x50\xed\x0d\xbc\xa7\x54\x11\x72\xe3\x71\x22\x95\xec\x77\x93\x45\x03\x67\xa5\x00\x4c\x84\x3b\x1c\xa4\xf7\x0e\x95\xd1\xe2\x04\xb6\x91\x63\x09"),
 84			},
 85		}
 86
 87		msgHeader = tmtesting.NewMsgHeader(
 88			chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
 89			nextValset, trustedValset, signatures,
 90		)
 91	)
 92	core.UpdateClient(cross(cur), clientID, msgHeader)
 93
 94	// Timeout the packet
 95	// Generate the proof that no RecvPacket have been received by the
 96	// counterparty chain.
 97	specs := ics23.IavlSpec()
 98	// NOTE code generated by:
 99	// go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'receipt'
100	proof := []ics23.CommitmentProof{
101
102		// iavl proof
103		ics23.CommitmentProof_Nonexist{
104			Nonexist: &ics23.NonExistenceProof{
105				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\x02\x00\x00\x00\x00\x00\x00\x00\x01"),
106				Left: &ics23.ExistenceProof{
107					Key:   []byte("\x50"),
108					Value: []byte("\x50"),
109					Leaf: &ics23.LeafOp{
110						Hash:         specs.LeafSpec.Hash,
111						PrehashKey:   specs.LeafSpec.PrehashKey,
112						PrehashValue: specs.LeafSpec.PrehashValue,
113						Length:       specs.LeafSpec.Length,
114						Prefix:       []byte("\x00\x02\x02"),
115					},
116					Path: []*ics23.InnerOp{
117						{
118							Hash:   specs.InnerSpec.Hash,
119							Prefix: []byte("\x04\x06\x02\x20"),
120							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"),
121						},
122						{
123							Hash:   specs.InnerSpec.Hash,
124							Prefix: []byte("\x06\x0a\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"),
125							Suffix: []byte(""),
126						},
127					},
128				},
129				Right: &ics23.ExistenceProof{
130					Key:   []byte("\x72"),
131					Value: []byte("\x72"),
132					Leaf: &ics23.LeafOp{
133						Hash:         specs.LeafSpec.Hash,
134						PrehashKey:   specs.LeafSpec.PrehashKey,
135						PrehashValue: specs.LeafSpec.PrehashValue,
136						Length:       specs.LeafSpec.Length,
137						Prefix:       []byte("\x00\x02\x02"),
138					},
139					Path: []*ics23.InnerOp{
140						{
141							Hash:   specs.InnerSpec.Hash,
142							Prefix: []byte("\x02\x04\x02\x20"),
143							Suffix: []byte("\x20\xa3\x03\x93\x0c\xa8\x83\x16\x18\xac\x7e\x4d\xdd\x10\x54\x6c\xfc\x36\x6f\xb7\x30\xd6\x63\x0c\x03\x0a\x97\x22\x6b\xbe\xfc\x69\x35"),
144						},
145						{
146							Hash:   specs.InnerSpec.Hash,
147							Prefix: []byte("\x04\x06\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"),
148							Suffix: []byte(""),
149						},
150						{
151							Hash:   specs.InnerSpec.Hash,
152							Prefix: []byte("\x06\x0a\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"),
153							Suffix: []byte(""),
154						},
155					},
156				},
157			},
158		},
159
160		// rootmulti proof
161		ics23.CommitmentProof_Exist{
162			Exist: &ics23.ExistenceProof{
163				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
164				Value: []byte("\x38\x22\xf3\x2f\x21\x66\x53\x63\x29\x4f\x96\xed\xda\x15\xa7\x81\x1a\x67\x6c\x2b\xa9\xdd\xcc\xec\x46\x63\x64\xf1\x00\x69\x82\x7d"),
165				Leaf: &ics23.LeafOp{
166					Hash:         specs.LeafSpec.Hash,
167					PrehashKey:   specs.LeafSpec.PrehashKey,
168					PrehashValue: specs.LeafSpec.PrehashValue,
169					Length:       specs.LeafSpec.Length,
170					Prefix:       []byte("\x00"),
171				},
172				Path: []*ics23.InnerOp{},
173			},
174		},
175	}
176
177	timeoutPacket := types.MsgTimeout{
178		Packet: types.Packet{
179			Sequence:          sequence,
180			SourceClient:      clientID,
181			DestinationClient: counterpartyID,
182			TimeoutTimestamp:  sendPacket.TimeoutTimestamp,
183			Payloads:          sendPacket.Payloads,
184		},
185		ProofUnreceived: proof,
186		ProofHeight:     msgHeader.GetHeight(),
187	}
188
189	res := core.Timeout(cross(cur), timeoutPacket)
190
191	println("res:", res)
192	println("\n----------- assert render clients/07-tendermint-1/packet_commitments")
193	println(core.Render("clients/" + clientID + "/packet_commitments"))
194	println("----------- assert render clients/07-tendermint-1/packet_commitments/1 (cleared)")
195	println(core.Render("clients/" + clientID + "/packet_commitments/1"))
196	println("\n----------- app report")
197	println(app.Report())
198
199	// assert NOOP response (res=1) if timeout is resubmitted
200	res = core.Timeout(cross(cur), timeoutPacket)
201
202	println("res:", res)
203	println("\n----------- app report")
204	println(app.Report())
205}
206
207// Output:
208// res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
209//
210// ----------- assert render clients/07-tendermint-1/packet_commitments
211// {"items":[],"page":1,"total":1}
212// ----------- assert render clients/07-tendermint-1/packet_commitments/1 (cleared)
213// {"error":"sequence 1 not found"}
214//
215// ----------- app report
216// OnSendPacket (1)
217// - sourceClient: 07-tendermint-1
218// - destinationClient: 07-tendermint-42
219// - sequence: 1
220// - payload:
221//   - sourcePort: app
222//   - destinationPort: destinationPort
223//   - version: v1
224//   - encoding: application/json
225//   - value: {}
226//
227// OnRecvPacket (0)
228// OnTimeoutPacket (1)
229// - sourceClient: 07-tendermint-1
230// - destinationClient: 07-tendermint-42
231// - sequence: 1
232// - payload:
233//   - sourcePort: app
234//   - destinationPort: destinationPort
235//   - version: v1
236//   - encoding: application/json
237//   - value: {}
238//
239// OnAcknowledgementPacket (0)
240//
241// res: (1 gno.land/p/aib/ibc/types.ResponseResultType)
242//
243// ----------- app report
244// OnSendPacket (1)
245// - sourceClient: 07-tendermint-1
246// - destinationClient: 07-tendermint-42
247// - sequence: 1
248// - payload:
249//   - sourcePort: app
250//   - destinationPort: destinationPort
251//   - version: v1
252//   - encoding: application/json
253//   - value: {}
254//
255// OnRecvPacket (0)
256// OnTimeoutPacket (1)
257// - sourceClient: 07-tendermint-1
258// - destinationClient: 07-tendermint-42
259// - sequence: 1
260// - payload:
261//   - sourcePort: app
262//   - destinationPort: destinationPort
263//   - version: v1
264//   - encoding: application/json
265//   - value: {}
266//
267// OnAcknowledgementPacket (0)
268
269// Events:
270// [
271//   {
272//     "type": "create_client",
273//     "attrs": [
274//       {
275//         "key": "client_id",
276//         "value": "07-tendermint-1"
277//       },
278//       {
279//         "key": "client_type",
280//         "value": "07-tendermint"
281//       },
282//       {
283//         "key": "consensus_heights",
284//         "value": "1/2"
285//       }
286//     ],
287//     "pkg_path": "gno.land/r/aib/ibc/core"
288//   },
289//   {
290//     "type": "send_packet",
291//     "attrs": [
292//       {
293//         "key": "packet_source_client",
294//         "value": "07-tendermint-1"
295//       },
296//       {
297//         "key": "packet_dest_client",
298//         "value": "07-tendermint-42"
299//       },
300//       {
301//         "key": "packet_sequence",
302//         "value": "1"
303//       },
304//       {
305//         "key": "packet_timeout_timestamp",
306//         "value": "1234568490"
307//       },
308//       {
309//         "key": "encoded_packet_hex",
310//         "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220aa8ad8cc042a300a03617070120f64657374696e6174696f6e506f72741a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
311//       }
312//     ],
313//     "pkg_path": "gno.land/r/aib/ibc/core"
314//   },
315//   {
316//     "type": "update_client",
317//     "attrs": [
318//       {
319//         "key": "client_id",
320//         "value": "07-tendermint-1"
321//       },
322//       {
323//         "key": "client_type",
324//         "value": "07-tendermint"
325//       },
326//       {
327//         "key": "consensus_heights",
328//         "value": "1/12"
329//       }
330//     ],
331//     "pkg_path": "gno.land/r/aib/ibc/core"
332//   },
333//   {
334//     "type": "timeout_packet",
335//     "attrs": [
336//       {
337//         "key": "packet_source_client",
338//         "value": "07-tendermint-1"
339//       },
340//       {
341//         "key": "packet_dest_client",
342//         "value": "07-tendermint-42"
343//       },
344//       {
345//         "key": "packet_sequence",
346//         "value": "1"
347//       },
348//       {
349//         "key": "packet_timeout_timestamp",
350//         "value": "1234568490"
351//       },
352//       {
353//         "key": "encoded_packet_hex",
354//         "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220aa8ad8cc042a300a03617070120f64657374696e6174696f6e506f72741a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
355//       }
356//     ],
357//     "pkg_path": "gno.land/r/aib/ibc/core"
358//   }
359// ]
360