-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmessenger.go
278 lines (235 loc) · 7.72 KB
/
messenger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package async_geyser
import (
"context"
"slices"
"time"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
"github.com/code-payments/code-server/pkg/cache"
chat_util "github.com/code-payments/code-server/pkg/code/chat"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/account"
"github.com/code-payments/code-server/pkg/code/data/chat"
"github.com/code-payments/code-server/pkg/code/push"
"github.com/code-payments/code-server/pkg/code/thirdparty"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/kin"
push_lib "github.com/code-payments/code-server/pkg/push"
"github.com/code-payments/code-server/pkg/retry"
"github.com/code-payments/code-server/pkg/solana"
"github.com/code-payments/code-server/pkg/solana/memo"
"github.com/code-payments/code-server/pkg/solana/token"
)
var (
syncedMessageCache = cache.NewCache(1_000_000)
)
// This assumes a specific structure for the transaction:
// - Legacy Transaction Format
// - Instruction[0] = Memo containing encoded message
// - Instruction[1] = Fee payment to Code
// - Message fee payer = Account verified against domain
//
// Any deviation from the expectation, and the message (or parts of it) won't be processed.
func processPotentialBlockchainMessage(ctx context.Context, data code_data.Provider, pusher push_lib.Provider, feeCollector *common.Account, signature string) error {
decodedSignature, err := base58.Decode(signature)
if err != nil {
return errors.Wrap(err, "invalid signature")
}
var typedSignature solana.Signature
copy(typedSignature[:], decodedSignature)
if _, ok := syncedMessageCache.Retrieve(signature); ok {
return nil
}
err = func() error {
// Only wait for a confirmed transaction, so we can optimize for speed of
// message delivery. Obviously, we wouldn't want to do anything with the
// user payment instruction, which should be handled by the external deposit
// logic that waits for finalization.
var txn *solana.ConfirmedTransaction
var err error
_, err = retry.Retry(
func() error {
txn, err = data.GetBlockchainTransaction(ctx, signature, solana.CommitmentConfirmed)
return err
},
waitForConfirmationRetryStrategies...,
)
if err != nil {
return errors.Wrap(err, "error getting transaction")
}
if txn.Err != nil || txn.Meta.Err != nil {
return nil
}
if len(txn.Transaction.Message.Instructions) != 2 {
return nil
}
// Contains encoded message
memoIxn, err := memo.DecompileMemo(txn.Transaction.Message, 0)
if err != nil {
return nil
}
// Links to Code, which trivially enables a Geyser listener
payCodeFeeIxn, err := token.DecompileTransfer(txn.Transaction.Message, 1)
if err != nil {
return nil
} else if base58.Encode(payCodeFeeIxn.Destination) != feeCollector.PublicKey().ToBase58() {
return nil
}
feePayer, err := common.NewAccountFromPublicKeyBytes(payCodeFeeIxn.Owner)
if err != nil {
return nil
}
// Process the transaction if the minimum fee was paid
//
// todo: configurable
// todo: set the real value when known
if payCodeFeeIxn.Amount >= kin.ToQuarks(1) {
//
// Attempt to parse the blockchain message from the memo payload
//
if len(memoIxn.Data) == 0 {
return nil
}
blockchainMessage, err := thirdparty.DecodeNaclBoxBlockchainMessage(memoIxn.Data)
if err != nil {
return nil
}
//
// Verify domain name ownership
//
asciiBaseDomain, err := thirdparty.GetAsciiBaseDomain(blockchainMessage.SenderDomain)
if err != nil {
return nil
}
ownsDomain, err := thirdparty.VerifyDomainNameOwnership(ctx, feePayer, blockchainMessage.SenderDomain)
if err != nil || !ownsDomain {
// Third party being down should not affect progress of other
// critical systems like backup, so give up on this message
// and return nil.
return nil
}
//
// Check the receiving account has a relationship with the verified domain
//
accountInfoRecord, err := data.GetAccountInfoByAuthorityAddress(ctx, blockchainMessage.ReceiverAccount.PublicKey().ToBase58())
if err == account.ErrAccountInfoNotFound {
return nil
} else if err != nil {
return errors.Wrap(err, "error getting account info")
}
if accountInfoRecord.AccountType != commonpb.AccountType_RELATIONSHIP {
return nil
} else if *accountInfoRecord.RelationshipTo != asciiBaseDomain {
return nil
}
//
// Surface the message with a push and storing it into chat history
//
recipientOwner, err := common.NewAccountFromPublicKeyString(accountInfoRecord.OwnerAccount)
if err != nil {
return errors.Wrap(err, "invalid owner account")
}
blockTime := time.Now()
if txn.BlockTime != nil {
blockTime = *txn.BlockTime
}
chatMessage, err := chat_util.ToBlockchainMessage(signature, feePayer, blockchainMessage, blockTime)
if err != nil {
return errors.Wrap(err, "error creating proto message")
}
canPush, err := chat_util.SendChatMessage(
ctx,
data,
asciiBaseDomain,
chat.ChatTypeExternalApp,
true,
recipientOwner,
chatMessage,
false,
)
if err != nil && err != chat.ErrMessageAlreadyExists {
return errors.Wrap(err, "error persisting chat message")
}
if canPush {
// Best-effort send a push
push.SendChatMessagePushNotification(
ctx,
data,
pusher,
asciiBaseDomain,
recipientOwner,
chatMessage,
)
}
}
return nil
}()
if err == nil {
syncedMessageCache.Insert(signature, true, 1)
}
return err
}
// todo: strategy might need to change at a very large scale
// todo: will likely need to add some parallelism to message processing once we have a decent scale
func fixMissingBlockchainMessages(ctx context.Context, data code_data.Provider, pusher push_lib.Provider, feeCollector *common.Account, checkpoint *string) (*string, error) {
signatures, err := findPotentialBlockchainMessages(ctx, data, feeCollector, checkpoint)
if err != nil {
return checkpoint, errors.Wrap(err, "error finding potential messages")
}
// Oldest signatures first
slices.Reverse(signatures)
var anyError error
for _, signature := range signatures {
err := processPotentialBlockchainMessage(ctx, data, pusher, feeCollector, signature)
if err != nil {
anyError = errors.Wrap(err, "error processing signature for message")
}
if anyError == nil {
checkpoint = &signature
}
}
return checkpoint, anyError
}
func findPotentialBlockchainMessages(ctx context.Context, data code_data.Provider, feeCollector *common.Account, untilSignature *string) ([]string, error) {
var res []string
var cursor []byte
var totalTransactionsFound int
for {
history, err := data.GetBlockchainHistory(
ctx,
feeCollector.PublicKey().ToBase58(),
solana.CommitmentFinalized,
query.WithLimit(1_000), // Max supported
query.WithCursor(cursor),
)
if err != nil {
return nil, errors.Wrap(err, "error getting signatures for address")
}
if len(history) == 0 {
return res, nil
}
for _, historyItem := range history {
signature := base58.Encode(historyItem.Signature[:])
if untilSignature != nil && signature == *untilSignature {
return res, nil
}
// Transaction has an error, so skip it
if historyItem.Err != nil {
continue
}
res = append(res, signature)
// Bound total results
if len(res) >= 1_000 {
return res, nil
}
}
// Bound total history to look for in the past
totalTransactionsFound += len(history)
if totalTransactionsFound >= 10_000 {
return res, nil
}
cursor = query.Cursor(history[len(history)-1].Signature[:])
}
}