Search Apps Documentation Source Content File Folder Download Copy Actions Download

z2p_update_client_filetest.gno

8.18 Kb · 174 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"time"
  6
  7	"gno.land/p/aib/ibc/lightclient/tendermint"
  8	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
  9	"gno.land/p/aib/ibc/types"
 10	"gno.land/r/aib/ibc/core"
 11)
 12
 13// UpdateClient conflicting header
 14func main(cur realm) {
 15	// CreateClient and RegisterCounterparty
 16	var (
 17		chainID     = "atomone-1"
 18		height      = uint64(2)
 19		clientState = tmtesting.NewClientState(chainID, types.NewHeight(1, height))
 20		apphash     = tmtesting.Hash("apphash-2")
 21		// priv=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==
 22		val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=",
 23			"VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 1)
 24		// priv=nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 25		val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=",
 26			"NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 1)
 27		trustedValset  = tendermint.NewValset(val1, val2)
 28		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 29	)
 30	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 31	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, "07-tendermint-2")
 32	// Update clientID
 33	// NOTE code generated by:
 34	// go run -C ./cmd/gen-block-signatures . -apphash-seed=apphash-3 -chainid=atomone-1 -header-time-shift=1 -height=3 -privkeys=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==,nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 35	{
 36		var (
 37			apphash         = tmtesting.Hash("apphash-3")
 38			commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
 39			newHeight       = height + 1 // adjacent height
 40			newTimestamp    = consensusState.Timestamp.Add(time.Minute * time.Duration(1))
 41			valset          = tendermint.NewValset(val1, val2)
 42			nextValset      = tendermint.NewValset(val1, val2)
 43			trustedHeight   = clientState.LatestHeight
 44
 45			signatures = []tendermint.CommitSig{
 46				{
 47					BlockIDFlag:      tendermint.BlockIDFlagCommit,
 48					ValidatorAddress: valset.Validators[0].Address,
 49					Timestamp:        commitTimestamp,
 50					Signature:        []byte("\x5c\xd2\x8b\xe3\x4b\x60\x3e\xaa\x75\x3c\xce\x24\xfe\x15\x75\x55\x84\xd4\xa3\xce\xbe\x0f\x94\xe9\xf7\x27\xb3\x7a\xdd\x02\x2d\xa0\x0b\xa7\x83\x7f\x50\xc3\xde\x3d\x95\x59\xb3\xad\xed\xd0\xdd\x23\x1d\x39\x9a\x8e\x1f\xc3\xcf\xdb\x1d\xa9\x93\xf5\x9a\xc0\x2b\x05"),
 51				},
 52				{
 53					BlockIDFlag:      tendermint.BlockIDFlagCommit,
 54					ValidatorAddress: valset.Validators[1].Address,
 55					Timestamp:        commitTimestamp,
 56					Signature:        []byte("\x34\x41\x2e\x78\x7f\xbf\x70\xef\x14\x48\xe3\x14\xd5\x83\xdc\x42\xff\x40\xf6\x5b\x71\x62\x09\xf9\x6f\x54\x63\x3e\xdb\xc5\x98\xc0\x9e\xa7\xde\x33\xac\xa7\x5f\xbb\xd6\x63\x49\xe1\xe9\x98\x86\x03\x46\x6a\x7e\xb6\x5d\xe5\x71\xe5\x1e\x5d\x4d\xd8\x8d\xbe\x2f\x01"),
 57				},
 58			}
 59
 60			msgHeader = tmtesting.NewMsgHeader(
 61				chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
 62				nextValset, trustedValset, signatures,
 63			)
 64		)
 65		core.UpdateClient(cross(cur), clientID, msgHeader)
 66	}
 67
 68	// Update with conflicting header (same height but different header)
 69	// This should froze the light client.
 70	// NOTE code generated by:
 71	// go run -C ./cmd/gen-block-signatures . -apphash-seed=apphash-4 -chainid=atomone-1 -header-time-shift=1 -height=3 -privkeys=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==,nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 72	{
 73		var (
 74			apphash = tmtesting.Hash("apphash-4")
 75			// priv=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==
 76			val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=", "VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 1)
 77			// priv=nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 78			val2            = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=", "NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 1)
 79			valset          = tendermint.NewValset(val1, val2)
 80			commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
 81			newHeight       = uint64(3)
 82			newTimestamp    = consensusState.Timestamp.Add(time.Minute * time.Duration(1))
 83			nextValset      = tendermint.NewValset(val1, val2)
 84			trustedHeight   = clientState.LatestHeight
 85
 86			signatures = []tendermint.CommitSig{
 87				{
 88					BlockIDFlag:      tendermint.BlockIDFlagCommit,
 89					ValidatorAddress: valset.Validators[0].Address,
 90					Timestamp:        commitTimestamp,
 91					Signature:        []byte("\x4c\x75\x8c\x96\x1b\xf5\xa8\x4b\xba\xc4\x48\x68\xf2\x19\x4f\x38\x65\x3b\x33\x19\xa4\xcc\x17\x77\x27\x3b\xff\xc8\x0d\xc0\xc4\xb8\x66\xad\x13\x67\x9a\x01\x6d\x05\xb0\x70\x53\x6b\xe0\xb7\x28\x87\x05\x23\x39\xc9\xca\x6c\xdc\xd1\xae\x25\x42\xff\x58\x2b\xec\x03"),
 92				},
 93				{
 94					BlockIDFlag:      tendermint.BlockIDFlagCommit,
 95					ValidatorAddress: valset.Validators[1].Address,
 96					Timestamp:        commitTimestamp,
 97					Signature:        []byte("\x78\xdd\x10\xad\x67\xa1\x4c\xeb\x9f\xe2\x73\x5c\x3b\x19\xea\xec\x26\x0c\x0a\x54\xb1\xec\x86\xf4\x20\x5b\x90\x5f\x53\x07\xe2\x98\xd5\x39\x69\x2e\x3a\x36\x35\xe6\xfb\x7b\xb9\x12\xfc\x99\x46\xe6\xf0\xd4\x28\x4b\xad\xdf\x18\x6c\xbc\xba\x4a\x32\xf8\xa6\x5f\x0a"),
 98				},
 99			}
100
101			msgHeader = tmtesting.NewMsgHeader(
102				chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
103				nextValset, trustedValset, signatures,
104			)
105		)
106		core.UpdateClient(cross(cur), clientID, msgHeader)
107	}
108
109	println("----------- assert render clients")
110	println(core.Render("clients")) // will show a frozenHeight!=0 and status="frozen"
111	println("----------- assert render clients/07-tendermint-1/consensus_states")
112	println(core.Render("clients/" + clientID + "/consensus_states"))
113}
114
115// Output:
116// ----------- assert render clients
117// {"items":[{"id":"07-tendermint-1","type":"07-tendermint","creator":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","status":"Frozen","counterparty_client_id":"07-tendermint-2","counterparty_merke_prefix":["iavlStoreKey","prefix2"],"client_state":{"chain_id":"atomone-1","latest_height":{"revision_number":1,"revision_height":3},"frozen_height":{"revision_number":0,"revision_height":1},"trust_level":{"numerator":1,"denominator":3},"trusting_period":3600,"unbonding_period":10800,"max_clock_drift":3600,"upgrade_path":[]},"last_consensus_state":{"height":{"revision_number":1,"revision_height":3},"timestamp":1234567950,"root":"m9afpc+PXaYRHdGC7FkrzuzRNn8ONC1xRCzYYAPRCU8=","next_validators_hash":"OJfJuH6dfiZI2d0uAxqamqfIVqV4c4oEAZigvJ8UqFQ="}}],"page":1,"total":1}
118// ----------- assert render clients/07-tendermint-1/consensus_states
119// {"items":[{"height":{"revision_number":1,"revision_height":2},"timestamp":1234567890,"root":"YAdxKyKDg+tUQc5Dli2fYkJ2imznqbJ+UDfQ7y0QNlM=","next_validators_hash":"OJfJuH6dfiZI2d0uAxqamqfIVqV4c4oEAZigvJ8UqFQ="},{"height":{"revision_number":1,"revision_height":3},"timestamp":1234567950,"root":"m9afpc+PXaYRHdGC7FkrzuzRNn8ONC1xRCzYYAPRCU8=","next_validators_hash":"OJfJuH6dfiZI2d0uAxqamqfIVqV4c4oEAZigvJ8UqFQ="}],"page":1,"total":1}
120
121// Events:
122// [
123//   {
124//     "type": "create_client",
125//     "attrs": [
126//       {
127//         "key": "client_id",
128//         "value": "07-tendermint-1"
129//       },
130//       {
131//         "key": "client_type",
132//         "value": "07-tendermint"
133//       },
134//       {
135//         "key": "consensus_heights",
136//         "value": "1/2"
137//       }
138//     ],
139//     "pkg_path": "gno.land/r/aib/ibc/core"
140//   },
141//   {
142//     "type": "update_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": "1/3"
155//       }
156//     ],
157//     "pkg_path": "gno.land/r/aib/ibc/core"
158//   },
159//   {
160//     "type": "client_misbehaviour",
161//     "attrs": [
162//       {
163//         "key": "client_id",
164//         "value": "07-tendermint-1"
165//       },
166//       {
167//         "key": "client_type",
168//         "value": "07-tendermint"
169//       }
170//     ],
171//     "pkg_path": "gno.land/r/aib/ibc/core"
172//   }
173// ]
174