-
Notifications
You must be signed in to change notification settings - Fork 28
/
log-chain.go
93 lines (79 loc) · 2.51 KB
/
log-chain.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
package tendermint
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
proto "github.com/cosmos/gogoproto/proto"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)
// LogFailedTx takes the transaction and the messages to create it and logs the appropriate data
func (c *Chain) LogFailedTx(res *sdk.TxResponse, err error, msgs []sdk.Msg) {
if c.debug {
c.Log(fmt.Sprintf("- [%s] -> sending transaction:", c.ChainID()))
for _, msg := range msgs {
c.Print(msg, false, false)
}
}
if err != nil {
c.logger.Error(fmt.Errorf("- [%s] -> err(%v)", c.ChainID(), err).Error())
if res == nil {
return
}
}
if res.Code != 0 && res.Codespace != "" {
c.logger.Info(fmt.Sprintf("✘ [%s]@{%d} - msg(%s) err(%s:%d:%s)",
c.ChainID(), res.Height, getMsgAction(msgs), res.Codespace, res.Code, res.RawLog))
}
if c.debug && !res.Empty() {
c.Log("- transaction response:")
c.Print(res, false, false)
}
}
// LogSuccessTx take the transaction and the messages to create it and logs the appropriate data
func (c *Chain) LogSuccessTx(res *sdk.TxResponse, msgs []sdk.Msg) {
c.logger.Info(fmt.Sprintf("✔ [%s]@{%d} - msg(%s) hash(%s)", c.ChainID(), res.Height, getMsgAction(msgs), res.TxHash))
}
// Log takes a string and logs the data
func (c *Chain) Log(s string) {
c.logger.Info(s)
}
// Print fmt.Printlns the json or yaml representation of whatever is passed in
// CONTRACT: The cmd calling this function needs to have the "json" and "indent" flags set
// TODO: better "text" printing here would be a nice to have
// TODO: fix indenting all over the code base
func (c *Chain) Print(toPrint proto.Message, text, indent bool) error {
var (
out []byte
err error
)
switch {
case indent && text:
return fmt.Errorf("must pass either indent or text, not both")
case text:
// TODO: This isn't really a good option,
out = []byte(fmt.Sprintf("%v", toPrint))
default:
out, err = c.codec.MarshalJSON(toPrint)
}
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
// MustGetHeight takes the height inteface and returns the actual height
func MustGetHeight(h ibcexported.Height) uint64 {
height, ok := h.(clienttypes.Height)
if !ok {
panic("height is not an instance of height! wtf")
}
return height.GetRevisionHeight()
}
func getMsgAction(msgs []sdk.Msg) string {
var out string
for i, msg := range msgs {
out += fmt.Sprintf("%d:%s,", i, sdk.MsgTypeURL(msg))
}
return strings.TrimSuffix(out, ",")
}