-
Notifications
You must be signed in to change notification settings - Fork 22
/
app.go
419 lines (362 loc) · 11.7 KB
/
app.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact info@ndid.co.th for any further questions
*
*/
package app
import (
"crypto/sha256"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tm-db"
"google.golang.org/protobuf/proto"
"github.com/ndidplatform/smart-contract/v7/abci/code"
"github.com/ndidplatform/smart-contract/v7/abci/utils"
"github.com/ndidplatform/smart-contract/v7/abci/version"
protoTm "github.com/ndidplatform/smart-contract/v7/protos/tendermint"
)
type ABCIApplication struct {
types.BaseApplication
AppProtocolVersion uint64
CurrentChain string
Version string
checkTxNonceState *utils.StringByteArrayMap
deliverTxNonceState map[string][]byte
logger *logrus.Entry
state AppState
valUpdates map[string]types.ValidatorUpdate
verifiedSignatures *utils.StringMap
lastBlockTime time.Time
}
func NewABCIApplication(logger *logrus.Entry, db dbm.DB) *ABCIApplication {
defer func() {
if r := recover(); r != nil {
logger.Errorf("%s", identifyPanic())
panic(r)
}
}()
appState, err := NewAppState(db)
if err != nil {
panic(err)
}
ABCIVersion := version.Version
ABCIProtocolVersion := version.AppProtocolVersion
logger.Infof("Start ABCI version: %s", ABCIVersion)
return &ABCIApplication{
AppProtocolVersion: ABCIProtocolVersion,
Version: ABCIVersion,
checkTxNonceState: utils.NewStringByteArrayMap(),
deliverTxNonceState: make(map[string][]byte),
logger: logger,
state: *appState,
valUpdates: make(map[string]types.ValidatorUpdate),
verifiedSignatures: utils.NewStringMap(),
}
}
func (app *ABCIApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
var res types.ResponseInfo
res.Version = app.Version
res.LastBlockHeight = app.state.Height
res.LastBlockAppHash = app.state.AppHash
res.AppVersion = app.AppProtocolVersion
return res
}
// Save the validators in the merkle tree
func (app *ABCIApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
for _, v := range req.Validators {
r := app.updateValidator(v)
if r.IsErr() {
app.logger.Error("Error updating validators", "r", r)
}
}
return types.ResponseInitChain{}
}
// Track the block hash and header information
func (app *ABCIApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
app.logger.Infof("BeginBlock: %d, Chain ID: %s", req.Header.Height, req.Header.ChainID)
app.state.CurrentBlockHeight = req.Header.Height
app.CurrentChain = req.Header.ChainID
// reset valset changes
app.valUpdates = make(map[string]types.ValidatorUpdate, 0)
app.lastBlockTime = req.Header.Time
return types.ResponseBeginBlock{}
}
// Update the validator set
func (app *ABCIApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
app.logger.Infof("EndBlock: %d", req.Height)
valUpdates := make([]types.ValidatorUpdate, 0)
for _, newValidator := range app.valUpdates {
valUpdates = append(valUpdates, newValidator)
}
return types.ResponseEndBlock{ValidatorUpdates: valUpdates}
}
func (app *ABCIApplication) DeliverTx(req types.RequestDeliverTx) (res types.ResponseDeliverTx) {
// Recover when panic
defer func() {
if r := recover(); r != nil {
app.logger.Errorf("Recovered in %s, %s", r, identifyPanic())
res = app.ReturnDeliverTxLog(code.UnknownError, "Unknown error", "")
}
}()
var txObj protoTm.Tx
err := proto.Unmarshal(req.Tx, &txObj)
if err != nil {
app.logger.Error(err.Error())
}
method := txObj.Method
param := txObj.Params
nonce := txObj.Nonce
signature := txObj.Signature
nodeID := txObj.NodeId
go recordDeliverTxMetrics(method)
startTime := time.Now()
defer func() {
duration := time.Since(startTime)
go recordDeliverTxDurationMetrics(duration, method)
}()
// ---- Check duplicate nonce ----
nonceDup := app.isDuplicateNonce(nonce)
if nonceDup {
go recordDeliverTxFailMetrics(method)
return app.ReturnDeliverTxLog(code.DuplicateNonce, "Duplicate nonce", "")
}
app.logger.Infof("DeliverTx: %s, NodeID: %s", method, nodeID)
if method == "" {
go recordDeliverTxFailMetrics(method)
return app.ReturnDeliverTxLog(code.MethodCanNotBeEmpty, "method can not be empty", "")
}
// Check signature
publicKey, retCode, retLog := app.getNodePublicKeyForSignatureVerification(method, param, nodeID, false)
if retCode != code.OK {
go recordDeliverTxFailMetrics(method)
return app.ReturnDeliverTxLog(retCode, retLog, "")
}
verifiedSignatureKey := string(signature) + "|" + nodeID
verifiedSigNodePubKey, verifiedSigResultExist := app.verifiedSignatures.Load(verifiedSignatureKey)
if verifiedSigResultExist {
app.logger.Debugf("Found cached verified Tx signature result")
app.verifiedSignatures.Delete(verifiedSignatureKey)
if verifiedSigNodePubKey != publicKey {
app.logger.Debugf("Node key updated, cached verified Tx signature result is no longer valid")
go recordDeliverTxFailMetrics(method)
return app.ReturnDeliverTxLog(code.VerifySignatureError, err.Error(), "")
}
} else {
app.logger.Debugf("Cached verified Tx signature result could not be found")
app.logger.Debugf("Verifying Tx signature")
verifyResult, err := verifySignature(param, nonce, signature, publicKey, method)
if err != nil {
go recordDeliverTxFailMetrics(method)
return app.ReturnDeliverTxLog(code.VerifySignatureError, err.Error(), "")
}
if verifyResult == false {
go recordDeliverTxFailMetrics(method)
return app.ReturnDeliverTxLog(code.VerifySignatureError, "Invalid Tx signature", "")
}
}
result := app.DeliverTxRouter(method, param, nonce, signature, nodeID)
app.logger.Infof(
`DeliverTx response: {"code":%d,"log":"%s","attributes":[{"key":"%s","value":"%s"}]}`,
result.Code,
result.Log,
string(result.Events[0].Attributes[0].Key), string(result.Events[0].Attributes[0].Value),
)
if result.Code != code.OK {
go recordDeliverTxFailMetrics(method)
}
return result
}
func (app *ABCIApplication) CheckTx(req types.RequestCheckTx) (res types.ResponseCheckTx) {
// Recover when panic
defer func() {
if r := recover(); r != nil {
app.logger.Errorf("Recovered in %s, %s", r, identifyPanic())
res = ReturnCheckTx(code.UnknownError, "Unknown error")
}
}()
var txObj protoTm.Tx
err := proto.Unmarshal(req.Tx, &txObj)
if err != nil {
app.logger.Error(err.Error())
}
method := txObj.Method
param := txObj.Params
nonce := txObj.Nonce
signature := txObj.Signature
nodeID := txObj.NodeId
go recordCheckTxMetrics(method)
startTime := time.Now()
defer func() {
duration := time.Since(startTime)
go recordCheckTxDurationMetrics(duration, method)
}()
// ---- Check duplicate nonce ----
nonceDup := app.isDuplicateNonce(nonce)
if nonceDup {
res.Code = code.DuplicateNonce
res.Log = "Duplicate nonce"
go recordCheckTxFailMetrics(method)
return res
}
// Check duplicate nonce in checkTx state
nonceStr := string(nonce)
_, exist := app.checkTxNonceState.Load(nonceStr)
if !exist {
app.checkTxNonceState.Store(nonceStr, []byte(nil))
} else {
res.Code = code.DuplicateNonce
res.Log = "Duplicate nonce"
go recordCheckTxFailMetrics(method)
return res
}
app.logger.Infof("CheckTx: %s, NodeID: %s", method, nodeID)
if method == "" || param == "" || nonce == nil || signature == nil || nodeID == "" {
res.Code = code.InvalidTransactionFormat
res.Log = "Invalid transaction format"
go recordCheckTxFailMetrics(method)
return res
}
// Check has function in system
if !IsMethod[method] {
res.Code = code.UnknownMethod
res.Log = "Unknown method name"
go recordCheckTxFailMetrics(method)
return res
}
// Check signature
publicKey, retCode, retLog := app.getNodePublicKeyForSignatureVerification(method, param, nodeID, true)
if retCode != code.OK {
return ReturnCheckTx(retCode, retLog)
}
verifyResult, err := verifySignature(param, nonce, signature, publicKey, method)
if err != nil {
go recordCheckTxFailMetrics(method)
return ReturnCheckTx(code.VerifySignatureError, err.Error())
}
if verifyResult == false {
go recordCheckTxFailMetrics(method)
return ReturnCheckTx(code.VerifySignatureError, "Invalid Tx signature")
}
verifiedSignatureKey := string(signature) + "|" + nodeID
app.verifiedSignatures.Store(verifiedSignatureKey, publicKey)
result := app.CheckTxRouter(method, param, nonce, signature, nodeID, true)
if result.Code != code.OK {
app.verifiedSignatures.Delete(verifiedSignatureKey)
go recordCheckTxFailMetrics(method)
}
return result
}
func hash(data []byte) []byte {
sum := sha256.Sum256(data)
return sum[:]
}
func (app *ABCIApplication) Commit() types.ResponseCommit {
startTime := time.Now()
app.logger.Infof("Commit")
app.state.Save()
app.state.Height = app.state.Height + 1
dbSaveDuration := time.Since(startTime)
go recordDBSaveDurationMetrics(dbSaveDuration)
for key := range app.deliverTxNonceState {
app.checkTxNonceState.Delete(key)
}
app.deliverTxNonceState = make(map[string][]byte)
appHashStartTime := time.Now()
// Calculate app hash
if len(app.state.HashData) > 0 {
app.state.HashData = append(app.state.AppHash, app.state.HashData...)
app.state.AppHash = hash(app.state.HashData)
}
appHash := app.state.AppHash
appHashDuration := time.Since(appHashStartTime)
go recordAppHashDurationMetrics(appHashDuration)
app.state.HashData = make([]byte, 0)
// Save state
app.state.SaveMetadata()
duration := time.Since(startTime)
go recordCommitDurationMetrics(duration)
return types.ResponseCommit{Data: appHash}
}
func (app *ABCIApplication) Query(reqQuery types.RequestQuery) (res types.ResponseQuery) {
// Recover when panic
defer func() {
if r := recover(); r != nil {
app.logger.Errorf("Recovered in %s, %s", r, identifyPanic())
res = app.ReturnQuery(nil, "Unknown error", app.state.Height)
}
}()
var query protoTm.Query
err := proto.Unmarshal(reqQuery.Data, &query)
if err != nil {
app.logger.Error(err.Error())
}
method := query.Method
param := query.Params
startTime := time.Now()
go recordQueryMetrics(method)
defer func() {
duration := time.Since(startTime)
go recordQueryDurationMetrics(duration, method)
}()
app.logger.Infof("Query: %s", method)
height := reqQuery.Height
if height == 0 {
height = app.state.Height
}
if method == "" {
return app.ReturnQuery(nil, "method can't be empty", app.state.Height)
}
return app.QueryRouter(method, param, height)
}
func getEnv(key, defaultValue string) string {
value, exists := os.LookupEnv(key)
if !exists {
value = defaultValue
}
return value
}
func identifyPanic() string {
var name, file string
var line int
var pc [16]uintptr
n := runtime.Callers(3, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
break
}
}
switch {
case name != "":
return fmt.Sprintf("%v:%v", name, line)
case file != "":
return fmt.Sprintf("%v:%v", file, line)
}
return fmt.Sprintf("pc:%x", pc)
}