/
config.go
134 lines (114 loc) · 3.52 KB
/
config.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (C) 2019 The onyxchain Authors
* This file is part of The onyxchain library.
*
* The onyxchain is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The onyxchain is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The onyxchain. If not, see <http://www.gnu.org/licenses/>.
*/
package vconfig
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"time"
"github.com/OnyxPay/OnyxChain/common"
"github.com/OnyxPay/OnyxChain/common/serialization"
)
var (
Version uint32 = 1
)
type PeerConfig struct {
Index uint32 `json:"index"`
ID string `json:"id"`
}
type ChainConfig struct {
Version uint32 `json:"version"` // software version
View uint32 `json:"view"` // config-updated version
N uint32 `json:"n"` // network size
C uint32 `json:"c"` // consensus quorum
BlockMsgDelay time.Duration `json:"block_msg_delay"`
HashMsgDelay time.Duration `json:"hash_msg_delay"`
PeerHandshakeTimeout time.Duration `json:"peer_handshake_timeout"`
Peers []*PeerConfig `json:"peers"`
PosTable []uint32 `json:"pos_table"`
MaxBlockChangeView uint32 `json:"MaxBlockChangeView"`
}
//
// VBFT consensus payload, stored on each block header
//
type VbftBlockInfo struct {
Proposer uint32 `json:"leader"`
VrfValue []byte `json:"vrf_value"`
VrfProof []byte `json:"vrf_proof"`
LastConfigBlockNum uint32 `json:"last_config_block_num"`
NewChainConfig *ChainConfig `json:"new_chain_config"`
}
const (
VRF_SIZE = 64 // bytes
MAX_PROPOSER_COUNT = 32
MAX_ENDORSER_COUNT = 240
MAX_COMMITTER_COUNT = 240
)
type VRFValue [VRF_SIZE]byte
var NilVRF = VRFValue{}
func (v VRFValue) Bytes() []byte {
return v[:]
}
func (v VRFValue) IsNil() bool {
return bytes.Compare(v.Bytes(), NilVRF.Bytes()) == 0
}
func VerifyChainConfig(cfg *ChainConfig) error {
// TODO
return nil
}
//Serialize the ChainConfig
func (cc *ChainConfig) Serialize(w io.Writer) error {
data, err := json.Marshal(cc)
if err != nil {
return err
}
if _, err := w.Write(data); err != nil {
return err
}
return nil
}
func (pc *PeerConfig) Serialize(w io.Writer) error {
if err := serialization.WriteUint32(w, pc.Index); err != nil {
return fmt.Errorf("ChainConfig peer index length serialization failed %s", err)
}
if err := serialization.WriteString(w, pc.ID); err != nil {
return fmt.Errorf("ChainConfig peer ID length serialization failed %s", err)
}
return nil
}
func (pc *PeerConfig) Deserialize(r io.Reader) error {
index, err := serialization.ReadUint32(r)
if err != nil {
return fmt.Errorf("serialization PeerConfig index err:%s", err)
}
pc.Index = index
nodeid, err := serialization.ReadString(r)
if err != nil {
return fmt.Errorf("serialization PeerConfig nodeid err:%s", err)
}
pc.ID = nodeid
return nil
}
func (cc *ChainConfig) Hash() common.Uint256 {
buf := new(bytes.Buffer)
cc.Serialize(buf)
hash := sha256.Sum256(buf.Bytes())
return hash
}