-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapiGetBlock.go
116 lines (96 loc) · 4.23 KB
/
apiGetBlock.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
package mintersdk
import (
//"encoding/json" -- переход на easyjson
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
// Содержимое блока
//easyjson:json
type node_block struct {
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
Result BlockResponse `json:"result"`
Error ErrorStruct `json:"error"`
}
type ErrorStruct struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
TxResult ErrorTxResult `json:"tx_result"`
}
type ErrorTxResult struct {
Code int `json:"code"`
Log string `json:"log"`
}
type BlockResponse struct {
Hash string `json:"hash" bson:"hash" gorm:"hash" db:"hash"`
HeightTx string `json:"height" bson:"-" gorm:"-" db:"-"`
Height int `json:"height_i32" bson:"height_i32" gorm:"height_i32" db:"height_i32"`
Time time.Time `json:"time" bson:"time" gorm:"time" db:"time"`
NumTxsTx string `json:"num_txs" bson:"-" gorm:"-" db:"-"`
NumTxs int `json:"num_txs_i32" bson:"num_txs_i32" gorm:"num_txs_i32" db:"num_txs_i32"`
TotalTxsTx string `json:"total_txs" bson:"-" gorm:"-" db:"-"`
TotalTxs int `json:"total_txs_i32" bson:"total_txs_i32" gorm:"total_txs_i32" db:"total_txs_i32"`
Transactions []TransResponse `json:"transactions" bson:"transactions" gorm:"transactions" db:"transactions"`
BlockRewardTx string `json:"block_reward" bson:"-" gorm:"-" db:"-"`
BlockReward float32 `json:"block_reward_f32" bson:"block_reward_f32" gorm:"block_reward_f32" db:"block_reward_f32"`
SizeTx string `json:"size" bson:"-" gorm:"-" db:"-"`
Size int `json:"size_i32" bson:"size_i32" gorm:"size_i32" db:"size_i32"`
Validators []BlockValidatorsResponse `json:"validators" bson:"validators" gorm:"validators" db:"validators"`
Proposer string `json:"proposer" bson:"proposer" gorm:"proposer" db:"proposer"` // PubKey пропозер блока
}
type BlockValidatorsResponse struct {
PubKey string `json:"pub_key" bson:"pub_key" gorm:"pub_key" db:"pub_key"`
Signed bool `json:"signed,bool" bson:"signed" gorm:"signed" db:"signed"` // подписал-true, или пропустил false
}
// type TransResponse struct --- в apiGetTransaction.go
// type TransData struct --- в apiGetTransaction.go
// получаем содержимое блока по его ID
func (c *SDK) GetBlock(id int) (BlockResponse, error) {
url := fmt.Sprintf("%s/block?height=%d", c.MnAddress, id)
res, err := http.Get(url)
if err != nil {
return BlockResponse{}, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return BlockResponse{}, err
}
var data node_block
//json.Unmarshal(body, &data) -- переход на easyjson
err = data.UnmarshalJSON(body)
if err != nil {
return BlockResponse{}, err
}
if data.Error.Code != 0 {
err = errors.New(fmt.Sprint(data.Error.Code, " - ", data.Error.Message))
return BlockResponse{}, err
}
data.Result.BlockReward = pipStr2bip_f32(data.Result.BlockRewardTx) // вознаграждение за блок
data.Result.Height, err = strconv.Atoi(data.Result.HeightTx)
if err != nil {
return BlockResponse{}, errors.New(fmt.Sprintf("%s - %s", err.Error(), "data.Result.HeightTx"))
}
data.Result.NumTxs, err = strconv.Atoi(data.Result.NumTxsTx)
if err != nil {
return BlockResponse{}, errors.New(fmt.Sprintf("%s - %s", err.Error(), "data.Result.NumTxsTx"))
}
data.Result.Size, err = strconv.Atoi(data.Result.SizeTx)
if err != nil {
return BlockResponse{}, errors.New(fmt.Sprintf("%s - %s", err.Error(), "data.Result.SizeTx"))
}
for iStep, _ := range data.Result.Transactions {
data.Result.Transactions[iStep].HeightTx = data.Result.HeightTx
//в apiGetTransaction->manipulationTransaction
err = manipulationTransaction(c, &data.Result.Transactions[iStep])
if err != nil {
return BlockResponse{}, errors.New(fmt.Sprintf("%s - %s", err.Error(), "manipulationTransaction"))
}
}
return data.Result, nil
}