-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
281 lines (248 loc) · 6.99 KB
/
repo.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
// Copyright © 2017 ZhongAn Technology
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package repository
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/astaxie/beego"
"github.com/dappledger/ann-explorer/rpc"
)
var (
BLOCK_COLLECT = "block"
TX_COLLECT = "transaction"
CONTRACT_META_COLLECT = "contract_meta"
MONGO_URL string
DB_NAME string
ChainID string
)
type Repo interface {
Init()
Save(br *BlockRepo) (err error)
SaveContractMeta(meta ContractMeta) (err error)
LatestBlocks(limit int) (displayData []DisplayItem, err error)
BlocksFromTo(from, to int) (blocks []Block, err error)
CollectionItemNum(collect string) (count int, err error)
Contract(hash string) (tx Transaction, txs []Transaction, err error)
Height() (maxHeight int, err error)
Contracts(limit int) (txs []Transaction, err error)
ContractsCount() (int, error)
LatestContracts(limit int, skip int) (txs []Transaction, err error)
TxsQuery(fromTo string) (txs []Transaction, err error)
Txs(limit int) (txs []Transaction, err error)
TxsCount() (int, error)
LatestTxs(limit int, skip int) (txs []Transaction, err error)
OneContract(hash string) (contract Transaction, txs []Transaction, err error)
OneContractMeta(hash string) (*ContractMeta, error)
OneTransaction(hash string) (tx Transaction, err error)
TransactionsByBlkhash(hash string) (txs []Transaction, err error)
TxsByBlkHeight(height int) (txs []Transaction, err error)
TransactionFromTo(from, to int) (txs []Transaction, err error)
BlockByHeight(height int) (block Block, err error)
OneBlock(hash string) (block Block, txs []Transaction, err error)
}
type HTTPResponse struct {
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
Result *json.RawMessage `json:"result"`
Error string `json:"error"`
}
type Status struct {
NodeInfo *NodeInfo `json:"node_info"`
LatestBlockHeight int `json:"latest_block_height"`
}
type NodeInfo struct {
NetWork string `json:"network"`
}
func GetHTTPResp(url string) (bytez []byte, err error) {
resp, errR := http.Get(url)
if errR != nil {
err = errR
return
}
defer resp.Body.Close()
bytez, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
var hr HTTPResponse
err = json.Unmarshal(bytez, &hr)
if err != nil {
return
}
if hr.Result == nil {
err = errors.New(fmt.Sprintf("json.Unmarshal (%s)HTTPResponse wrong ,maybe you need config 'chain_id'", url))
return
}
bytez, err = hr.Result.MarshalJSON()
if err != nil {
return
}
return
}
func GetStatus(chainID string) (status Status) {
url := fmt.Sprintf("%s/status?chainid=\"%s\"", rpc.HTTP_ADDR, chainID)
bytez, err := GetHTTPResp(url)
if err != nil {
log.Fatalf("GetHTTPResp failed: %s", err.Error())
}
err = json.Unmarshal(bytez, &status)
if err != nil {
log.Fatalf("json.Unmarshal(Status) failed: %s", err.Error())
}
return
}
var deleteRepo Repo
func Init() {
if beego.AppConfig.String("mogo_addr") != "" {
deleteRepo = &monRepo{}
} else {
deleteRepo = &sqliteRepo{}
}
deleteRepo.Init()
return
}
type BlockRepo struct {
Blocks []Block
Txs []Transaction
}
type DisplayItem struct {
Block
Tps int
Interval float64
}
type Block struct {
Hash string
ParentHash string
ChainID string
Height int `bson:"_id"`
Time time.Time
NumTxs int
LastCommitHash string
DataHash string
ValidatorsHash string
AppHash string
ProposerAddress string
}
type ContractMeta struct {
Hash string `json:"hash" bson:"_id"`
ABI string `json:"abi" bson:"abi"`
}
type Transaction struct {
Payload []byte `json:"-"`
PayloadHex string
ContractMeta ContractMeta `bson:"-"`
Hash string `bson:"_id"`
From string
To string
Receipt string
Amount string
Nonce uint64
Gas string
Size int64
Block string
Contract string
Time time.Time
Height int
}
func Save(br *BlockRepo) (err error) {
err = deleteRepo.Save(br)
return
}
func LatestBlocks(limit int) (displayData []DisplayItem, err error) {
displayData, err = deleteRepo.LatestBlocks(limit)
return
}
func BlocksFromTo(from, to int) (blocks []Block, err error) {
blocks, err = deleteRepo.BlocksFromTo(from, to)
return
}
func CollectionItemNum(collect string) (count int, err error) {
count, err = deleteRepo.CollectionItemNum(collect)
return
}
func Contract(hash string) (contract Transaction, txs []Transaction, err error) {
contract, txs, err = deleteRepo.Contract(hash)
return
}
func Height() (maxHeight int, err error) {
maxHeight, err = deleteRepo.Height()
return
}
func ContractsCount() (int, error) {
return deleteRepo.ContractsCount()
}
func LatestContracts(limit int, skip int) (txs []Transaction, err error) {
txs, err = deleteRepo.LatestContracts(limit, skip)
return
}
func TxsQuery(fromTo string) (txs []Transaction, err error) {
txs, err = deleteRepo.TxsQuery(fromTo)
return
}
func TxsCount() (int, error) {
return deleteRepo.TxsCount()
}
func LatestTxs(limit int, skip int) (txs []Transaction, err error) {
txs, err = deleteRepo.LatestTxs(limit, skip)
return
}
func Txs(limit int) (txs []Transaction, err error) {
txs, err = deleteRepo.Txs(limit)
return
}
func OneContract(hash string) (contract Transaction, txs []Transaction, err error) {
contract, txs, err = deleteRepo.OneContract(hash)
return
}
func OneContractMeta(hash string) (meta *ContractMeta, err error) {
return deleteRepo.OneContractMeta(hash)
}
func OneTransaction(hash string) (tx Transaction, err error) {
tx, err = deleteRepo.OneTransaction(hash)
return
}
func TxsByBlkHeight(height int) (txs []Transaction, err error) {
txs, err = deleteRepo.TxsByBlkHeight(height)
return
}
func TransactionsByBlkhash(hash string) (txs []Transaction, err error) {
txs, err = deleteRepo.TransactionsByBlkhash(hash)
return
}
func TransactionFromTo(from, to int) (txs []Transaction, err error) {
txs, err = deleteRepo.TransactionFromTo(from, to)
return
}
func BlockByHeight(height int) (block Block, err error) {
block, err = deleteRepo.BlockByHeight(height)
return
}
func OneBlock(hash string) (block Block, txs []Transaction, err error) {
block, txs, err = deleteRepo.OneBlock(hash)
return
}
func SaveContractMeta(meta ContractMeta) error {
return deleteRepo.SaveContractMeta(meta)
}
func TryLock(owner string, long int) (bool, error) {
m, ok := deleteRepo.(*monRepo)
if ok {
return m.TryLock(owner, long)
}
return true, nil
}