Search Apps Documentation Source Content File Folder Download Copy Actions Download

z8g_update_client_misbehaviour_filetest.gno

7.88 Kb · 164 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"testing"
  6	"time"
  7
  8	"gno.land/p/aib/ibc/lightclient/tendermint"
  9	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
 10	"gno.land/p/aib/ibc/types"
 11	"gno.land/r/aib/ibc/core"
 12)
 13
 14// UpdateClient with invalid misbehaviour:
 15// header2 trusted consensus older than trusting period
 16func main(cur realm) {
 17	// CreateClient and RegisterCounterparty
 18	var (
 19		chainID     = "atomone-1"
 20		height      = uint64(1)
 21		clientState = tmtesting.NewClientState(chainID, types.NewHeight(1, height))
 22		apphash     = tmtesting.Hash("apphash-2")
 23		// priv=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==
 24		val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=",
 25			"VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 10)
 26		// priv=nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 27		val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=",
 28			"NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 10)
 29		trustedValset  = tendermint.NewValset(val1, val2)
 30		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 31	)
 32	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 33	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, "07-tendermint-2")
 34
 35	// Change block time to expire trusted consensus at height 1 (step #1)
 36	// we do this in 2 steps because in between we add an other trusted consensus
 37	// so the UpdateClient will not reject the misbehavior because of expired
 38	// client.
 39	ctx := testing.GetContext()
 40	ctx.Time = time.Now().Add(clientState.TrustingPeriod / 2)
 41	testing.SetContext(ctx)
 42
 43	// NOTE code generated by:
 44	// go run -C ./cmd/gen-block-signatures . -chainid=atomone-1 -header-time-shift=30 -height=2 -new-validators=0 -privkeys=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==,nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 45	{
 46		var (
 47			apphash         = tmtesting.Hash("apphash-2")
 48			valset          = tendermint.NewValset(val1, val2)
 49			commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
 50			newHeight       = uint64(2)
 51			newTimestamp    = consensusState.Timestamp.Add(time.Minute * time.Duration(30))
 52			nextValset      = tendermint.NewValset(val1, val2)
 53			trustedHeight   = clientState.LatestHeight
 54
 55			signatures = []tendermint.CommitSig{
 56				{
 57					BlockIDFlag:      tendermint.BlockIDFlagCommit,
 58					ValidatorAddress: valset.Validators[0].Address,
 59					Timestamp:        commitTimestamp,
 60					Signature:        []byte("\x36\x14\x4e\x60\x72\x73\x93\xb1\x26\x89\xd0\x1e\x1a\xd0\xd7\xda\xf2\xb2\x86\xa8\xb8\x4d\x7c\xf9\xfc\xdf\xab\x58\xd8\x3b\x74\x5a\x0f\xe2\x0f\xe0\x57\x69\x09\xf8\xd3\xea\xcc\xd8\x65\xed\x16\x5a\x87\xc3\x7f\x13\x16\x88\x53\xf5\xe8\xf0\xef\xe9\x87\x05\x9e\x05"),
 61				},
 62				{
 63					BlockIDFlag:      tendermint.BlockIDFlagCommit,
 64					ValidatorAddress: valset.Validators[1].Address,
 65					Timestamp:        commitTimestamp,
 66					Signature:        []byte("\xd4\xee\xf2\xb7\xc0\x73\x09\x68\x4e\x80\x38\x95\xc4\x4b\x56\xc3\x05\x59\xc3\x47\x8b\xb8\x1e\xf5\x27\x0a\xd4\xa6\x6a\xc9\xe0\x1a\xec\x78\x66\x56\x15\x18\x56\xb0\xa0\x0f\x24\x72\xce\x8a\x2a\x39\x0d\x2d\x6f\xbe\x28\x9a\xc3\x52\x21\x40\x54\x65\x08\xd9\x36\x09"),
 67				},
 68			}
 69
 70			msgHeader = tmtesting.NewMsgHeader(
 71				chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
 72				nextValset, trustedValset, signatures,
 73			)
 74		)
 75		core.UpdateClient(cross(cur), clientID, msgHeader)
 76	}
 77	// Change block time to expire trusted consensus at height 1 (step #2)
 78	ctx = testing.GetContext()
 79	ctx.Time = time.Now().Add(clientState.TrustingPeriod / 2)
 80	testing.SetContext(ctx)
 81
 82	// Build valid misbehavior
 83	var header1, header2 *tendermint.MsgHeader
 84	// NOTE code generated by:
 85	// go run -C ./cmd/gen-block-signatures . -chainid=atomone-1 -header-time-shift=0 -height=4 -new-validators=0 -privkeys=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==,nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
 86	{
 87		var (
 88			apphash         = tmtesting.Hash("apphash-4")
 89			valset          = tendermint.NewValset(val1, val2)
 90			commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
 91			newHeight       = uint64(4)
 92			newTimestamp    = consensusState.Timestamp.Add(time.Minute * time.Duration(0))
 93			nextValset      = tendermint.NewValset(val1, val2)
 94			trustedHeight   = types.NewHeight(1, 2)
 95
 96			signatures = []tendermint.CommitSig{
 97				{
 98					BlockIDFlag:      tendermint.BlockIDFlagCommit,
 99					ValidatorAddress: valset.Validators[0].Address,
100					Timestamp:        commitTimestamp,
101					Signature:        []byte("\x2e\xba\x21\xb0\x2f\xd2\x85\xb9\xef\x82\x68\xdc\xef\xd1\xd1\x12\x70\x88\x94\x10\x7e\x4d\x49\xac\x46\x3d\x86\xe2\xf2\xae\x38\xb4\xa5\xab\x0c\xc1\x8f\x8a\x59\xda\x36\x17\x01\xe4\x16\x49\xbf\x03\x86\xf0\x31\x3f\x30\x37\x9e\x47\x28\x72\x3b\x0c\x89\xb9\x94\x05"),
102				},
103				{
104					BlockIDFlag:      tendermint.BlockIDFlagCommit,
105					ValidatorAddress: valset.Validators[1].Address,
106					Timestamp:        commitTimestamp,
107					Signature:        []byte("\x67\xcc\xc6\xc4\x20\xa0\xcb\x36\xaf\x33\x5c\xf8\xe4\xad\x47\x49\x38\x9d\x9e\xa3\x7c\xe6\x88\x81\x62\x51\x7f\xcc\xa5\x92\x0d\x98\xe9\xbe\x71\x2d\x22\xcd\x41\x30\x28\x83\x03\xcb\xf5\xd7\x28\xde\x7b\x92\x85\x9e\xa6\xe5\xde\x50\x89\x2d\x2b\xc5\x9d\x9a\x33\x00"),
108				},
109			}
110		)
111
112		header1 = tmtesting.NewMsgHeader(
113			chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
114			nextValset, trustedValset, signatures,
115		)
116	}
117	// NOTE code generated by:
118	// go run -C ./cmd/gen-block-signatures . -apphash-seed=apphash-3 -chainid=atomone-1 -header-time-shift=0 -height=3 -privkeys=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==,nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
119	{
120		var (
121			apphash         = tmtesting.Hash("apphash-3")
122			valset          = tendermint.NewValset(val1, val2)
123			commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
124			newHeight       = uint64(3)
125			newTimestamp    = consensusState.Timestamp.Add(time.Minute * time.Duration(0))
126			nextValset      = tendermint.NewValset(val1, val2)
127			trustedHeight   = clientState.LatestHeight
128
129			signatures = []tendermint.CommitSig{
130				{
131					BlockIDFlag:      tendermint.BlockIDFlagCommit,
132					ValidatorAddress: valset.Validators[0].Address,
133					Timestamp:        commitTimestamp,
134					Signature:        []byte("\x51\xa4\x8f\x78\x42\x4e\x6e\x0d\xc3\x2b\xe6\xcb\x09\x5c\xe5\x7d\x35\x84\xcf\xb1\x0f\x53\x72\x0c\x41\xde\xd3\x6d\xbc\x81\x28\x64\x41\xf7\xe9\x1e\xa9\x93\xd0\xa4\x84\x16\xca\xa8\xa3\x8e\x56\x45\xec\xae\x1d\x24\xb4\xa8\xf4\x29\x85\x04\x06\xe4\x18\x6b\x7d\x08"),
135				},
136				{
137					BlockIDFlag:      tendermint.BlockIDFlagCommit,
138					ValidatorAddress: valset.Validators[1].Address,
139					Timestamp:        commitTimestamp,
140					Signature:        []byte("\xd9\xd6\x82\x83\x78\x75\xfa\x2e\xd7\x1b\xa5\x3d\xfe\xb3\x80\xb6\xc7\x59\x20\x45\x10\xdf\x0c\x19\xd1\xcc\xfc\x37\xd5\x59\x79\x95\x93\x60\x08\xfb\x5f\xa0\x87\x95\x51\x1e\x29\xed\x94\xd1\x31\x5e\xda\x90\x86\x11\xbe\xb2\x88\x68\xe1\xba\xa7\x31\xc7\xa2\xda\x07"),
141				},
142			}
143		)
144
145		header2 = tmtesting.NewMsgHeader(
146			chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
147			nextValset, trustedValset, signatures,
148		)
149	}
150	msg := &tendermint.Misbehaviour{
151		Header1: header1,
152		Header2: header2,
153	}
154
155	core.UpdateClient(cross(cur), clientID, msg)
156
157	println("----------- assert render clients")
158	println(core.Render("clients"))
159	println("----------- assert render clients/07-tendermint-1/consensus_states")
160	println(core.Render("clients/" + clientID + "/consensus_states"))
161}
162
163// Error:
164// verifying Header2 in Misbehaviour failed: current timestamp minus the latest consensus state timestamp is greater than or equal to the trusting period (1h0m0s >= 1h0m0s)