/
utils.go
203 lines (185 loc) · 5.08 KB
/
utils.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
/**
* 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 utils
import (
"encoding/json"
"fmt"
"os"
"time"
"google.golang.org/protobuf/proto"
)
func ProtoDeterministicMarshal(m proto.Message) ([]byte, error) {
b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
if err != nil {
return nil, err
}
retBytes := b
if b == nil {
retBytes = make([]byte, 0)
}
return retBytes, nil
}
func WriteEventLogTx(filename string, time time.Time, name string, function string, nonce string) {
createDirIfNotExist("event_log")
f, err := os.OpenFile("event_log/"+filename+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer f.Close()
var eventLog EventLogTx
eventLog.Datetime = time.UnixNano() / 1000000
eventLog.Name = name
eventLog.Function = function
eventLog.Nonce = nonce
eventLogJSON, err := json.Marshal(eventLog)
if err != nil {
fmt.Println("error:", err)
}
_, err = f.WriteString(string(eventLogJSON) + "\r\n")
if err != nil {
panic(err)
}
}
type EventLogTx struct {
Datetime int64 `json:"datetime"`
Name string `json:"name"`
Function string `json:"function"`
Nonce string `json:"nonce"`
}
func WriteEventLogBeginBlock(filename string, time time.Time, name string, height int64, numTxs int64) {
createDirIfNotExist("event_log")
f, err := os.OpenFile("event_log/"+filename+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer f.Close()
var eventLog EventLogBeginBlock
eventLog.Datetime = time.UnixNano() / 1000000
eventLog.Name = name
eventLog.Height = height
eventLog.NumTxs = numTxs
eventLogJSON, err := json.Marshal(eventLog)
if err != nil {
fmt.Println("error:", err)
}
_, err = f.WriteString(string(eventLogJSON) + "\r\n")
if err != nil {
panic(err)
}
}
type EventLogBeginBlock struct {
Datetime int64 `json:"datetime"`
Name string `json:"name"`
Height int64 `json:"height"`
NumTxs int64 `json:"numTxs"`
}
func WriteEventLog(filename string, time time.Time, name string) {
createDirIfNotExist("event_log")
f, err := os.OpenFile("event_log/"+filename+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer f.Close()
var eventLog EventLog
eventLog.Datetime = time.UnixNano() / 1000000
eventLog.Name = name
eventLogJSON, err := json.Marshal(eventLog)
if err != nil {
fmt.Println("error:", err)
}
_, err = f.WriteString(string(eventLogJSON) + "\r\n")
if err != nil {
panic(err)
}
}
type EventLog struct {
Datetime int64 `json:"datetime"`
Name string `json:"name"`
}
func WriteEventLogQuery(filename string, time time.Time, name string, function string) {
createDirIfNotExist("event_log")
f, err := os.OpenFile("event_log/"+filename+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer f.Close()
var eventLog EventLogQuery
eventLog.Datetime = time.UnixNano() / 1000000
eventLog.Name = name
eventLog.Function = function
eventLogJSON, err := json.Marshal(eventLog)
if err != nil {
fmt.Println("error:", err)
}
_, err = f.WriteString(string(eventLogJSON) + "\r\n")
if err != nil {
panic(err)
}
}
type EventLogQuery struct {
Datetime int64 `json:"datetime"`
Name string `json:"name"`
Function string `json:"function"`
}
func WriteDurationLog(filename string, durationTime int64, function string, optionalParams ...string) {
createDirIfNotExist("event_log")
f, err := os.OpenFile("event_log/"+filename+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer f.Close()
var methodName string
var nonceBase64 string
if len(optionalParams) > 0 {
if function == "CheckTx" {
nonceBase64 = optionalParams[1]
}
methodName = optionalParams[0]
}
var eventLog DurationLog
eventLog.Duration = durationTime
eventLog.Function = function
eventLog.Method = methodName
eventLog.Nonce = nonceBase64
eventLogJSON, err := json.Marshal(eventLog)
if err != nil {
fmt.Println("error:", err)
}
_, err = f.WriteString(string(eventLogJSON) + "\r\n")
if err != nil {
panic(err)
}
}
type DurationLog struct {
Duration int64 `json:"duration"`
Function string `json:"function"`
Method string `json:"method"`
Nonce string `json:"nonce"`
}
func createDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
}