forked from perlin-network/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
181 lines (142 loc) · 4.75 KB
/
protocol.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
// Copyright (c) 2019 Perlin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package wavelet
import (
"bytes"
"context"
"fmt"
"github.com/perlin-network/wavelet/log"
"github.com/perlin-network/wavelet/sys"
"github.com/pkg/errors"
"golang.org/x/crypto/blake2b"
)
type Protocol struct {
ledger *Ledger
}
func (p *Protocol) Gossip(stream Wavelet_GossipServer) error {
for {
batch, err := stream.Recv()
if err != nil {
return err
}
for _, buf := range batch.Transactions {
tx, err := UnmarshalTransaction(bytes.NewReader(buf))
if err != nil {
logger := log.TX("gossip")
logger.Err(err).Msg("Failed to unmarshal transaction")
continue
}
if err := p.ledger.AddTransaction(tx); err != nil && errors.Cause(err) != ErrMissingParents {
fmt.Printf("error adding incoming tx to graph [%v]: %+v\n", err, tx)
}
}
}
}
func (p *Protocol) Query(ctx context.Context, req *QueryRequest) (*QueryResponse, error) {
res := &QueryResponse{}
round, err := p.ledger.rounds.GetByIndex(req.RoundIndex)
if err == nil {
res.Round = round.Marshal()
return res, nil
}
preferred := p.ledger.finalizer.Preferred()
if preferred != nil {
res.Round = preferred.(*Round).Marshal()
return res, nil
}
return res, nil
}
func (p *Protocol) Sync(stream Wavelet_SyncServer) error {
req, err := stream.Recv()
if err != nil {
return err
}
res := &SyncResponse{}
diff := p.ledger.accounts.Snapshot().DumpDiff(req.GetRoundId())
header := &SyncInfo{LatestRound: p.ledger.rounds.Latest().Marshal()}
for i := 0; i < len(diff); i += sys.SyncChunkSize {
end := i + sys.SyncChunkSize
if end > len(diff) {
end = len(diff)
}
checksum := blake2b.Sum256(diff[i:end])
p.ledger.cacheChunks.Put(checksum, diff[i:end])
header.Checksums = append(header.Checksums, checksum[:])
}
res.Data = &SyncResponse_Header{Header: header}
if err := stream.Send(res); err != nil {
return err
}
res.Data = &SyncResponse_Chunk{}
for {
req, err := stream.Recv()
if err != nil {
return err
}
var checksum [blake2b.Size256]byte
copy(checksum[:], req.GetChecksum())
if chunk, found := p.ledger.cacheChunks.Load(checksum); found {
chunk := chunk.([]byte)
logger := log.Sync("provide_chunk")
logger.Info().
Hex("requested_hash", req.GetChecksum()).
Msg("Responded to sync chunk request.")
res.Data.(*SyncResponse_Chunk).Chunk = chunk
} else {
res.Data.(*SyncResponse_Chunk).Chunk = nil
}
if err = stream.Send(res); err != nil {
return err
}
}
}
func (p *Protocol) CheckOutOfSync(ctx context.Context, req *OutOfSyncRequest) (*OutOfSyncResponse, error) {
return &OutOfSyncResponse{
OutOfSync: p.ledger.rounds.Latest().Index >= sys.SyncIfRoundsDifferBy+req.RoundIndex,
}, nil
}
func (p *Protocol) DownloadMissingTx(ctx context.Context, req *DownloadMissingTxRequest) (*DownloadTxResponse, error) {
res := &DownloadTxResponse{Transactions: make([][]byte, 0, len(req.Ids))}
for _, buf := range req.Ids {
var id TransactionID
copy(id[:], buf)
if tx := p.ledger.Graph().FindTransaction(id); tx != nil {
res.Transactions = append(res.Transactions, tx.Marshal())
}
}
return res, nil
}
func (p *Protocol) DownloadTx(ctx context.Context, req *DownloadTxRequest) (*DownloadTxResponse, error) {
lowLimit := req.Depth - sys.MaxDepthDiff
highLimit := req.Depth + sys.MaxDownloadDepthDiff
receivedIDs := make(map[TransactionID]struct{}, len(req.SkipIds))
for _, buf := range req.SkipIds {
var id TransactionID
copy(id[:], buf)
receivedIDs[id] = struct{}{}
}
var txs [][]byte
hostTXs := p.ledger.Graph().GetTransactionsByDepth(&lowLimit, &highLimit)
for _, tx := range hostTXs {
if _, ok := receivedIDs[tx.ID]; !ok {
txs = append(txs, tx.Marshal())
}
}
return &DownloadTxResponse{Transactions: txs}, nil
}