-
Notifications
You must be signed in to change notification settings - Fork 93
/
utils.go
525 lines (470 loc) · 14.7 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program 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 GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package utils
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
"github.com/AplaProject/go-apla/packages/conf"
"github.com/AplaProject/go-apla/packages/conf/syspar"
"github.com/AplaProject/go-apla/packages/consts"
"github.com/AplaProject/go-apla/packages/converter"
"github.com/AplaProject/go-apla/packages/crypto"
"github.com/AplaProject/go-apla/packages/model"
uuid "github.com/satori/go.uuid"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/theckman/go-flock"
)
const (
firstBlock = 1
minBlockSize = 9
)
var ErrBlockSize = errors.New("Bad block size")
// BlockData is a structure of the block's header
type BlockData struct {
BlockID int64
Time int64
EcosystemID int64
KeyID int64
NodePosition int64
Sign []byte
Hash []byte
RollbacksHash []byte
Version int
PrivateBlockchain bool
}
func (b BlockData) String() string {
return fmt.Sprintf("BlockID:%d, Time:%d, NodePosition %d", b.BlockID, b.Time, b.NodePosition)
}
func blockVer(cur, prev *BlockData) (ret string) {
if cur.Version >= consts.BV_ROLLBACK_HASH {
ret = fmt.Sprintf(",%x", prev.RollbacksHash)
}
return
}
func (b BlockData) ForSha(prev *BlockData, mrklRoot []byte) string {
return fmt.Sprintf("%d,%x,%s,%d,%d,%d,%d",
b.BlockID, prev.Hash, mrklRoot, b.Time, b.EcosystemID, b.KeyID, b.NodePosition) +
blockVer(&b, prev)
}
// ForSign from 128 bytes to 512 bytes. Signature of TYPE, BLOCK_ID, PREV_BLOCK_HASH, TIME, WALLET_ID, state_id, MRKL_ROOT
func (b BlockData) ForSign(prev *BlockData, mrklRoot []byte) string {
return fmt.Sprintf("0,%v,%x,%v,%v,%v,%v,%s",
b.BlockID, prev.Hash, b.Time, b.EcosystemID, b.KeyID, b.NodePosition, mrklRoot) +
blockVer(&b, prev)
}
// ParseBlockHeader is parses block header
func ParseBlockHeader(buf *bytes.Buffer) (header, prev BlockData, err error) {
if buf.Len() < minBlockSize {
err = ErrBlockSize
return
}
header.Version = int(converter.BinToDec(buf.Next(2)))
header.BlockID = converter.BinToDec(buf.Next(4))
header.Time = converter.BinToDec(buf.Next(4))
header.EcosystemID = converter.BinToDec(buf.Next(4))
header.KeyID, err = converter.DecodeLenInt64Buf(buf)
if err != nil {
return
}
header.NodePosition = converter.BinToDec(buf.Next(1))
// for version of block with included the rollback hash
if header.Version >= consts.BV_INCLUDE_ROLLBACK_HASH {
prev.RollbacksHash, err = converter.DecodeBytesBuf(buf)
if err != nil {
return
}
}
if header.BlockID == firstBlock {
buf.Next(1)
return
}
if int64(buf.Len()) > syspar.GetMaxBlockSize() {
err = ErrBlockSize
return
}
header.Sign, err = converter.DecodeBytesBuf(buf)
if err != nil {
return
}
return
}
var (
// ReturnCh is chan for returns
ReturnCh chan string
// CancelFunc is represents cancel func
CancelFunc context.CancelFunc
// DaemonsCount is number of daemons
DaemonsCount int
)
// GetHTTPTextAnswer returns HTTP answer as a string
func GetHTTPTextAnswer(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError, "url": url}).Error("cannot get url")
return "", err
}
defer resp.Body.Close()
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError}).Error("cannot read response body")
return "", err
}
if resp.StatusCode == 404 {
err = fmt.Errorf(`404`)
}
return string(htmlData), err
}
// ErrInfoFmt fomats the error message
func ErrInfoFmt(err string, a ...interface{}) error {
return fmt.Errorf("%s (%s)", fmt.Sprintf(err, a...), Caller(1))
}
// ErrInfo formats the error message
func ErrInfo(verr interface{}, additionally ...string) error {
var err error
switch verr.(type) {
case error:
err = verr.(error)
case string:
err = errors.New(verr.(string))
}
if err != nil {
if len(additionally) > 0 {
return fmt.Errorf("%s # %s (%s)", err, additionally, Caller(1))
}
return fmt.Errorf("%s (%s)", err, Caller(1))
}
return err
}
// CallMethod calls the function by its name
func CallMethod(i interface{}, methodName string) interface{} {
var ptr reflect.Value
var value reflect.Value
var finalMethod reflect.Value
value = reflect.ValueOf(i)
// if we start with a pointer, we need to get value pointed to
// if we start with a value, we need to get a pointer to that value
if value.Type().Kind() == reflect.Ptr {
ptr = value
value = ptr.Elem()
} else {
ptr = reflect.New(reflect.TypeOf(i))
temp := ptr.Elem()
temp.Set(value)
}
// check for method on value
method := value.MethodByName(methodName)
if method.IsValid() {
finalMethod = method
}
// check for method on pointer
method = ptr.MethodByName(methodName)
if method.IsValid() {
finalMethod = method
}
if finalMethod.IsValid() {
return finalMethod.Call([]reflect.Value{})[0].Interface()
}
// return or panic, method not found of either type
log.WithFields(log.Fields{"method_name": methodName, "type": consts.NotFound}).Error("method not found")
return fmt.Errorf("method %s not found", methodName)
}
// Caller returns the name of the latest function
func Caller(steps int) string {
name := "?"
if pc, _, num, ok := runtime.Caller(steps + 1); ok {
name = fmt.Sprintf("%s : %d", filepath.Base(runtime.FuncForPC(pc).Name()), num)
}
return name
}
// CopyFileContents copy files
func CopyFileContents(src, dst string) error {
in, err := os.Open(src)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError, "file_name": src}).Error("opening file")
return ErrInfo(err)
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError, "file_name": dst}).Error("creating file")
return ErrInfo(err)
}
defer func() {
cerr := out.Close()
if err == nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError, "file_name": dst}).Error("closing file")
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError, "from_file": src, "to_file": dst}).Error("copying from to")
return ErrInfo(err)
}
err = out.Sync()
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError, "file_name": dst}).Error("syncing file")
}
return ErrInfo(err)
}
// CheckSign checks the signature
func CheckSign(publicKeys [][]byte, forSign []byte, signs []byte, nodeKeyOrLogin bool) (bool, error) {
defer func() {
if r := recover(); r != nil {
log.WithFields(log.Fields{"type": consts.PanicRecoveredError, "error": r}).Error("recovered panic in check sign")
}
}()
var signsSlice [][]byte
if len(forSign) == 0 {
log.WithFields(log.Fields{"type": consts.EmptyObject}).Error("for sign is empty")
return false, ErrInfoFmt("len(forSign) == 0")
}
if len(publicKeys) == 0 {
log.WithFields(log.Fields{"type": consts.EmptyObject}).Error("public keys is empty")
return false, ErrInfoFmt("len(publicKeys) == 0")
}
if len(signs) == 0 {
log.WithFields(log.Fields{"type": consts.EmptyObject}).Error("signs is empty")
return false, ErrInfoFmt("len(signs) == 0")
}
// node always has only one signature
if nodeKeyOrLogin {
signsSlice = append(signsSlice, signs)
} else {
length, err := converter.DecodeLength(&signs)
if err != nil {
log.WithFields(log.Fields{"type": consts.UnmarshallingError, "error": err}).Fatal("decoding signs length")
return false, err
}
if length > 0 {
signsSlice = append(signsSlice, converter.BytesShift(&signs, length))
}
if len(publicKeys) != len(signsSlice) {
log.WithFields(log.Fields{"public_keys_length": len(publicKeys), "signs_length": len(signsSlice), "type": consts.SizeDoesNotMatch}).Error("public keys and signs slices lengths does not match")
return false, fmt.Errorf("sign error %d!=%d", len(publicKeys), len(signsSlice))
}
}
return crypto.CheckSign(publicKeys[0], forSign, signsSlice[0])
}
// MerkleTreeRoot rertun Merkle value
func MerkleTreeRoot(dataArray [][]byte) []byte {
result := make(map[int32][][]byte)
for _, v := range dataArray {
hash, err := crypto.DoubleHash(v)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.CryptoError}).Fatal("double hasing value, while calculating merkle tree root")
}
hash = converter.BinToHex(hash)
result[0] = append(result[0], hash)
}
var j int32
for len(result[j]) > 1 {
for i := 0; i < len(result[j]); i = i + 2 {
if len(result[j]) <= (i + 1) {
if _, ok := result[j+1]; !ok {
result[j+1] = [][]byte{result[j][i]}
} else {
result[j+1] = append(result[j+1], result[j][i])
}
} else {
if _, ok := result[j+1]; !ok {
hash, err := crypto.DoubleHash(append(result[j][i], result[j][i+1]...))
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.CryptoError}).Fatal("double hasing value, while calculating merkle tree root")
}
hash = converter.BinToHex(hash)
result[j+1] = [][]byte{hash}
} else {
hash, err := crypto.DoubleHash([]byte(append(result[j][i], result[j][i+1]...)))
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.CryptoError}).Fatal("double hasing value, while calculating merkle tree root")
}
hash = converter.BinToHex(hash)
result[j+1] = append(result[j+1], hash)
}
}
}
j++
}
ret := result[int32(len(result)-1)]
return []byte(ret[0])
}
// TypeInt returns the identifier of the embedded transaction
func TypeInt(txType string) int64 {
for k, v := range consts.TxTypes {
if v == txType {
return int64(k)
}
}
return 0
}
// GetCurrentDir returns the current directory
func GetCurrentDir() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.WithFields(log.Fields{"type": consts.IOError, "error": err}).Warning("getting current dir")
return "."
}
return dir
}
// ShellExecute runs cmdline
func ShellExecute(cmdline string) {
time.Sleep(500 * time.Millisecond)
switch runtime.GOOS {
case "linux":
exec.Command("xdg-open", cmdline).Start()
case "windows":
exec.Command(`rundll32.exe`, `url.dll,FileProtocolHandler`, cmdline).Start()
case "darwin":
exec.Command("open", cmdline).Start()
}
}
// GetParent returns the information where the call of function happened
func GetParent() string {
parent := ""
for i := 2; ; i++ {
var name string
if pc, _, num, ok := runtime.Caller(i); ok {
name = filepath.Base(runtime.FuncForPC(pc).Name())
file, line := runtime.FuncForPC(pc).FileLine(pc)
if i > 5 || name == "runtime.goexit" {
break
} else {
parent += fmt.Sprintf("%s:%d -> %s:%d / ", filepath.Base(file), line, name, num)
}
}
}
return parent
}
// GetNodeKeys returns node private key and public key
func GetNodeKeys() (string, string, error) {
nprivkey, err := ioutil.ReadFile(filepath.Join(conf.Config.KeysDir, consts.NodePrivateKeyFilename))
if err != nil {
log.WithFields(log.Fields{"type": consts.IOError, "error": err}).Error("reading node private key from file")
return "", "", err
}
key, err := hex.DecodeString(string(nprivkey))
if err != nil {
log.WithFields(log.Fields{"type": consts.ConversionError, "error": err}).Error("decoding private key from hex")
return "", "", err
}
npubkey, err := crypto.PrivateToPublic(key)
if err != nil {
log.WithFields(log.Fields{"type": consts.CryptoError, "error": err}).Error("converting node private key to public")
return "", "", err
}
return string(nprivkey), crypto.PubToHex(npubkey), nil
}
func GetNodePrivateKey() ([]byte, error) {
data, err := ioutil.ReadFile(filepath.Join(conf.Config.KeysDir, consts.NodePrivateKeyFilename))
if err != nil {
log.WithFields(log.Fields{"type": consts.IOError, "error": err}).Error("reading node private key from file")
return nil, err
}
privateKey, err := hex.DecodeString(string(data))
if err != nil {
log.WithFields(log.Fields{"type": consts.ConversionError, "error": err}).Error("decoding private key from hex")
return nil, err
}
return privateKey, nil
}
func GetHostPort(h string) string {
if strings.Contains(h, ":") {
return h
}
return fmt.Sprintf("%s:%d", h, consts.DEFAULT_TCP_PORT)
}
func BuildBlockTimeCalculator(transaction *model.DbTransaction) (BlockTimeCalculator, error) {
var btc BlockTimeCalculator
firstBlock := model.Block{}
found, err := firstBlock.Get(1)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting first block")
return btc, err
}
if !found {
log.WithFields(log.Fields{"type": consts.NotFound, "error": err}).Error("first block not found")
return btc, err
}
blockGenerationDuration := time.Millisecond * time.Duration(syspar.GetMaxBlockGenerationTime())
blocksGapDuration := time.Second * time.Duration(syspar.GetGapsBetweenBlocks())
btc = NewBlockTimeCalculator(time.Unix(firstBlock.Time, 0),
blockGenerationDuration,
blocksGapDuration,
syspar.GetNumberOfNodesFromDB(transaction),
)
return btc, nil
}
func CreateDirIfNotExists(dir string, mode os.FileMode) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, mode)
if err != nil {
return errors.Wrapf(err, "creating dir %s", dir)
}
}
return nil
}
func LockOrDie(dir string) *flock.Flock {
f := flock.NewFlock(dir)
success, err := f.TryLock()
if err != nil {
log.WithError(err).Fatal("Locking go-apla")
}
if !success {
log.Fatal("Go-apla is locked")
}
return f
}
func ShuffleSlice(slice []string) {
for i := range slice {
j := rand.Intn(i + 1)
slice[i], slice[j] = slice[j], slice[i]
}
}
func UUID() string {
return uuid.Must(uuid.NewV4()).String()
}
// MakeDirectory makes directory if is not exists
func MakeDirectory(dir string) error {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
return os.Mkdir(dir, 0775)
}
return err
}
return nil
}
func StringInSlice(slice []string, v string) bool {
for _, item := range slice {
if v == item {
return true
}
}
return false
}