github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/test/maverick/consensus/msgs.go (about) 1 package consensus 2 3 import ( 4 "errors" 5 "fmt" 6 7 cmtcon "github.com/badrootd/celestia-core/consensus" 8 cstypes "github.com/badrootd/celestia-core/consensus/types" 9 cmtmath "github.com/badrootd/celestia-core/libs/math" 10 "github.com/badrootd/celestia-core/p2p" 11 cmtcons "github.com/badrootd/celestia-core/proto/tendermint/consensus" 12 cmtproto "github.com/badrootd/celestia-core/proto/tendermint/types" 13 "github.com/badrootd/celestia-core/types" 14 ) 15 16 func WALToProto(msg cmtcon.WALMessage) (*cmtcons.WALMessage, error) { 17 var pb cmtcons.WALMessage 18 19 switch msg := msg.(type) { 20 case types.EventDataRoundState: 21 pb = cmtcons.WALMessage{ 22 Sum: &cmtcons.WALMessage_EventDataRoundState{ 23 EventDataRoundState: &cmtproto.EventDataRoundState{ 24 Height: msg.Height, 25 Round: msg.Round, 26 Step: msg.Step, 27 }, 28 }, 29 } 30 case msgInfo: 31 consMsg, err := cmtcon.MsgToProto(msg.Msg) 32 if err != nil { 33 return nil, err 34 } 35 pb = cmtcons.WALMessage{ 36 Sum: &cmtcons.WALMessage_MsgInfo{ 37 MsgInfo: &cmtcons.MsgInfo{ 38 Msg: *consMsg, 39 PeerID: string(msg.PeerID), 40 }, 41 }, 42 } 43 case timeoutInfo: 44 pb = cmtcons.WALMessage{ 45 Sum: &cmtcons.WALMessage_TimeoutInfo{ 46 TimeoutInfo: &cmtcons.TimeoutInfo{ 47 Duration: msg.Duration, 48 Height: msg.Height, 49 Round: msg.Round, 50 Step: uint32(msg.Step), 51 }, 52 }, 53 } 54 case cmtcon.EndHeightMessage: 55 pb = cmtcons.WALMessage{ 56 Sum: &cmtcons.WALMessage_EndHeight{ 57 EndHeight: &cmtcons.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 *cmtcons.WALMessage) (cmtcon.WALMessage, error) { 71 if msg == nil { 72 return nil, errors.New("nil WAL message") 73 } 74 var pb cmtcon.WALMessage 75 76 switch msg := msg.Sum.(type) { 77 case *cmtcons.WALMessage_EventDataRoundState: 78 pb = types.EventDataRoundState{ 79 Height: msg.EventDataRoundState.Height, 80 Round: msg.EventDataRoundState.Round, 81 Step: msg.EventDataRoundState.Step, 82 } 83 case *cmtcons.WALMessage_MsgInfo: 84 walMsg, err := cmtcon.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 *cmtcons.WALMessage_TimeoutInfo: 94 tis, err := cmtmath.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 *cmtcons.WALMessage_EndHeight: 107 pb := cmtcon.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 }