forked from ezdapps/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_block.go
More file actions
137 lines (122 loc) · 4.96 KB
/
first_block.go
File metadata and controls
137 lines (122 loc) · 4.96 KB
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
// Copyright 2016 The go-daylight Authors
// This file is part of the go-daylight library.
//
// The go-daylight library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-daylight library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-daylight library. If not, see <http://www.gnu.org/licenses/>.
package parser
import (
"encoding/hex"
"errors"
"io/ioutil"
"path/filepath"
"github.com/GenesisKernel/go-genesis/packages/conf"
"github.com/GenesisKernel/go-genesis/packages/config/syspar"
"github.com/GenesisKernel/go-genesis/packages/consts"
"github.com/GenesisKernel/go-genesis/packages/converter"
"github.com/GenesisKernel/go-genesis/packages/crypto"
"github.com/GenesisKernel/go-genesis/packages/model"
"github.com/GenesisKernel/go-genesis/packages/smart"
"github.com/GenesisKernel/go-genesis/packages/utils/tx"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
)
// FirstBlockParser is parser wrapper
type FirstBlockParser struct {
*Parser
}
// ErrFirstBlockHostIsEmpty host for first block is not specified
var ErrFirstBlockHostIsEmpty = errors.New("FirstBlockHost is empty")
// Init first block
func (p *FirstBlockParser) Init() error {
return nil
}
// Validate first block
func (p *FirstBlockParser) Validate() error {
return nil
}
// Action is fires first block
func (p *FirstBlockParser) Action() error {
logger := p.GetLogger()
data := p.TxPtr.(*consts.FirstBlock)
myAddress := crypto.Address(data.PublicKey)
err := model.ExecSchemaEcosystem(nil, 1, myAddress, ``, myAddress)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("executing ecosystem schema")
return p.ErrInfo(err)
}
err = model.GetDB(p.DbTransaction).Exec(`insert into "1_keys" (id,pub,amount) values(?, ?,?)`,
myAddress, data.PublicKey, decimal.NewFromFloat(consts.FIRST_QDLT).String()).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("inserting default page")
return p.ErrInfo(err)
}
err = model.GetDB(p.DbTransaction).Exec(`insert into "1_pages" (id,name,menu,value,conditions) values('1', 'default_page',
'default_menu', ?, 'ContractAccess("@1EditPage")')`, syspar.SysString(`default_ecosystem_page`)).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("inserting default page")
return p.ErrInfo(err)
}
err = model.GetDB(p.DbTransaction).Exec(`insert into "1_menu" (id,name,value,title,conditions) values('1', 'default_menu', ?, ?, 'ContractAccess("@1EditMenu")')`,
syspar.SysString(`default_ecosystem_menu`), `default`).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("inserting default menu")
return p.ErrInfo(err)
}
err = smart.LoadContract(p.DbTransaction, `1`)
if err != nil {
return p.ErrInfo(err)
}
node := &model.SystemParameter{Name: `full_nodes`}
if err = node.SaveArray([][]string{{data.Host, converter.Int64ToStr(myAddress),
hex.EncodeToString(data.NodePublicKey)}}); err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("saving node array")
return p.ErrInfo(err)
}
commission := &model.SystemParameter{Name: `commission_wallet`}
if err = commission.SaveArray([][]string{{"1", converter.Int64ToStr(myAddress)}}); err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("saving commission_wallet array")
return p.ErrInfo(err)
}
if err = syspar.SysUpdate(nil); err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("updating syspar")
return p.ErrInfo(err)
}
return nil
}
// Rollback first block
func (p *FirstBlockParser) Rollback() error {
return nil
}
// Header is returns first block header
func (p FirstBlockParser) Header() *tx.Header {
return nil
}
// GetKeyIDFromPrivateKey load KeyID fron PrivateKey file
func GetKeyIDFromPrivateKey() (int64, error) {
key, err := ioutil.ReadFile(filepath.Join(conf.Config.PrivateDir, consts.PrivateKeyFilename))
if err != nil {
log.WithFields(log.Fields{"type": consts.IOError, "error": err}).Error("reading private key file")
return 0, err
}
key, err = hex.DecodeString(string(key))
if err != nil {
log.WithFields(log.Fields{"type": consts.ConversionError, "error": err}).Error("decoding private key from hex")
return 0, err
}
key, err = crypto.PrivateToPublic(key)
if err != nil {
log.WithFields(log.Fields{"type": consts.CryptoError, "error": err}).Error("converting private key to public")
return 0, err
}
return crypto.Address(key), nil
}