-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandler.go
188 lines (155 loc) · 5.75 KB
/
handler.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
package async_geyser
import (
"bytes"
"context"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
geyserpb "github.com/code-payments/code-server/pkg/code/async/geyser/api/gen"
"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/kin"
push_lib "github.com/code-payments/code-server/pkg/push"
splitter_token "github.com/code-payments/code-server/pkg/solana/splitter"
timelock_token_v1 "github.com/code-payments/code-server/pkg/solana/timelock/v1"
"github.com/code-payments/code-server/pkg/solana/token"
)
var (
ErrUnexpectedProgramOwner = errors.New("unexpected program owner")
)
type ProgramAccountUpdateHandler interface {
// Handle handles account updates from Geyser. Updates are not guaranteed
// to come in order. Implementations must be idempotent and should not
// trust the account data passed in. Always refer to finalized blockchain
// state from another RPC provider.
Handle(ctx context.Context, update *geyserpb.AccountUpdate) error
}
type TokenProgramAccountHandler struct {
conf *conf
data code_data.Provider
pusher push_lib.Provider
}
func NewTokenProgramAccountHandler(conf *conf, data code_data.Provider, pusher push_lib.Provider) ProgramAccountUpdateHandler {
return &TokenProgramAccountHandler{
conf: conf,
data: data,
pusher: pusher,
}
}
func (h *TokenProgramAccountHandler) Handle(ctx context.Context, update *geyserpb.AccountUpdate) error {
if !bytes.Equal(update.Owner, token.ProgramKey) {
return ErrUnexpectedProgramOwner
}
// We need to know the amount being deposited, and that's impossible without
// a transaction signature.
if update.TxSignature == nil {
return nil
}
// We need to know more about the account before accessing our data stores,
// so skip anything that doesn't have data. I'm assuming this means the account
// is closed anyways.
if len(update.Data) == 0 {
return nil
}
var unmarshalled token.Account
if !unmarshalled.Unmarshal(update.Data) {
// Probably not a token account (eg. mint)
return nil
}
// Not a Kin account, so filter it out
if !bytes.Equal(unmarshalled.Mint, kin.TokenMint) {
return nil
}
kinTokenAccount, err := common.NewAccountFromPublicKeyBytes(update.Pubkey)
if err != nil {
return errors.Wrap(err, "invalid token account")
}
if kinTokenAccount.PublicKey().ToBase58() == h.conf.messagingFeeCollectorPublicKey.Get(ctx) {
return processPotentialBlockchainMessage(
ctx,
h.data,
h.pusher,
kinTokenAccount,
*update.TxSignature,
)
}
// Not a program vault account, so filter it out
if !bytes.Equal(update.Pubkey, unmarshalled.Owner) {
return nil
}
// Account is empty, and all we care about are external deposits, so filter it out
if unmarshalled.Amount == 0 {
return nil
}
isCodeAccount, err := testForKnownCodeUserAccount(ctx, h.data, kinTokenAccount)
if err != nil {
return errors.Wrap(err, "error testing for known account")
} else if !isCodeAccount {
// Not an account we track, so skip the update
return nil
}
return processPotentialExternalDeposit(ctx, h.data, h.pusher, *update.TxSignature, kinTokenAccount)
}
type TimelockV1ProgramAccountHandler struct {
data code_data.Provider
}
func NewTimelockV1ProgramAccountHandler(data code_data.Provider) ProgramAccountUpdateHandler {
return &TimelockV1ProgramAccountHandler{
data: data,
}
}
func (h *TimelockV1ProgramAccountHandler) Handle(ctx context.Context, update *geyserpb.AccountUpdate) error {
if !bytes.Equal(update.Owner, timelock_token_v1.PROGRAM_ID) {
return ErrUnexpectedProgramOwner
}
if len(update.Data) > 0 {
var unmarshalled timelock_token_v1.TimelockAccount
err := unmarshalled.Unmarshal(update.Data)
if err != nil {
return errors.Wrap(err, "error unmarshalling account data from update")
}
// Not a Kin account, so filter it out
if !bytes.Equal(unmarshalled.Mint, kin.TokenMint) {
return nil
}
// Not managed by Code, so filter it out
if !bytes.Equal(unmarshalled.TimeAuthority, common.GetSubsidizer().PublicKey().ToBytes()) {
return nil
}
// Account is locked, so filter it out. Scheduler success handlers ensure
// we properly update state from unknown to locked state. We don't care if
// the account remains locked. We're really just interested in external
// unlocks. Skip the update to reduce load.
if unmarshalled.VaultState == timelock_token_v1.StateLocked {
return nil
}
}
stateAccount, err := common.NewAccountFromPublicKeyBytes(update.Pubkey)
if err != nil {
return errors.Wrap(err, "invalid state account")
}
// Go out to the blockchain to fetch finalized account state. Don't trust the update.
return updateTimelockV1AccountCachedState(ctx, h.data, stateAccount, update.Slot)
}
type SplitterProgramAccountHandler struct {
data code_data.Provider
}
func NewSplitterProgramAccountHandler(data code_data.Provider) ProgramAccountUpdateHandler {
return &SplitterProgramAccountHandler{
data: data,
}
}
func (h *SplitterProgramAccountHandler) Handle(ctx context.Context, update *geyserpb.AccountUpdate) error {
if !bytes.Equal(update.Owner, splitter_token.PROGRAM_ID) {
return ErrUnexpectedProgramOwner
}
// Nothing to do, we don't care about updates here yet. Everything is handled
// externally atm in the fulfillment and treasury workers.
return nil
}
func initializeProgramAccountUpdateHandlers(conf *conf, data code_data.Provider, pusher push_lib.Provider) map[string]ProgramAccountUpdateHandler {
return map[string]ProgramAccountUpdateHandler{
base58.Encode(token.ProgramKey): NewTokenProgramAccountHandler(conf, data, pusher),
base58.Encode(timelock_token_v1.PROGRAM_ID): NewTimelockV1ProgramAccountHandler(data),
base58.Encode(splitter_token.PROGRAM_ID): NewSplitterProgramAccountHandler(data),
}
}