8 "github.com/tendermint/go-wire"
10 "github.com/bytom/protocol/bc"
11 "github.com/bytom/protocol/bc/types"
16 BlockRequestByte = byte(0x10)
17 BlockResponseByte = byte(0x11)
18 StatusRequestByte = byte(0x20)
19 StatusResponseByte = byte(0x21)
20 NewTransactionByte = byte(0x30)
21 NewMineBlockByte = byte(0x40)
23 maxBlockchainResponseSize = 22020096 + 2
26 // BlockchainMessage is a generic message for this reactor.
27 type BlockchainMessage interface{}
29 var _ = wire.RegisterInterface(
30 struct{ BlockchainMessage }{},
31 wire.ConcreteType{&BlockRequestMessage{}, BlockRequestByte},
32 wire.ConcreteType{&BlockResponseMessage{}, BlockResponseByte},
33 wire.ConcreteType{&StatusRequestMessage{}, StatusRequestByte},
34 wire.ConcreteType{&StatusResponseMessage{}, StatusResponseByte},
35 wire.ConcreteType{&TransactionNotifyMessage{}, NewTransactionByte},
36 wire.ConcreteType{&MineBlockMessage{}, NewMineBlockByte},
39 type blockPending struct {
44 type txsNotify struct {
49 //DecodeMessage decode msg
50 func DecodeMessage(bz []byte) (msgType byte, msg BlockchainMessage, err error) {
53 r := bytes.NewReader(bz)
54 msg = wire.ReadBinary(struct{ BlockchainMessage }{}, r, maxBlockchainResponseSize, &n, &err).(struct{ BlockchainMessage }).BlockchainMessage
55 if err != nil && n != len(bz) {
56 err = errors.New("DecodeMessage() had bytes left over")
61 //BlockRequestMessage request blocks from remote peers by height/hash
62 type BlockRequestMessage struct {
68 func (m *BlockRequestMessage) GetHash() *bc.Hash {
69 hash := bc.NewHash(m.RawHash)
73 //String convert msg to string
74 func (m *BlockRequestMessage) String() string {
76 return fmt.Sprintf("BlockRequestMessage{Height: %d}", m.Height)
79 return fmt.Sprintf("BlockRequestMessage{Hash: %s}", hash.String())
82 //BlockResponseMessage response get block msg
83 type BlockResponseMessage struct {
87 //NewBlockResponseMessage construct bock response msg
88 func NewBlockResponseMessage(block *types.Block) (*BlockResponseMessage, error) {
89 rawBlock, err := block.MarshalText()
93 return &BlockResponseMessage{RawBlock: rawBlock}, nil
96 //GetBlock get block from msg
97 func (m *BlockResponseMessage) GetBlock() *types.Block {
98 block := &types.Block{
99 BlockHeader: types.BlockHeader{},
100 Transactions: []*types.Tx{},
102 block.UnmarshalText(m.RawBlock)
106 //String convert msg to string
107 func (m *BlockResponseMessage) String() string {
108 return fmt.Sprintf("BlockResponseMessage{Size: %d}", len(m.RawBlock))
111 //TransactionNotifyMessage notify new tx msg
112 type TransactionNotifyMessage struct {
116 //NewTransactionNotifyMessage construct notify new tx msg
117 func NewTransactionNotifyMessage(tx *types.Tx) (*TransactionNotifyMessage, error) {
118 rawTx, err := tx.TxData.MarshalText()
122 return &TransactionNotifyMessage{RawTx: rawTx}, nil
125 //GetTransaction get tx from msg
126 func (m *TransactionNotifyMessage) GetTransaction() (*types.Tx, error) {
128 if err := tx.UnmarshalText(m.RawTx); err != nil {
135 func (m *TransactionNotifyMessage) String() string {
136 return fmt.Sprintf("TransactionNotifyMessage{Size: %d}", len(m.RawTx))
139 //StatusRequestMessage status request msg
140 type StatusRequestMessage struct{}
143 func (m *StatusRequestMessage) String() string {
144 return "StatusRequestMessage"
147 //StatusResponseMessage get status response msg
148 type StatusResponseMessage struct {
153 //NewStatusResponseMessage construct get status response msg
154 func NewStatusResponseMessage(blockHeader *types.BlockHeader) *StatusResponseMessage {
155 return &StatusResponseMessage{
156 Height: blockHeader.Height,
157 RawHash: blockHeader.Hash().Byte32(),
161 //GetHash get hash from msg
162 func (m *StatusResponseMessage) GetHash() *bc.Hash {
163 hash := bc.NewHash(m.RawHash)
167 //String convert msg to string
168 func (m *StatusResponseMessage) String() string {
170 return fmt.Sprintf("StatusResponseMessage{Height: %d, Hash: %s}", m.Height, hash.String())
173 //MineBlockMessage new mined block msg
174 type MineBlockMessage struct {
178 //NewMinedBlockMessage construct new mined block msg
179 func NewMinedBlockMessage(block *types.Block) (*MineBlockMessage, error) {
180 rawBlock, err := block.MarshalText()
184 return &MineBlockMessage{RawBlock: rawBlock}, nil
187 //GetMineBlock get mine block from msg
188 func (m *MineBlockMessage) GetMineBlock() (*types.Block, error) {
189 block := &types.Block{}
190 if err := block.UnmarshalText(m.RawBlock); err != nil {
196 //String convert msg to string
197 func (m *MineBlockMessage) String() string {
198 return fmt.Sprintf("NewMineBlockMessage{Size: %d}", len(m.RawBlock))