forked from forbole/juno
/
cosmos.go
155 lines (132 loc) · 4.37 KB
/
cosmos.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
package types
import (
"fmt"
"time"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
)
// Validator contains the data of a single validator
type Validator struct {
ConsAddr string
ConsPubKey string
}
// NewValidator allows to build a new Validator instance
func NewValidator(consAddr string, consPubKey string) *Validator {
return &Validator{
ConsAddr: consAddr,
ConsPubKey: consPubKey,
}
}
// -------------------------------------------------------------------------------------------------------------------
// CommitSig contains the data of a single validator commit signature
type CommitSig struct {
Height int64
ValidatorAddress string
VotingPower int64
ProposerPriority int64
Timestamp time.Time
}
// NewCommitSig allows to build a new CommitSign object
func NewCommitSig(validatorAddress string, votingPower, proposerPriority, height int64, timestamp time.Time) *CommitSig {
return &CommitSig{
Height: height,
ValidatorAddress: validatorAddress,
VotingPower: votingPower,
ProposerPriority: proposerPriority,
Timestamp: timestamp,
}
}
// -------------------------------------------------------------------------------------------------------------------
// Block contains the data of a single chain block
type Block struct {
Height int64
Hash string
TxNum int
TotalGas uint64
ProposerAddress string
Timestamp time.Time
}
// NewBlock allows to build a new Block instance
func NewBlock(
height int64, hash string, txNum int, totalGas uint64, proposerAddress string, timestamp time.Time,
) *Block {
return &Block{
Height: height,
Hash: hash,
TxNum: txNum,
TotalGas: totalGas,
ProposerAddress: proposerAddress,
Timestamp: timestamp,
}
}
// NewBlockFromTmBlock builds a new Block instance from a given ResultBlock object
func NewBlockFromTmBlock(blk *tmctypes.ResultBlock, totalGas uint64) *Block {
return NewBlock(
blk.Block.Height,
blk.Block.Hash().String(),
len(blk.Block.Txs),
totalGas,
ConvertValidatorAddressToBech32String(blk.Block.ProposerAddress),
blk.Block.Time,
)
}
// -------------------------------------------------------------------------------------------------------------------
// Tx represents an already existing blockchain transaction
type Tx struct {
*tx.Tx
*sdk.TxResponse
}
// NewTx allows to create a new Tx instance from the given txResponse
func NewTx(txResponse *sdk.TxResponse, tx *tx.Tx) (*Tx, error) {
return &Tx{
Tx: tx,
TxResponse: txResponse,
}, nil
}
// FindEventByType searches inside the given tx events for the message having the specified index, in order
// to find the event having the given type, and returns it.
// If no such event is found, returns an error instead.
func (tx Tx) FindEventByType(index int, eventType string) (sdk.StringEvent, error) {
for _, ev := range tx.Logs[index].Events {
if ev.Type == eventType {
return ev, nil
}
}
return sdk.StringEvent{}, fmt.Errorf("no %s event found inside tx with hash %s", eventType, tx.TxHash)
}
// FindAttributeByKey searches inside the specified event of the given tx to find the attribute having the given key.
// If the specified event does not contain a such attribute, returns an error instead.
func (tx Tx) FindAttributeByKey(event sdk.StringEvent, attrKey string) (string, error) {
for _, attr := range event.Attributes {
if attr.Key == attrKey {
return attr.Value, nil
}
}
return "", fmt.Errorf("no event with attribute %s found inside tx with hash %s", attrKey, tx.TxHash)
}
// Successful tells whether this tx is successful or not
func (tx Tx) Successful() bool {
return tx.TxResponse.Code == 0
}
// -------------------------------------------------------------------------------------------------------------------
// Message represents the data of a single message
type Message struct {
TxHash string
Index int
Type string
Value string
Addresses []string
Height int64
}
// NewMessage allows to build a new Message instance
func NewMessage(txHash string, index int, msgType string, value string, addresses []string, height int64) *Message {
return &Message{
TxHash: txHash,
Index: index,
Type: msgType,
Value: value,
Addresses: addresses,
Height: height,
}
}