-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathchannelstate.go
187 lines (154 loc) · 5.9 KB
/
channelstate.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
package server
import (
"bytes"
graphsync "github.com/filecoin-project/boost-graphsync"
"github.com/filecoin-project/boost/datatransfer"
"github.com/filecoin-project/boost/retrievalmarket/types/legacyretrievaltypes"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/libp2p/go-libp2p/core/peer"
cbg "github.com/whyrusleeping/cbor-gen"
)
type RetrievalType string
const RetrievalTypeDeal RetrievalType = "Deal"
const RetrievalTypeLegs RetrievalType = "Legs"
type retrievalState struct {
retType RetrievalType
cs *channelState
mkts *legacyretrievaltypes.ProviderDealState
gsReq graphsync.RequestID
}
func (r retrievalState) ChannelState() channelState { return *r.cs }
func (r retrievalState) ProviderDealState() legacyretrievaltypes.ProviderDealState { return *r.mkts }
// channelState is immutable channel data plus mutable state
type channelState struct {
// peerId of the manager peer
selfPeer peer.ID
// an identifier for this channel shared by request and responder, set by requester through protocol
transferID datatransfer.TransferID
// base CID for the piece being transferred
baseCid cid.Cid
// portion of Piece to return, specified by an IPLD selector
selector *cbg.Deferred
// the party that is sending the data (not who initiated the request)
sender peer.ID
// the party that is receiving the data (not who initiated the request)
recipient peer.ID
// expected amount of data to be transferred
totalSize uint64
// current status of this deal
status datatransfer.Status
// isPull indicates if this is a push or pull request
isPull bool
// total bytes read from this node and queued for sending (0 if receiver)
queued uint64
// total bytes sent from this node (0 if receiver)
sent uint64
// total bytes received by this node (0 if sender)
received uint64
// number of blocks that have been received, including blocks that are
// present in more than one place in the DAG
receivedBlocksTotal int64
// Number of blocks that have been queued, including blocks that are
// present in more than one place in the DAG
queuedBlocksTotal int64
// Number of blocks that have been sent, including blocks that are
// present in more than one place in the DAG
sentBlocksTotal int64
// more informative status on a channel
message string
}
func (c channelState) Vouchers() []datatransfer.Voucher {
//TODO implement me
panic("implement me")
}
func (c channelState) VoucherResults() []datatransfer.VoucherResult {
//TODO implement me
panic("implement me")
}
func (c channelState) LastVoucher() datatransfer.Voucher {
//TODO implement me
panic("implement me")
}
func (c channelState) LastVoucherResult() datatransfer.VoucherResult {
//TODO implement me
panic("implement me")
}
// EmptyChannelState is the zero value for channel state, meaning not present
var EmptyChannelState = channelState{}
// Status is the current status of this channel
func (c channelState) Status() datatransfer.Status { return c.status }
// Received returns the number of bytes received
func (c channelState) Queued() uint64 { return c.queued }
// Sent returns the number of bytes sent
func (c channelState) Sent() uint64 { return c.sent }
// Received returns the number of bytes received
func (c channelState) Received() uint64 { return c.received }
// TransferID returns the transfer id for this channel
func (c channelState) TransferID() datatransfer.TransferID { return c.transferID }
// BaseCID returns the CID that is at the root of this data transfer
func (c channelState) BaseCID() cid.Cid { return c.baseCid }
// Selector returns the IPLD selector for this data transfer (represented as
// an IPLD node)
func (c channelState) Selector() ipld.Node {
builder := basicnode.Prototype.Any.NewBuilder()
reader := bytes.NewReader(c.selector.Raw)
err := dagcbor.Decode(builder, reader)
if err != nil {
log.Error(err)
}
return builder.Build()
}
// Voucher returns the voucher for this data transfer
func (c channelState) Voucher() datatransfer.Voucher {
return nil
}
// ReceivedCidsTotal returns the number of (non-unique) cids received so far
// on the channel - note that a block can exist in more than one place in the DAG
func (c channelState) ReceivedCidsTotal() int64 {
return c.receivedBlocksTotal
}
// QueuedCidsTotal returns the number of (non-unique) cids queued so far
// on the channel - note that a block can exist in more than one place in the DAG
func (c channelState) QueuedCidsTotal() int64 {
return c.queuedBlocksTotal
}
// SentCidsTotal returns the number of (non-unique) cids sent so far
// on the channel - note that a block can exist in more than one place in the DAG
func (c channelState) SentCidsTotal() int64 {
return c.sentBlocksTotal
}
// Sender returns the peer id for the node that is sending data
func (c channelState) Sender() peer.ID { return c.sender }
// Recipient returns the peer id for the node that is receiving data
func (c channelState) Recipient() peer.ID { return c.recipient }
// TotalSize returns the total size for the data being transferred
func (c channelState) TotalSize() uint64 { return c.totalSize }
// IsPull returns whether this is a pull request based on who initiated it
func (c channelState) IsPull() bool {
return c.isPull
}
func (c channelState) ChannelID() datatransfer.ChannelID {
if c.isPull {
return datatransfer.ChannelID{ID: c.transferID, Initiator: c.recipient, Responder: c.sender}
}
return datatransfer.ChannelID{ID: c.transferID, Initiator: c.sender, Responder: c.recipient}
}
func (c channelState) Message() string {
return c.message
}
func (c channelState) SelfPeer() peer.ID {
return c.selfPeer
}
func (c channelState) OtherPeer() peer.ID {
if c.sender == c.selfPeer {
return c.recipient
}
return c.sender
}
func (c channelState) Stages() *datatransfer.ChannelStages {
return nil
}
var _ datatransfer.ChannelState = channelState{}