forked from making-choice-personal/volary-opera
/
types.go
82 lines (66 loc) · 1.54 KB
/
types.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
package dagstream
import (
"bytes"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/Fantom-foundation/lachesis-base/gossip/basestream"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/dag"
)
type Request struct {
Session Session
Limit dag.Metric
Type basestream.RequestType
MaxChunks uint32
}
type Response struct {
SessionID uint32
Done bool
IDs hash.Events
Events []rlp.RawValue
}
type Session struct {
ID uint32
Start Locator
Stop Locator
}
type Locator []byte
func (l Locator) Compare(b basestream.Locator) int {
return bytes.Compare(l, b.(Locator))
}
func (l Locator) Inc() basestream.Locator {
nextBn := new(big.Int).SetBytes(l)
nextBn.Add(nextBn, common.Big1)
return Locator(common.LeftPadBytes(nextBn.Bytes(), len(l)))
}
type Payload struct {
IDs hash.Events
Events []rlp.RawValue
Size uint64
}
func (p *Payload) AddEvent(id hash.Event, eventB rlp.RawValue) {
p.IDs = append(p.IDs, id)
p.Events = append(p.Events, eventB)
p.Size += uint64(len(eventB))
}
func (p *Payload) AddID(id hash.Event, size int) {
p.IDs = append(p.IDs, id)
p.Size += uint64(size)
}
func (p Payload) Len() int {
return len(p.IDs)
}
func (p Payload) TotalSize() uint64 {
return p.Size
}
func (p Payload) TotalMemSize() int {
if len(p.Events) != 0 {
return int(p.Size) + len(p.IDs)*128
}
return len(p.IDs) * 128
}
const (
RequestIDs basestream.RequestType = 0
RequestEvents basestream.RequestType = 2
)