Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

getters.gno

5.68 Kb · 172 lines
  1package core
  2
  3import (
  4	"gno.land/p/onbloc/ibc/union/lightclient"
  5	"gno.land/p/onbloc/ibc/union/types"
  6	core "gno.land/r/onbloc/ibc/union/core"
  7)
  8
  9// getters.gno implements the read-only ICoreGetter surface. Results read the
 10// injected store / light client. Queries that fail on a missing lookup
 11// (client/consensus/connection/channel state, status, latest height, timestamp,
 12// batch commitments) return an error rather than a found-bool or a panic.
 13
 14// GetLightClient returns the live light client instance for a client, or
 15// (nil, false) when the client is missing.
 16func (c *coreV1) GetLightClient(clientId types.ClientId) (lightclient.Interface, bool) {
 17	return c.store.GetLightClient(clientId)
 18}
 19
 20// GetStatus returns the client's status, or an error when the client is missing.
 21func (c *coreV1) GetStatus(clientId types.ClientId) (types.Status, error) {
 22	lc, ok := c.store.GetLightClient(clientId)
 23	if !ok {
 24		return types.StatusUnknown, makeError(core.ErrClientNotFound)
 25	}
 26
 27	return lc.Status().ToType(), nil
 28}
 29
 30// GetClientType returns the registered client type for a client, or an error
 31// when the client is missing.
 32func (c *coreV1) GetClientType(clientId types.ClientId) (types.ClientType, error) {
 33	typ, ok := c.store.ClientType(clientId)
 34	if !ok {
 35		return "", makeError(core.ErrClientNotFound)
 36	}
 37
 38	return typ, nil
 39}
 40
 41// GetClientImpl is best-effort: gno has no client_type->impl address mapping, so
 42// it returns the registered client type as the impl identifier, or an error when
 43// the client is missing.
 44func (c *coreV1) GetClientImpl(clientId types.ClientId) (string, error) {
 45	typ, ok := c.store.ClientType(clientId)
 46	if !ok {
 47		return "", makeError(core.ErrClientNotFound)
 48	}
 49
 50	return string(typ), nil
 51}
 52
 53// GetRegisteredClientType reports whether a light client implementation is
 54// registered for the given client type.
 55func (c *coreV1) GetRegisteredClientType(clientType types.ClientType) bool {
 56	return c.store.HasRegisteredClient(clientType)
 57}
 58
 59// GetClientStateBytes returns the mirrored client state bytes, or an error when
 60// the client state is missing.
 61func (c *coreV1) GetClientStateBytes(clientId types.ClientId) ([]byte, error) {
 62	bz, ok := c.store.ClientStateBytes(clientId)
 63	if !ok {
 64		return nil, makeError(core.ErrClientStateNotFound)
 65	}
 66
 67	return bz, nil
 68}
 69
 70// GetConsensusStateBytes returns the consensus state bytes at a height, or an
 71// error when no consensus state is stored at that height.
 72func (c *coreV1) GetConsensusStateBytes(clientId types.ClientId, height types.Height) ([]byte, error) {
 73	bz, ok := c.store.ConsensusStateBytes(clientId, height)
 74	if !ok {
 75		return nil, makeError(core.ErrConsensusStateNotFound)
 76	}
 77
 78	return bz, nil
 79}
 80
 81// GetLatestHeight returns the light client's latest height, or an error when the
 82// client is missing.
 83func (c *coreV1) GetLatestHeight(clientId types.ClientId) (uint64, error) {
 84	lc, ok := c.store.GetLightClient(clientId)
 85	if !ok {
 86		return 0, makeError(core.ErrClientNotFound)
 87	}
 88
 89	return lc.GetLatestHeight(), nil
 90}
 91
 92// GetTimestampAtHeight returns the consensus timestamp recorded at a height, or
 93// an error when the client is missing or has no consensus state at that height.
 94func (c *coreV1) GetTimestampAtHeight(clientId types.ClientId, height uint64) (types.Timestamp, error) {
 95	lc, ok := c.store.GetLightClient(clientId)
 96	if !ok {
 97		return 0, makeError(core.ErrClientNotFound)
 98	}
 99
100	ts, err := lc.GetTimestampAtHeight(height)
101	if err != nil {
102		return 0, err
103	}
104
105	return ts, nil
106}
107
108// GetConnection returns the typed connection record, or an error when the
109// connection is missing.
110func (c *coreV1) GetConnection(connectionId types.ConnectionId) (types.Connection, error) {
111	conn, ok := c.store.GetConnection(connectionId)
112	if !ok {
113		return types.Connection{}, makeError(core.ErrConnectionNotFound)
114	}
115
116	return conn, nil
117}
118
119// GetChannels returns all stored channel records.
120func (c *coreV1) GetChannels() ([]types.Channel, error) {
121	return c.store.GetChannels()
122}
123
124// GetChannel returns the typed channel record, or an error when the channel
125// is missing.
126func (c *coreV1) GetChannel(channelId types.ChannelId) (types.Channel, error) {
127	ch, ok := c.store.GetChannel(channelId)
128	if !ok {
129		return types.Channel{}, makeError(core.ErrChannelNotFound)
130	}
131
132	return ch, nil
133}
134
135// GetBatchPacketsCommitment returns the batch packet commitment for a batch hash,
136// or an error when no batch packet commitment is stored for that hash.
137func (c *coreV1) GetBatchPacketsCommitment(batchHash types.H256) (types.H256, error) {
138	commitment, ok := c.store.BatchPacketsAt(batchHash)
139	if !ok {
140		return types.H256{}, makeError(core.ErrBatchPacketsNotFound)
141	}
142
143	return commitment, nil
144}
145
146// GetBatchReceiptsCommitment returns the batch receipt commitment for a batch hash,
147// or an error when no batch receipt commitment is stored for that hash.
148func (c *coreV1) GetBatchReceiptsCommitment(batchHash types.H256) (types.H256, error) {
149	commitment, ok := c.store.BatchReceiptsAt(batchHash)
150	if !ok {
151		return types.H256{}, makeError(core.ErrBatchReceiptsNotFound)
152	}
153
154	return commitment, nil
155}
156
157// GetCommittedMembershipProofCommitment returns the committed membership proof
158// value for a client/height/path key.
159func (c *coreV1) GetCommittedMembershipProofCommitment(clientId types.ClientId, proofHeight uint64, path []byte) (types.H256, error) {
160	commitment, ok := c.store.MembershipProofAt(clientId, proofHeight, path)
161	if !ok {
162		return types.H256{}, makeError(core.ErrMembershipProofNotFound)
163	}
164
165	return commitment, nil
166}
167
168// GetCommittedNonMembershipProof reports whether a non-membership proof
169// commitment exists for a client/height/path key.
170func (c *coreV1) GetCommittedNonMembershipProof(clientId types.ClientId, proofHeight uint64, path []byte) bool {
171	return c.store.HasNonMembershipProof(clientId, proofHeight, path)
172}