Search Apps Documentation Source Content File Folder Download Copy Actions Download

zz_upgrade_client_example_filetest.gno

5.32 Kb · 143 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"time"
  6
  7	"gno.land/p/aib/ibc/lightclient/tendermint"
  8	"gno.land/p/aib/ibc/types"
  9	"gno.land/p/aib/ics23"
 10	"gno.land/r/aib/ibc/core"
 11)
 12
 13func main(cur realm) {
 14	// UpgradeClient applies a counterparty chain upgrade to the local
 15	// client. The relayer reads the upgraded client/consensus state and
 16	// chained membership proofs from the counterparty's upgrade store at
 17	// {UpgradePath}/{H}/upgradedClient and {UpgradePath}/{H}/upgradedConsState.
 18	//
 19	// Generate the proofs with: go run -C ./cmd/gen-proof . upgrade
 20	specs := ics23.IavlSpec()
 21
 22	clientID := "07-tendermint-1" // XXX update
 23
 24	// The upgraded client state, as committed by the counterparty.
 25	// Only chain-specified fields (ChainID, UnbondingPeriod, LatestHeight,
 26	// ProofSpecs, UpgradePath) need accurate values — customizable fields
 27	// (TrustLevel, TrustingPeriod, MaxClockDrift) are dropped on
 28	// verification via ZeroCustomFields.
 29	upgradedClient := tendermint.ClientState{ // XXX update
 30		ChainID:         "chain-after-upgrade-2",
 31		TrustLevel:      tendermint.NewFraction(1, 3),
 32		TrustingPeriod:  2 * 7 * 24 * time.Hour,
 33		UnbondingPeriod: 3 * 7 * 24 * time.Hour,
 34		MaxClockDrift:   10 * time.Second,
 35		LatestHeight:    types.Height{RevisionNumber: 2, RevisionHeight: 1},
 36		ProofSpecs:      ics23.GetSDKProofSpecs(),
 37		UpgradePath:     []string{"upgrade", "upgradedIBCState"},
 38	}
 39
 40	// The upgraded consensus state. Root is a sentinel value because the
 41	// upgraded chain hasn't produced any blocks yet at commit time; real
 42	// roots arrive afterwards via UpdateClient.
 43	upgradedConsState := tendermint.ConsensusState{ // XXX update
 44		Timestamp:          time.Unix(1700000000, 0).UTC(),
 45		Root:               tendermint.NewMerkleRoot([]byte(tendermint.SentinelRoot)),
 46		NextValidatorsHash: make([]byte, 32),
 47	}
 48
 49	// Proof that the upgraded client state was committed under
 50	//   {UpgradePath[0]}/{UpgradePath[1]}/{H}/upgradedClient
 51	// in the upgrade IAVL store. Two CommitmentProof entries: the IAVL
 52	// existence proof for the inner key, and the rootmulti existence
 53	// proof for the store name.
 54	proofUpgradeClient := []ics23.CommitmentProof{
 55		// iavl proof: existence of key `upgradedIBCState/{H}/upgradedClient`
 56		// in the IAVL store named in UpgradePath[0] (e.g. "upgrade").
 57		ics23.CommitmentProof_Exist{
 58			Exist: &ics23.ExistenceProof{
 59				Key:   []byte("\x75\x70\x67\x72\x61\x64\x65\x64\x49\x42\x43\x53\x74\x61\x74\x65\x2f\x31\x30\x30\x2f\x75\x70\x67\x72\x61\x64\x65\x64\x43\x6c\x69\x65\x6e\x74"), // XXX update
 60				Value: []byte{}, // XXX update — Any-wrapped ZeroCustomFields(upgradedClient) bytes
 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"), // XXX update
 67				},
 68				Path: []*ics23.InnerOp{
 69					// XXX update — depth and number of inner ops depend on
 70					// the IAVL tree shape at commit time
 71					{
 72						Hash:   specs.InnerSpec.Hash,
 73						Prefix: []byte{}, // XXX update
 74						Suffix: []byte{}, // XXX update
 75					},
 76				},
 77			},
 78		},
 79		// rootmulti proof: existence of UpgradePath[0] (the store name)
 80		// in the chain's multistore root.
 81		ics23.CommitmentProof_Exist{
 82			Exist: &ics23.ExistenceProof{
 83				Key:   []byte("\x75\x70\x67\x72\x61\x64\x65"), // XXX update — UpgradePath[0]
 84				Value: []byte{},                               // XXX update — IAVL store hash
 85				Leaf: &ics23.LeafOp{
 86					Hash:         specs.LeafSpec.Hash,
 87					PrehashKey:   specs.LeafSpec.PrehashKey,
 88					PrehashValue: specs.LeafSpec.PrehashValue,
 89					Length:       specs.LeafSpec.Length,
 90					Prefix:       []byte("\x00"),
 91				},
 92				Path: []*ics23.InnerOp{},
 93			},
 94		},
 95	}
 96
 97	// Same shape as proofUpgradeClient, but for the upgraded consensus
 98	// state at `upgradedIBCState/{H}/upgradedConsState`.
 99	proofUpgradeConsState := []ics23.CommitmentProof{
100		ics23.CommitmentProof_Exist{
101			Exist: &ics23.ExistenceProof{
102				Key:   []byte("\x75\x70\x67\x72\x61\x64\x65\x64\x49\x42\x43\x53\x74\x61\x74\x65\x2f\x31\x30\x30\x2f\x75\x70\x67\x72\x61\x64\x65\x64\x43\x6f\x6e\x73\x53\x74\x61\x74\x65"), // XXX update
103				Value: []byte{}, // XXX update — Any-wrapped upgradedConsState bytes
104				Leaf: &ics23.LeafOp{
105					Hash:         specs.LeafSpec.Hash,
106					PrehashKey:   specs.LeafSpec.PrehashKey,
107					PrehashValue: specs.LeafSpec.PrehashValue,
108					Length:       specs.LeafSpec.Length,
109					Prefix:       []byte("\x00\x02\x02"), // XXX update
110				},
111				Path: []*ics23.InnerOp{
112					{
113						Hash:   specs.InnerSpec.Hash,
114						Prefix: []byte{}, // XXX update
115						Suffix: []byte{}, // XXX update
116					},
117				},
118			},
119		},
120		ics23.CommitmentProof_Exist{
121			Exist: &ics23.ExistenceProof{
122				Key:   []byte("\x75\x70\x67\x72\x61\x64\x65"), // XXX update — UpgradePath[0]
123				Value: []byte{},                               // XXX update — IAVL store hash
124				Leaf: &ics23.LeafOp{
125					Hash:         specs.LeafSpec.Hash,
126					PrehashKey:   specs.LeafSpec.PrehashKey,
127					PrehashValue: specs.LeafSpec.PrehashValue,
128					Length:       specs.LeafSpec.Length,
129					Prefix:       []byte("\x00"),
130				},
131				Path: []*ics23.InnerOp{},
132			},
133		},
134	}
135
136	core.UpgradeClient(cross(cur), clientID,
137		upgradedClient, upgradedConsState,
138		proofUpgradeClient, proofUpgradeConsState,
139	)
140}
141
142// Error:
143// client 07-tendermint-1 not found