/
snapshot.go
231 lines (208 loc) · 6.87 KB
/
snapshot.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2019 The go-smilo Authors
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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 go-ethereum library 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package backend
import (
"bytes"
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"go-smilo/src/blockchain/smilobft/consensus/sport"
"go-smilo/src/blockchain/smilobft/core/types"
"go-smilo/src/blockchain/smilobft/ethdb"
)
const (
dbKeySnapshotPrefix = "smilobft-snapshot"
)
// newSnapshot (clique override) create a new snapshot with the specified startup parameters. This
// method does not initialize the set of recent fullnodes, so only ever use if for the genesis block.
func newSnapshot(epoch uint64, number uint64, hash common.Hash, fullnodeSet sport.FullnodeSet) *Snapshot {
snap := &Snapshot{
Epoch: epoch,
Number: number,
Hash: hash,
FullnodeSet: fullnodeSet,
Tally: make(map[common.Address]Tally),
}
return snap
}
// loadSnapshot (clique override) loads an existing snapshot from the database.
func loadSnapshot(epoch uint64, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
blob, err := db.Get(append([]byte(dbKeySnapshotPrefix), hash[:]...))
if err != nil {
return nil, err
}
snap := new(Snapshot)
if err := json.Unmarshal(blob, snap); err != nil {
return nil, err
}
snap.Epoch = epoch
return snap, nil
}
// store (clique override) inserts the snapshot into the database.
func (s *Snapshot) store(db ethdb.Database) error {
blob, err := json.Marshal(s)
if err != nil {
return err
}
return db.Put(append([]byte(dbKeySnapshotPrefix), s.Hash[:]...), blob)
}
// copy (clique override) creates a deep copy of the snapshot, though not the individual votes.
func (s *Snapshot) copy() *Snapshot {
cpy := &Snapshot{
Epoch: s.Epoch,
Number: s.Number,
Hash: s.Hash,
FullnodeSet: s.FullnodeSet.Copy(),
Votes: make([]*Vote, len(s.Votes)),
Tally: make(map[common.Address]Tally),
}
for address, tally := range s.Tally {
cpy.Tally[address] = tally
}
copy(cpy.Votes, s.Votes)
return cpy
}
// checkVote (clique override) return whether it's a valid vote
func (s *Snapshot) checkVote(address common.Address, authorize bool) bool {
_, fullnode := s.FullnodeSet.GetByAddress(address)
return (fullnode != nil && !authorize) || (fullnode == nil && authorize)
}
// cast (clique override) adds a new vote into the tally.
func (s *Snapshot) cast(address common.Address, authorize bool) bool {
// Ensure the vote is meaningful
if !s.checkVote(address, authorize) {
return false
}
// Cast the vote into an existing or new tally
if old, ok := s.Tally[address]; ok {
old.Votes++
s.Tally[address] = old
} else {
s.Tally[address] = Tally{Authorize: authorize, Votes: 1}
}
return true
}
// uncast (clique override) removes a previously cast vote from the tally.
func (s *Snapshot) uncast(address common.Address, authorize bool) bool {
// If there's no tally, it's a dangling vote, just drop
tally, ok := s.Tally[address]
if !ok {
return false
}
// Ensure we only revert counted votes
if tally.Authorize != authorize {
return false
}
// Otherwise revert the vote
if tally.Votes > 1 {
tally.Votes--
s.Tally[address] = tally
} else {
delete(s.Tally, address)
}
return true
}
// apply (clique override) creates a new authorization snapshot by applying the given headers to the original one.
func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
// Allow passing in no headers for cleaner code
if len(headers) == 0 {
return s, nil
}
// Sanity check that the headers can be applied
for i := 0; i < len(headers)-1; i++ {
if headers[i+1].Number.Uint64() != headers[i].Number.Uint64()+1 {
return nil, errInvalidVotingChain
}
}
if headers[0].Number.Uint64() != s.Number+1 {
return nil, errInvalidVotingChain
}
// Iterate through the headers and create a new snapshot
snap := s.copy()
for _, header := range headers {
// Remove any votes on checkpoint blocks
number := header.Number.Uint64()
if number%s.Epoch == 0 {
snap.Votes = nil
snap.Tally = make(map[common.Address]Tally)
}
// Resolve the authorization key and check against fullnodes
fullnode, err := ecrecover(header)
if err != nil {
return nil, err
}
if _, v := snap.FullnodeSet.GetByAddress(fullnode); v == nil {
return nil, errUnauthorized
}
// Header authorized, discard any previous votes from the fullnode
for i, vote := range snap.Votes {
if vote.Fullnode == fullnode && vote.Address == header.Coinbase {
// Uncast the vote from the cached tally
snap.uncast(vote.Address, vote.Authorize)
// Uncast the vote from the chronological list
snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
break // only one vote allowed
}
}
// Tally up the new vote from the fullnode
var authorize bool
switch {
case bytes.Equal(header.Nonce[:], nonceAuthVote):
authorize = true
case bytes.Equal(header.Nonce[:], nonceDropVote):
authorize = false
default:
return nil, errInvalidVote
}
if snap.cast(header.Coinbase, authorize) {
snap.Votes = append(snap.Votes, &Vote{
Fullnode: fullnode,
BlockNum: number,
Address: header.Coinbase,
Authorize: authorize,
})
}
// If the vote passed, update the list of fullnodes
if tally := snap.Tally[header.Coinbase]; tally.Votes > snap.FullnodeSet.Size()/2 {
if tally.Authorize {
snap.FullnodeSet.AddFullnode(header.Coinbase)
} else {
snap.FullnodeSet.RemoveFullnode(header.Coinbase)
// Discard any previous votes the deauthorized fullnode cast
for i := 0; i < len(snap.Votes); i++ {
if snap.Votes[i].Fullnode == header.Coinbase {
// Uncast the vote from the cached tally
snap.uncast(snap.Votes[i].Address, snap.Votes[i].Authorize)
// Uncast the vote from the chronological list
snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
i--
}
}
}
// Discard any previous votes around the just changed account
for i := 0; i < len(snap.Votes); i++ {
if snap.Votes[i].Address == header.Coinbase {
snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
i--
}
}
delete(snap.Tally, header.Coinbase)
}
}
snap.Number += uint64(len(headers))
snap.Hash = headers[len(headers)-1].Hash()
return snap, nil
}