This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
blockresponse.go
75 lines (61 loc) · 1.71 KB
/
blockresponse.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
package clienttypes
import (
"encoding/json"
"errors"
"math/big"
handlertypes "github.com/opennetsys/golkadot/client/p2p/handler/types"
"github.com/opennetsys/golkadot/common/bnutil"
"github.com/opennetsys/golkadot/common/codec"
"github.com/opennetsys/golkadot/common/u8util"
"github.com/opennetsys/golkadot/logger"
)
// Kind ...
func (b *BlockResponse) Kind() handlertypes.FuncEnum {
return handlertypes.BlockResponse
}
// Encode serializes the message into a bytes array
// TODO: fix...
func (b *BlockResponse) Encode() ([]byte, error) {
jsn, err := b.MarshalJSON()
if err != nil {
return nil, err
}
msgBytes, err := codec.Encode(jsn)
if err != nil {
return nil, err
}
return u8util.Concat(bnutil.ToUint8Slice(big.NewInt(int64(handlertypes.BlockResponse)), 8, true, false), msgBytes), nil
}
// Decode deserializes a bytes array into a message
// TODO: fix...
func (b *BlockResponse) Decode(data []byte) error {
if data == nil || len(data) == 0 {
return errors.New("nil data")
}
bn := bnutil.ToBN(data[0], true)
if bn == nil {
return errors.New("nil kind")
}
if bn.Int64() != int64(handlertypes.BlockResponse) {
logger.Errorf("[block request] expected Block Response, but received %v", bn.Int64())
return errors.New("wrong kind")
}
return b.UnmarshalJSON(data[1:])
}
// MarshalJSON returns json
func (b *BlockResponse) MarshalJSON() ([]byte, error) {
return json.Marshal(b.Message)
}
// UnmarshalJSON converts json to a message
func (b *BlockResponse) UnmarshalJSON(data []byte) error {
msg := new(BlockResponseMessage)
if err := json.Unmarshal(data, msg); err != nil {
return err
}
b.Message = msg
return nil
}
// GetHeader ...
func (b *BlockResponse) GetHeader() *Header {
return nil
}