-
Notifications
You must be signed in to change notification settings - Fork 43
/
emitter.go
351 lines (327 loc) · 12.1 KB
/
emitter.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package emitter
import (
"context"
"encoding/json"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/segmentio/kafka-go"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
bandapp "github.com/bandprotocol/chain/app"
"github.com/bandprotocol/chain/hooks/common"
"github.com/bandprotocol/chain/x/oracle"
"github.com/bandprotocol/chain/x/oracle/keeper"
"github.com/bandprotocol/chain/x/oracle/types"
)
// Hook uses Kafka functionality to act as an event producer for all events in the blockchains.
type Hook struct {
cdc *codec.Codec
txDecoder sdk.TxDecoder
// Main Kafka writer instance.
writer *kafka.Writer
// Temporary variables that are reset on every block.
accsInBlock map[string]bool // The accounts that need balance update at the end of block.
accsInTx map[string]bool // The accounts related to the current processing transaction.
msgs []common.Message // The list of all messages to publish for this block.
emitStartState bool // If emitStartState is true will emit all non historical state to Kafka
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
supplyKeeper supply.Keeper
stakingKeeper staking.Keeper
mintKeeper mint.Keeper
distrKeeper distr.Keeper
govKeeper gov.Keeper
oracleKeeper oracle.Keeper
}
// NewHook creates an emitter hook instance that will be added in Band App.
func NewHook(
cdc *codec.Codec, accountKeeper auth.AccountKeeper, bankKeeper bank.Keeper, supplyKeeper supply.Keeper,
stakingKeeper staking.Keeper, mintKeeper mint.Keeper, distrKeeper distr.Keeper, govKeeper gov.Keeper,
oracleKeeper keeper.Keeper, kafkaURI string, emitStartState bool,
) *Hook {
paths := strings.SplitN(kafkaURI, "@", 2)
return &Hook{
cdc: cdc,
txDecoder: auth.DefaultTxDecoder(cdc),
writer: kafka.NewWriter(kafka.WriterConfig{
Brokers: paths[1:],
Topic: paths[0],
Balancer: &kafka.LeastBytes{},
BatchTimeout: 1 * time.Millisecond,
// Async: true, // TODO: We may be able to enable async mode on replay
}),
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
supplyKeeper: supplyKeeper,
stakingKeeper: stakingKeeper,
mintKeeper: mintKeeper,
distrKeeper: distrKeeper,
govKeeper: govKeeper,
oracleKeeper: oracleKeeper,
emitStartState: emitStartState,
}
}
// AddAccountsInBlock adds the given accounts to the list of accounts to update balances end-of-block.
func (h *Hook) AddAccountsInBlock(accs ...sdk.AccAddress) {
for _, acc := range accs {
h.accsInBlock[acc.String()] = true
}
}
// AddAccountsInTx adds the given accounts to the list of accounts to track related account in transaction.
func (h *Hook) AddAccountsInTx(accs ...sdk.AccAddress) {
for _, acc := range accs {
h.accsInTx[acc.String()] = true
}
}
// Write adds the given key-value pair to the list of messages to publish during Commit.
func (h *Hook) Write(key string, val common.JsDict) {
h.msgs = append(h.msgs, common.Message{Key: key, Value: val})
}
// FlushMessages publishes all pending messages to Kafka. Blocks until completion.
func (h *Hook) FlushMessages() {
kafkaMsgs := make([]kafka.Message, len(h.msgs))
for idx, msg := range h.msgs {
res, _ := json.Marshal(msg.Value) // Error must always be nil.
kafkaMsgs[idx] = kafka.Message{Key: []byte(msg.Key), Value: res}
}
err := h.writer.WriteMessages(context.Background(), kafkaMsgs...)
if err != nil {
panic(err)
}
}
// AfterInitChain specify actions need to do after chain initialization (app.Hook interface).
func (h *Hook) AfterInitChain(ctx sdk.Context, req abci.RequestInitChain, res abci.ResponseInitChain) {
var genesisState bandapp.GenesisState
h.cdc.MustUnmarshalJSON(req.AppStateBytes, &genesisState)
// Auth module
var genaccountsState auth.GenesisState
auth.ModuleCdc.MustUnmarshalJSON(genesisState[auth.ModuleName], &genaccountsState)
for _, account := range genaccountsState.Accounts {
h.Write("SET_ACCOUNT", common.JsDict{
"address": account.GetAddress(),
"balance": h.bankKeeper.GetCoins(ctx, account.GetAddress()).String(),
})
}
// GenUtil module for create validator genesis transactions.
var genutilState genutil.GenesisState
h.cdc.MustUnmarshalJSON(genesisState[genutil.ModuleName], &genutilState)
for _, genTx := range genutilState.GenTxs {
var tx auth.StdTx
h.cdc.MustUnmarshalJSON(genTx, &tx)
for _, msg := range tx.Msgs {
if msg, ok := msg.(staking.MsgCreateValidator); ok {
h.handleMsgCreateValidator(ctx, msg)
}
}
}
// Staking module
var stakingState staking.GenesisState
h.cdc.MustUnmarshalJSON(genesisState[staking.ModuleName], &stakingState)
for _, val := range stakingState.Validators {
h.emitSetValidator(ctx, val.OperatorAddress)
}
for _, del := range stakingState.Delegations {
h.emitDelegation(ctx, del.ValidatorAddress, del.DelegatorAddress)
}
for _, unbonding := range stakingState.UnbondingDelegations {
for _, entry := range unbonding.Entries {
h.Write("NEW_UNBONDING_DELEGATION", common.JsDict{
"delegator_address": unbonding.DelegatorAddress,
"operator_address": unbonding.ValidatorAddress,
"completion_time": entry.CompletionTime.UnixNano(),
"amount": entry.Balance,
})
}
}
for _, redelegate := range stakingState.Redelegations {
for _, entry := range redelegate.Entries {
h.Write("NEW_REDELEGATION", common.JsDict{
"delegator_address": redelegate.DelegatorAddress,
"operator_src_address": redelegate.ValidatorSrcAddress,
"operator_dst_address": redelegate.ValidatorDstAddress,
"completion_time": entry.CompletionTime.UnixNano(),
"amount": entry.InitialBalance,
})
}
}
// Gov module
var govState gov.GenesisState
h.cdc.MustUnmarshalJSON(genesisState[gov.ModuleName], &govState)
for _, proposal := range govState.Proposals {
h.Write("NEW_PROPOSAL", common.JsDict{
"id": proposal.ProposalID,
"proposer": nil,
"type": proposal.ProposalType(),
"title": proposal.Content.GetTitle(),
"description": proposal.Content.GetDescription(),
"proposal_route": proposal.Content.ProposalRoute(),
"status": int(proposal.Status),
"submit_time": proposal.SubmitTime.UnixNano(),
"deposit_end_time": proposal.DepositEndTime.UnixNano(),
"total_deposit": proposal.TotalDeposit.String(),
"voting_time": proposal.VotingStartTime.UnixNano(),
"voting_end_time": proposal.VotingEndTime.UnixNano(),
})
}
for _, deposit := range govState.Deposits {
h.Write("SET_DEPOSIT", common.JsDict{
"proposal_id": deposit.ProposalID,
"depositor": deposit.Depositor,
"amount": deposit.Amount.String(),
"tx_hash": nil,
})
}
for _, vote := range govState.Votes {
h.Write("SET_VOTE", common.JsDict{
"proposal_id": vote.ProposalID,
"voter": vote.Voter,
"answer": int(vote.Option),
"tx_hash": nil,
})
}
// Oracle module
var oracleState oracle.GenesisState
h.cdc.MustUnmarshalJSON(genesisState[oracle.ModuleName], &oracleState)
for idx, ds := range oracleState.DataSources {
h.emitSetDataSource(types.DataSourceID(idx+1), ds, nil)
}
for idx, os := range oracleState.OracleScripts {
h.emitSetOracleScript(types.OracleScriptID(idx+1), os, nil)
}
h.Write("COMMIT", common.JsDict{"height": 0})
h.FlushMessages()
}
func (h *Hook) emitNonHistoricalState(ctx sdk.Context) {
h.emitAuthModule(ctx)
h.emitStakingModule(ctx)
h.emitGovModule(ctx)
h.emitOracleModule(ctx)
h.Write("COMMIT", common.JsDict{"height": -1})
h.FlushMessages()
h.msgs = []common.Message{}
}
// AfterBeginBlock specify actions need to do after begin block period (app.Hook interface).
func (h *Hook) AfterBeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, res abci.ResponseBeginBlock) {
h.accsInBlock = make(map[string]bool)
h.accsInTx = make(map[string]bool)
h.msgs = []common.Message{}
if h.emitStartState {
h.emitStartState = false
h.emitNonHistoricalState(ctx)
} else {
for _, val := range req.GetLastCommitInfo().Votes {
validator := h.stakingKeeper.ValidatorByConsAddr(ctx, val.GetValidator().Address)
h.Write("NEW_VALIDATOR_VOTE", common.JsDict{
"consensus_address": validator.GetConsAddr().String(),
"block_height": req.Header.GetHeight() - 1,
"voted": val.GetSignedLastBlock(),
})
h.emitUpdateValidatorRewardAndAccumulatedCommission(ctx, validator.GetOperator())
}
}
h.Write("NEW_BLOCK", common.JsDict{
"height": req.Header.GetHeight(),
"timestamp": ctx.BlockTime().UnixNano(),
"proposer": sdk.ConsAddress(req.Header.GetProposerAddress()).String(),
"hash": req.GetHash(),
"inflation": h.mintKeeper.GetMinter(ctx).Inflation.String(),
"supply": h.supplyKeeper.GetSupply(ctx).GetTotal().String(),
})
for _, event := range res.Events {
h.handleBeginBlockEndBlockEvent(ctx, event)
}
}
// AfterDeliverTx specify actions need to do after transaction has been processed (app.Hook interface).
func (h *Hook) AfterDeliverTx(ctx sdk.Context, req abci.RequestDeliverTx, res abci.ResponseDeliverTx) {
if ctx.BlockHeight() == 0 {
return
}
h.accsInTx = make(map[string]bool)
tx, err := h.txDecoder(req.Tx)
if err != nil {
return
}
stdTx, ok := tx.(auth.StdTx)
if !ok {
return
}
txHash := tmhash.Sum(req.Tx)
var errMsg *string
if !res.IsOK() {
errMsg = &res.Log
}
txDict := common.JsDict{
"hash": txHash,
"block_height": ctx.BlockHeight(),
"gas_used": res.GasUsed,
"gas_limit": stdTx.Fee.Gas,
"gas_fee": stdTx.Fee.Amount.String(),
"err_msg": errMsg,
"sender": stdTx.GetSigners()[0].String(),
"success": res.IsOK(),
"memo": stdTx.Memo,
}
// NOTE: We add txDict to the list of pending Kafka messages here, but it will still be
// mutated in the loop below as we know the messages won't get flushed until ABCI Commit.
h.Write("NEW_TRANSACTION", txDict)
logs, _ := sdk.ParseABCILogs(res.Log) // Error must always be nil if res.IsOK is true.
messages := []map[string]interface{}{}
for idx, msg := range tx.GetMsgs() {
var extra = make(common.JsDict)
if res.IsOK() {
h.handleMsg(ctx, txHash, msg, logs[idx], extra)
}
messages = append(messages, common.JsDict{
"msg": msg,
"type": msg.Type(),
"extra": extra,
})
}
h.AddAccountsInTx(stdTx.GetSigners()...)
relatedAccounts := make([]sdk.AccAddress, 0, len(h.accsInBlock))
for accStr := range h.accsInTx {
acc, _ := sdk.AccAddressFromBech32(accStr)
relatedAccounts = append(relatedAccounts, acc)
}
txDict["related_accounts"] = relatedAccounts
h.AddAccountsInBlock(relatedAccounts...)
txDict["messages"] = messages
}
// AfterEndBlock specify actions need to do after end block period (app.Hook interface).
func (h *Hook) AfterEndBlock(ctx sdk.Context, req abci.RequestEndBlock, res abci.ResponseEndBlock) {
for _, event := range res.Events {
h.handleBeginBlockEndBlockEvent(ctx, event)
}
// Update balances of all affected accounts on this block.
// Index 0 is message NEW_BLOCK, we insert SET_ACCOUNT messages right after it.
modifiedMsgs := []common.Message{h.msgs[0]}
for accStr := range h.accsInBlock {
acc, _ := sdk.AccAddressFromBech32(accStr)
modifiedMsgs = append(modifiedMsgs, common.Message{
Key: "SET_ACCOUNT",
Value: common.JsDict{
"address": acc,
"balance": h.bankKeeper.GetCoins(ctx, acc).String(),
}})
}
h.msgs = append(modifiedMsgs, h.msgs[1:]...)
h.Write("COMMIT", common.JsDict{"height": req.Height})
}
// ApplyQuery catch the custom query that matches specific paths (app.Hook interface).
func (h *Hook) ApplyQuery(req abci.RequestQuery) (res abci.ResponseQuery, stop bool) {
return abci.ResponseQuery{}, false
}
// BeforeCommit specify actions need to do before commit block (app.Hook interface).
func (h *Hook) BeforeCommit() {
h.FlushMessages()
}