Search Apps Documentation Source Content File Folder Download Copy Actions Download

app.gno

4.51 Kb · 171 lines
  1package testing
  2
  3import (
  4	"gno.land/p/aib/ibc/app"
  5	"gno.land/p/aib/ibc/types"
  6	"gno.land/p/nt/ufmt/v0"
  7)
  8
  9type (
 10	App struct {
 11		id    string
 12		Calls struct {
 13			OnSendPacket                  []Args
 14			OnSendPacketReturn            error
 15			OnRecvPacket                  []Args
 16			OnRecvPacketReturn            types.RecvPacketResult
 17			OnTimeoutPacket               []Args
 18			OnTimeoutPacketReturn         error
 19			OnAcknowledgementPacket       []Args
 20			OnAcknowledgementPacketReturn error
 21		}
 22	}
 23	Args struct {
 24		SourceClient      string
 25		DestinationClient string
 26		Sequence          uint64
 27		Payload           types.Payload
 28		Ack               []byte
 29	}
 30)
 31
 32var (
 33	_    app.IBCApp = &App{}
 34	apps []*App
 35)
 36
 37func NewApp(_ realm) *App {
 38	app := &App{}
 39	// store the app in the realm is required to prevent the following panic:
 40	// "cannot directly modify readonly tainted object (w/o method)"
 41	// Other than that it is not used.
 42	apps = append(apps, app)
 43	return app
 44}
 45
 46// Implements app.IBCApp
 47func (a *App) OnSendPacket(
 48	_ realm,
 49	sourceClient string,
 50	destinationClient string,
 51	sequence uint64,
 52	payload types.Payload,
 53) error {
 54	a.Calls.OnSendPacket = append(a.Calls.OnSendPacket,
 55		Args{
 56			SourceClient:      sourceClient,
 57			DestinationClient: destinationClient,
 58			Sequence:          sequence,
 59			Payload:           payload,
 60		})
 61	return a.Calls.OnSendPacketReturn
 62}
 63
 64func (a *App) SetOnSendPacketReturn(err error) {
 65	a.Calls.OnSendPacketReturn = err
 66}
 67
 68// Implements app.IBCApp
 69func (a *App) OnRecvPacket(
 70	_ realm,
 71	sourceClient string,
 72	destinationClient string,
 73	sequence uint64,
 74	payload types.Payload,
 75) types.RecvPacketResult {
 76	a.Calls.OnRecvPacket = append(a.Calls.OnRecvPacket,
 77		Args{
 78			SourceClient:      sourceClient,
 79			DestinationClient: destinationClient,
 80			Sequence:          sequence,
 81			Payload:           payload,
 82		})
 83	// NOTE return a copy of the result to avoid annoying gno errors like
 84	// "illegal conversion of readonly or externally stored value"
 85	res := types.RecvPacketResult{
 86		Status:          a.Calls.OnRecvPacketReturn.Status,
 87		Acknowledgement: make([]byte, len(a.Calls.OnRecvPacketReturn.Acknowledgement)),
 88	}
 89	copy(res.Acknowledgement, a.Calls.OnRecvPacketReturn.Acknowledgement)
 90	return res
 91}
 92
 93func (a *App) SetOnRecvPacketReturn(res types.RecvPacketResult) {
 94	a.Calls.OnRecvPacketReturn = res
 95}
 96
 97// Implements app.IBCApp
 98func (a *App) OnTimeoutPacket(
 99	_ realm,
100	sourceClient string,
101	destinationClient string,
102	sequence uint64,
103	payload types.Payload,
104) error {
105	a.Calls.OnTimeoutPacket = append(a.Calls.OnTimeoutPacket,
106		Args{
107			SourceClient:      sourceClient,
108			DestinationClient: destinationClient,
109			Sequence:          sequence,
110			Payload:           payload,
111		})
112	return a.Calls.OnTimeoutPacketReturn
113}
114
115// Implements app.IBCApp
116func (a *App) OnAcknowledgementPacket(
117	_ realm,
118	sourceClient string,
119	destinationClient string,
120	sequence uint64,
121	acknowledgement []byte,
122	payload types.Payload,
123) error {
124	a.Calls.OnAcknowledgementPacket = append(a.Calls.OnAcknowledgementPacket,
125		Args{
126			SourceClient:      sourceClient,
127			DestinationClient: destinationClient,
128			Sequence:          sequence,
129			Payload:           payload,
130			Ack:               acknowledgement,
131		})
132	return a.Calls.OnAcknowledgementPacketReturn
133}
134
135func (a *App) Report() string {
136	var s string
137	s += ufmt.Sprintf("OnSendPacket (%d)\n", len(a.Calls.OnSendPacket))
138	for _, args := range a.Calls.OnSendPacket {
139		s += args.Report() + "\n"
140	}
141	s += ufmt.Sprintf("OnRecvPacket (%d)\n", len(a.Calls.OnRecvPacket))
142	for _, args := range a.Calls.OnRecvPacket {
143		s += args.Report() + "\n"
144	}
145	s += ufmt.Sprintf("OnTimeoutPacket (%d)\n", len(a.Calls.OnTimeoutPacket))
146	for _, args := range a.Calls.OnTimeoutPacket {
147		s += args.Report() + "\n"
148	}
149	s += ufmt.Sprintf("OnAcknowledgementPacket (%d)\n", len(a.Calls.OnAcknowledgementPacket))
150	for _, args := range a.Calls.OnAcknowledgementPacket {
151		s += args.Report() + "\n"
152	}
153	return s
154}
155
156func (a *Args) Report() string {
157	var s string
158	s += ufmt.Sprintf("- sourceClient: %s\n", a.SourceClient)
159	s += ufmt.Sprintf("- destinationClient: %s\n", a.DestinationClient)
160	s += ufmt.Sprintf("- sequence: %d\n", a.Sequence)
161	s += "- payload:\n"
162	s += ufmt.Sprintf("  - sourcePort: %s\n", a.Payload.SourcePort)
163	s += ufmt.Sprintf("  - destinationPort: %s\n", a.Payload.DestinationPort)
164	s += ufmt.Sprintf("  - version: %s\n", a.Payload.Version)
165	s += ufmt.Sprintf("  - encoding: %s\n", a.Payload.Encoding)
166	s += ufmt.Sprintf("  - value: %s\n", string(a.Payload.Value))
167	if len(a.Ack) > 0 {
168		s += ufmt.Sprintf("- ack: %s\n", string(a.Ack))
169	}
170	return s
171}