-
Notifications
You must be signed in to change notification settings - Fork 3
/
block.go
109 lines (96 loc) · 2.57 KB
/
block.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
package types
import (
"encoding/base64"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/encoding"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/irisnet/service-sdk-go/codec"
)
type Block struct {
tmtypes.Header `json:"header"`
Data `json:"data"`
Evidence tmtypes.EvidenceData `json:"evidence"`
LastCommit *tmtypes.Commit `json:"last_commit"`
}
type Data struct {
Txs []StdTx `json:"txs"`
}
func ParseBlock(cdc *codec.LegacyAmino, block *tmtypes.Block) Block {
var txs []StdTx
for _, tx := range block.Txs {
var stdTx StdTx
if err := cdc.UnmarshalBinaryBare(tx, &stdTx); err == nil {
txs = append(txs, stdTx)
}
}
return Block{
Header: block.Header,
Data: Data{Txs: txs},
Evidence: block.Evidence,
LastCommit: block.LastCommit,
}
}
type BlockResult struct {
Height int64 `json:"height"`
Results ABCIResponses `json:"results"`
}
type BlockDetail struct {
BlockID tmtypes.BlockID `json:"block_id"`
Block Block `json:"block"`
BlockResult BlockResult `json:"block_result"`
}
type ABCIResponses struct {
DeliverTx []TxResult
EndBlock ResultEndBlock
BeginBlock ResultBeginBlock
}
type ResultBeginBlock struct {
Events StringEvents `json:"events"`
}
type ResultEndBlock struct {
Events StringEvents `json:"events"`
ValidatorUpdates []ValidatorUpdate `json:"validator_updates"`
}
func ParseValidatorUpdate(updates []abci.ValidatorUpdate) []ValidatorUpdate {
var vUpdates []ValidatorUpdate
for _, v := range updates {
pubkey, _ := encoding.PubKeyFromProto(v.PubKey)
vUpdates = append(
vUpdates,
ValidatorUpdate{
PubKey: PubKey{
Type: pubkey.Type(),
Value: base64.StdEncoding.EncodeToString(pubkey.Bytes()),
},
Power: v.Power,
},
)
}
return vUpdates
}
func ParseBlockResult(res *ctypes.ResultBlockResults) BlockResult {
var txResults = make([]TxResult, len(res.TxsResults))
for i, r := range res.TxsResults {
txResults[i] = TxResult{
Code: r.Code,
Log: r.Log,
GasWanted: r.GasWanted,
GasUsed: r.GasUsed,
Events: StringifyEvents(r.Events),
}
}
return BlockResult{
Height: res.Height,
Results: ABCIResponses{
DeliverTx: txResults,
EndBlock: ResultEndBlock{
Events: StringifyEvents(res.EndBlockEvents),
ValidatorUpdates: ParseValidatorUpdate(res.ValidatorUpdates),
},
BeginBlock: ResultBeginBlock{
Events: StringifyEvents(res.BeginBlockEvents),
},
},
}
}