github.com/vipernet-xyz/tm@v0.34.24/test/maverick/consensus/msgs.go (about) 1 package consensus 2 3 import ( 4 "errors" 5 "fmt" 6 7 tmcon "github.com/vipernet-xyz/tm/consensus" 8 cstypes "github.com/vipernet-xyz/tm/consensus/types" 9 tmmath "github.com/vipernet-xyz/tm/libs/math" 10 "github.com/vipernet-xyz/tm/p2p" 11 tmcons "github.com/vipernet-xyz/tm/proto/tendermint/consensus" 12 tmproto "github.com/vipernet-xyz/tm/proto/tendermint/types" 13 "github.com/vipernet-xyz/tm/types" 14 ) 15 16 func WALToProto(msg tmcon.WALMessage) (*tmcons.WALMessage, error) { 17 var pb tmcons.WALMessage 18 19 switch msg := msg.(type) { 20 case types.EventDataRoundState: 21 pb = tmcons.WALMessage{ 22 Sum: &tmcons.WALMessage_EventDataRoundState{ 23 EventDataRoundState: &tmproto.EventDataRoundState{ 24 Height: msg.Height, 25 Round: msg.Round, 26 Step: msg.Step, 27 }, 28 }, 29 } 30 case msgInfo: 31 consMsg, err := tmcon.MsgToProto(msg.Msg) 32 if err != nil { 33 return nil, err 34 } 35 pb = tmcons.WALMessage{ 36 Sum: &tmcons.WALMessage_MsgInfo{ 37 MsgInfo: &tmcons.MsgInfo{ 38 Msg: *consMsg, 39 PeerID: string(msg.PeerID), 40 }, 41 }, 42 } 43 case timeoutInfo: 44 pb = tmcons.WALMessage{ 45 Sum: &tmcons.WALMessage_TimeoutInfo{ 46 TimeoutInfo: &tmcons.TimeoutInfo{ 47 Duration: msg.Duration, 48 Height: msg.Height, 49 Round: msg.Round, 50 Step: uint32(msg.Step), 51 }, 52 }, 53 } 54 case tmcon.EndHeightMessage: 55 pb = tmcons.WALMessage{ 56 Sum: &tmcons.WALMessage_EndHeight{ 57 EndHeight: &tmcons.EndHeight{ 58 Height: msg.Height, 59 }, 60 }, 61 } 62 default: 63 return nil, fmt.Errorf("to proto: wal message not recognized: %T", msg) 64 } 65 66 return &pb, nil 67 } 68 69 // WALFromProto takes a proto wal message and return a consensus walMessage and error 70 func WALFromProto(msg *tmcons.WALMessage) (tmcon.WALMessage, error) { 71 if msg == nil { 72 return nil, errors.New("nil WAL message") 73 } 74 var pb tmcon.WALMessage 75 76 switch msg := msg.Sum.(type) { 77 case *tmcons.WALMessage_EventDataRoundState: 78 pb = types.EventDataRoundState{ 79 Height: msg.EventDataRoundState.Height, 80 Round: msg.EventDataRoundState.Round, 81 Step: msg.EventDataRoundState.Step, 82 } 83 case *tmcons.WALMessage_MsgInfo: 84 walMsg, err := tmcon.MsgFromProto(&msg.MsgInfo.Msg) 85 if err != nil { 86 return nil, fmt.Errorf("msgInfo from proto error: %w", err) 87 } 88 pb = msgInfo{ 89 Msg: walMsg, 90 PeerID: p2p.ID(msg.MsgInfo.PeerID), 91 } 92 93 case *tmcons.WALMessage_TimeoutInfo: 94 tis, err := tmmath.SafeConvertUint8(int64(msg.TimeoutInfo.Step)) 95 // deny message based on possible overflow 96 if err != nil { 97 return nil, fmt.Errorf("denying message due to possible overflow: %w", err) 98 } 99 pb = timeoutInfo{ 100 Duration: msg.TimeoutInfo.Duration, 101 Height: msg.TimeoutInfo.Height, 102 Round: msg.TimeoutInfo.Round, 103 Step: cstypes.RoundStepType(tis), 104 } 105 return pb, nil 106 case *tmcons.WALMessage_EndHeight: 107 pb := tmcon.EndHeightMessage{ 108 Height: msg.EndHeight.Height, 109 } 110 return pb, nil 111 default: 112 return nil, fmt.Errorf("from proto: wal message not recognized: %T", msg) 113 } 114 return pb, nil 115 }