-
Notifications
You must be signed in to change notification settings - Fork 40
/
handlerv2.go
244 lines (231 loc) · 7.39 KB
/
handlerv2.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
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright 2021 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package block
import (
"bytes"
"io"
"github.com/icon-project/goloop/btp"
"github.com/icon-project/goloop/chain/base"
"github.com/icon-project/goloop/common/atomic"
"github.com/icon-project/goloop/common/db"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/module"
"github.com/icon-project/goloop/service"
"github.com/icon-project/goloop/service/txresult"
)
type blockV2Handler struct {
chain base.Chain
sm ServiceManager
}
func NewBlockV2Handler(chain base.Chain) base.BlockHandler {
return &blockV2Handler{
chain: chain,
sm: chain.ServiceManager(),
}
}
func (b *blockV2Handler) Version() int {
return module.BlockVersion2
}
func (b *blockV2Handler) bucketFor(id db.BucketID) (*db.CodedBucket, error) {
return db.NewCodedBucket(b.chain.Database(), id, nil)
}
func (b *blockV2Handler) commitVoteSetFromHash(hash []byte) module.CommitVoteSet {
bk, err := b.bucketFor(db.BytesByHash)
if err != nil {
return nil
}
bs, err := bk.GetBytes(db.Raw(hash))
if err != nil {
return nil
}
dec := b.chain.CommitVoteSetDecoder()
return dec(bs)
}
func (b *blockV2Handler) NewBlock(
height int64, ts int64, proposer module.Address, prev module.Block,
logsBloom module.LogsBloom, result []byte,
patchTransactions module.TransactionList,
normalTransactions module.TransactionList,
nextValidators module.ValidatorList, votes module.CommitVoteSet,
bs module.BTPSection,
) base.Block {
var prevID []byte
if prev != nil {
prevID = prev.ID()
}
return &blockV2{
height: height,
timestamp: ts,
proposer: proposer,
prevID: prevID,
logsBloom: logsBloom,
result: result,
patchTransactions: patchTransactions,
normalTransactions: normalTransactions,
nextValidatorsHash: nextValidators.Hash(),
_nextValidators: nextValidators,
votes: votes,
nsFilter: bs.Digest().NetworkSectionFilter(),
sm: b.sm,
_btpSection: atomic.MakeCache(bs),
_btpDigest: atomic.MakeCache(bs.Digest()),
}
}
func (b *blockV2Handler) NewBlockFromHeaderReader(r io.Reader) (base.Block, error) {
var header V2HeaderFormat
err := v2Codec.Unmarshal(r, &header)
if err != nil {
return nil, err
}
sm := b.sm
patches := sm.TransactionListFromHash(header.PatchTransactionsHash)
if patches == nil {
return nil, errors.Errorf("TransactionListFromHash(%x) failed", header.PatchTransactionsHash)
}
normalTxs := sm.TransactionListFromHash(header.NormalTransactionsHash)
if normalTxs == nil {
return nil, errors.Errorf("TransactionListFromHash(%x) failed", header.NormalTransactionsHash)
}
nextValidators := sm.ValidatorListFromHash(header.NextValidatorsHash)
if nextValidators == nil {
return nil, errors.Errorf("ValidatorListFromHas(%x)", header.NextValidatorsHash)
}
votes := b.commitVoteSetFromHash(header.VotesHash)
if votes == nil {
return nil, errors.Errorf("commitVoteSetFromHash(%x) failed", header.VotesHash)
}
proposer, err := newProposer(header.Proposer)
if err != nil {
return nil, err
}
return &blockV2{
height: header.Height,
timestamp: header.Timestamp,
proposer: proposer,
prevID: header.PrevID,
logsBloom: txresult.NewLogsBloomFromCompressed(header.LogsBloom),
result: header.Result,
patchTransactions: patches,
normalTransactions: normalTxs,
nextValidatorsHash: nextValidators.Hash(),
_nextValidators: nextValidators,
votes: votes,
nsFilter: module.BitSetFilterFromBytes(header.NSFilter, btp.NSFilterCap),
sm: b.sm,
}, nil
}
func newTransactionListFromBSS(
sm ServiceManager, bss [][]byte, version int,
) (module.TransactionList, error) {
ts := make([]module.Transaction, len(bss))
for i, bs := range bss {
if tx, err := sm.TransactionFromBytes(bs, version); err != nil {
return nil, err
} else {
ts[i] = tx
}
}
return sm.TransactionListFromSlice(ts, version), nil
}
func (b *blockV2Handler) NewBlockDataFromReader(r io.Reader) (base.BlockData, error) {
sm := b.sm
var headerFormat V2HeaderFormat
err := v2Codec.Unmarshal(r, &headerFormat)
if err != nil {
return nil, err
}
var bodyFormat V2BodyFormat
err = v2Codec.Unmarshal(r, &bodyFormat)
if err != nil {
return nil, err
}
patches, err := newTransactionListFromBSS(
sm,
bodyFormat.PatchTransactions,
module.BlockVersion2,
)
if err != nil {
return nil, err
}
if !bytes.Equal(patches.Hash(), headerFormat.PatchTransactionsHash) {
return nil, errors.New("bad patch transactions hash")
}
normalTxs, err := newTransactionListFromBSS(
sm,
bodyFormat.NormalTransactions,
module.BlockVersion2,
)
if err != nil {
return nil, err
}
if !bytes.Equal(normalTxs.Hash(), headerFormat.NormalTransactionsHash) {
return nil, errors.New("bad normal transactions hash")
}
// nextValidators may be nil
nextValidators := sm.ValidatorListFromHash(headerFormat.NextValidatorsHash)
votes := b.chain.CommitVoteSetDecoder()(bodyFormat.Votes)
if votes == nil {
return nil, errors.Errorf("invalid votes. bytes=%x", bodyFormat.Votes)
}
if !bytes.Equal(votes.Hash(), headerFormat.VotesHash) {
return nil, errors.New("bad vote list hash")
}
bd, err := btp.NewDigestFromBytes(bodyFormat.BTPDigest)
if err != nil {
return nil, err
}
bdHashInResult, err := service.BTPDigestHashFromResult(headerFormat.Result)
if err != nil {
return nil, err
}
if !bytes.Equal(bdHashInResult, bd.Hash()) {
return nil, errors.Errorf("bad digestHash header=%x calc=%x", bdHashInResult, bd.Hash())
}
filter := bd.NetworkSectionFilter()
if !bytes.Equal(headerFormat.NSFilter, filter.Bytes()) {
return nil, errors.Errorf("bad nsFilter header=%x fromBD=%x", headerFormat.NSFilter, filter.Bytes())
}
proposer, err := newProposer(headerFormat.Proposer)
if err != nil {
return nil, err
}
return &blockV2{
height: headerFormat.Height,
timestamp: headerFormat.Timestamp,
proposer: proposer,
prevID: headerFormat.PrevID,
logsBloom: txresult.NewLogsBloomFromCompressed(headerFormat.LogsBloom),
result: headerFormat.Result,
patchTransactions: patches,
normalTransactions: normalTxs,
nextValidatorsHash: headerFormat.NextValidatorsHash,
_nextValidators: nextValidators,
votes: votes,
nsFilter: module.BitSetFilterFromBytes(headerFormat.NSFilter, btp.NSFilterCap),
sm: b.sm,
_btpDigest: atomic.MakeCache(bd),
}, nil
}
func (b *blockV2Handler) GetBlock(id []byte) (base.Block, error) {
dbase := b.chain.Database()
headerBytes, err := db.DoGetWithBucketID(dbase, db.BytesByHash, id)
if errors.NotFoundError.Equals(err) {
return nil, errors.WithStack(errors.ErrUnsupported)
} else if err != nil {
return nil, err
}
return b.NewBlockFromHeaderReader(bytes.NewReader(headerBytes))
}