/
blockchain.go
203 lines (193 loc) · 5.55 KB
/
blockchain.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 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 main
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/EGaaS/go-egaas-mvp/packages/consts"
"github.com/EGaaS/go-egaas-mvp/packages/lib"
"github.com/EGaaS/go-egaas-mvp/packages/utils"
)
type getCntJSON struct {
Name string `json:"name"`
Error string `json:"error"`
}
// TxInfo contains information about transaction
type TxInfo struct {
ID int64 `json:"id"`
BlockID int64 `json:"block"`
Address string `json:"wallet"`
State string `json:"state"`
Time string `json:"time"`
TxName string `json:"tx"`
Comment string `json:"comment"`
next *TxInfo
prev *TxInfo
}
const (
txLimit = 300
)
var (
txID int64
txLatest int64
// txOff int
txList, txTop *TxInfo
txStates = make(map[int64]string)
txContracts = make(map[int32]string)
)
// GetTx gets information about new transactions
func GetTx() {
txList = &TxInfo{}
txTop = txList
prev := txList
for i := 0; i < txLimit; i++ {
txTop.next = &TxInfo{}
txTop = txTop.next
txTop.prev = prev
prev = txTop
}
txTop.next = txList
txList.prev = prev
txTop = txList
for {
// b.hash, b.state_id,
explorer, err := utils.DB.GetAll(`SELECT b.data, b.time, b.tx, b.id FROM block_chain as b
where b.id > $1 order by b.id desc limit 30 offset 0`, -1, txLatest)
if err == nil && len(explorer) > 0 {
txLatest = utils.StrToInt64(explorer[0][`id`])
for i := len(explorer); i > 0; i-- {
item := explorer[i-1]
if utils.StrToInt64(item[`tx`]) == 0 {
continue
}
block := ([]byte(item[`data`]))[1:]
utils.ParseBlockHeader(&block)
for len(block) > 0 {
size := int(utils.DecodeLength(&block))
if size == 0 || len(block) < size {
break
}
var (
name, comment string
txtime int64
wallet, state int64
)
itype := int(block[0])
if itype < 128 {
if stype, ok := consts.TxTypes[itype]; ok {
name = stype
} else {
name = fmt.Sprintf("unknown %d", itype)
}
input := block[1:]
txtime = utils.BinToDecBytesShift(&input, 4)
length := utils.DecodeLength(&input)
if length > 0 && length < 30 {
wallet = utils.BytesToInt64(utils.BytesShift(&input, length))
length = utils.DecodeLength(&input)
if length > 0 && length < 20 {
state = utils.BytesToInt64(utils.BytesShift(&input, length))
}
} else {
break
}
//wallet, _ = utils.DecodeLenInt64(&input)
//state, _ = utils.DecodeLenInt64(&input)
} else {
itype -= 128
tmp := make([]byte, 4)
for i := 0; i < itype; i++ {
tmp[4-itype+i] = block[i+1]
}
idc := int32(binary.BigEndian.Uint32(tmp))
if val, ok := txContracts[idc]; ok {
name = val
} else {
resp, err := http.Get(strings.TrimRight(gSettings.Node, `/`) +
fmt.Sprintf(`/ajax?json=ajax_get_cnt&id=%d`, idc))
if err != nil {
break
}
if answer, err := ioutil.ReadAll(resp.Body); err != nil {
resp.Body.Close()
break
} else {
var answerJSON getCntJSON
resp.Body.Close()
if err = json.Unmarshal(answer, &answerJSON); err != nil {
break
}
var off int
for off < len(answerJSON.Name) && answerJSON.Name[off] < 'A' {
off++
}
name = answerJSON.Name[off:]
txContracts[idc] = name
}
}
input := block[:]
header := consts.TXHeader{}
if err = lib.BinUnmarshal(&input, &header); err != nil {
break
}
txtime = int64(header.Time)
wallet = int64(header.WalletID)
state = int64(header.StateID)
}
switch name {
case `GenCitizen`:
comment = `1`
}
if name == `GenCitizen` && txTop.TxName == name && txTop.BlockID == utils.StrToInt64(item[`id`]) &&
txTop.Address == lib.AddressToString(wallet) {
txTop.Comment = fmt.Sprintf(`%d`, utils.StrToInt64(txTop.Comment)+1)
} else {
txTop = txTop.next
txID++
txTop.ID = txID
txTop.BlockID = utils.StrToInt64(item[`id`])
txTop.Address = lib.AddressToString(wallet)
txTop.Comment = comment
txTop.TxName = name
txTop.Time = time.Unix(txtime, 0).String()[:19]
if state > 0 {
if val, ok := txStates[state]; ok {
txTop.State = val
} else {
stateName, _ := utils.DB.Single(`select state_name from global_states_list where gstate_id=?`, state).String()
if len(stateName) > 0 {
txStates[state] = stateName
txTop.State = stateName
}
}
// txTop.State = utils.Int64ToStr(state)
} else {
txTop.State = ``
}
}
// fmt.Println(`NAME`, *txTop)
block = block[size:]
}
}
}
time.Sleep(1 * time.Second)
}
}