-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitset.go
74 lines (65 loc) · 2.57 KB
/
bitset.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
/*
* Copyright 2018 Insolar
*
* 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 packets
import (
"github.com/insolar/insolar/core"
"github.com/pkg/errors"
)
// TriState is state of the communicating node
type TriState uint8
const (
// Legit is bit indicating OK data from node
Legit TriState = iota + 1
// TimedOut is bit indicating that timeout occurred when communicating with node
TimedOut
// Fraud is bit indicating that the node is malicious (fraud)
Fraud
)
// BitSetCell is structure that contains the state of the node
type BitSetCell struct {
NodeID core.RecordRef
State TriState
}
// Possible errors in BitSetMapper
var (
// ErrBitSetOutOfRange is returned when index passed to IndexToRef function is out of range (ERROR)
ErrBitSetOutOfRange = errors.New("index out of range")
// ErrBitSetNodeIsMissing is returned in IndexToRef when we have no information about the node on specified index (SPECIAL CASE)
ErrBitSetNodeIsMissing = errors.New("no information about node on specified index")
// ErrBitSetIncorrectNode is returned when an incorrect node is passed to RefToIndex (ERROR)
ErrBitSetIncorrectNode = errors.New("incorrect node ID")
)
// BitSetMapper contains the mapping from bitset index to node ID (and vice versa)
type BitSetMapper interface {
// IndexToRef get ID of the node that is stored on the specified internal index
IndexToRef(index int) (core.RecordRef, error)
// RefToIndex get bitset internal index where the specified node state is stored
RefToIndex(nodeID core.RecordRef) (int, error)
// Length returns required length of the bitset
Length() int
}
// BitSet is interface
type BitSet interface {
Serialize() ([]byte, error)
// GetCells get buckets of bitset
GetCells(mapper BitSetMapper) ([]BitSetCell, error)
// ApplyChanges returns copy of the current bitset with changes applied
ApplyChanges(changes []BitSetCell, mapper BitSetMapper) error
}
// NewBitSet creates bitset from a set of buckets and the mapper. Size == cells count.
func NewBitSet(size int) (BitSet, error) {
return NewTriStateBitSet(size)
}