/
group.go
158 lines (127 loc) · 5.3 KB
/
group.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
// Copyright (C) 2019 ZVChain
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package types
import "github.com/zvchain/zvchain/common"
// SeedI is the seed block which the group-create routine based on
type SeedI interface {
Seed() common.Hash
}
type SenderI interface {
Sender() []byte
}
// SharePiecePacket contains share piece data generated during the group-create routine
type SharePiecePacket interface {
SeedI
SenderI
Pieces() []byte // Encrypted pieces data
}
// EncryptedSharePiecePacket is encrypted share piece data and only the corresponding receiver can decrypt it
type EncryptedSharePiecePacket interface {
SharePiecePacket
Pubkey0() []byte // Initial Pubkey
}
// OriginSharePiecePacket is the origin share piece data which is not encrypted
type OriginSharePiecePacket interface {
SharePiecePacket
EncSeckey() []byte
}
// MpkPacket contains the signature pubkey of the group which is aggregated from the share pieces received
type MpkPacket interface {
SeedI
SenderI
Mpk() []byte // Pubkey aggregated
Sign() []byte // Signature to the seed signed by the Mpk
}
// MemberI is the group member interface
type MemberI interface {
ID() []byte
PK() []byte
}
type CreateResultCode int
const (
CreateResultSuccess CreateResultCode = 1 // Group create success
CreateResultMarkEvil CreateResultCode = 2 // Someone cheat, and mark the origin pieces required
CreateResultFail CreateResultCode = 3 // Error occurs
CreateResultIdle CreateResultCode = 4 // Idle round, won't start group-create routine
)
// GroupI is the group info interface
type GroupI interface {
Header() GroupHeaderI
Members() []MemberI
}
// CreateResult is the group-create result presentation
type CreateResult interface {
SeedI
Code() CreateResultCode // Seen in the CreateResultCode enum above
GroupInfo() GroupI // Return new groupInfo if create success
FrozenMiners() [][]byte // Determines miners to be froze
Err() error // Error occurs
}
// GroupHeaderI is the group header info interface
type GroupHeaderI interface {
SeedI
WorkHeight() uint64
DismissHeight() uint64
PublicKey() []byte
Threshold() uint32
GroupHeight() uint64
}
// PunishmentMsg is the punishment message when someone cheats
type PunishmentMsg interface {
PenaltyTarget() [][]byte // Determines miners to be punished
RewardTarget() [][]byte // Determines miners to be rewarded
}
type CheckerContext interface {
Height() uint64
}
// GroupCreateChecker provides function to check if the group-create related packets are legal
type GroupCreateChecker interface {
// CheckEncryptedPiecePacket checks the encrypted share piece packet
CheckEncryptedPiecePacket(packet EncryptedSharePiecePacket, ctx CheckerContext) error
// CheckMpkPacket checks if the mpk packet legal
CheckMpkPacket(packet MpkPacket, ctx CheckerContext) error
// CheckGroupCreateResult checks if the group-create is success
CheckGroupCreateResult(ctx CheckerContext) CreateResult
// CheckOriginPiecePacket checks the origin share pieces packet is legal
CheckOriginPiecePacket(packet OriginSharePiecePacket, ctx CheckerContext) error
// CheckGroupCreatePunishment determines miners to be punished or rewarded
CheckGroupCreatePunishment(ctx CheckerContext) (PunishmentMsg, error)
}
// GroupStoreReader provides function to access the data generated during the routine or group info
type GroupStoreReader interface {
// GetEncryptedPiecePackets Get the encrypted share packet of the given seed
GetEncryptedPiecePackets(seed SeedI) ([]EncryptedSharePiecePacket, error)
// HasSentEncryptedPiecePacket checks if the given sender has sent the packet yet
HasSentEncryptedPiecePacket(sender []byte, seed SeedI) bool
// HasSentMpkPacket checks if the given sender has sent the packet yet
HasSentMpkPacket(sender []byte, seed SeedI) bool
// GetMpkPackets Get the mpk packet of the given seed
GetMpkPackets(seed SeedI) ([]MpkPacket, error)
// IsOriginPieceRequired check if origin piece is required of the given seed
IsOriginPieceRequired(seed SeedI) bool
// GetOriginPiecePackets returns the origin share piece data of the given seed
GetOriginPiecePackets(seed SeedI) ([]OriginSharePiecePacket, error)
// HasSentOriginPiecePacket checks if the given sender has sent the packet yet
HasSentOriginPiecePacket(sender []byte, seed SeedI) bool
}
// GroupPacketSender provides functions for sending packets
type GroupPacketSender interface {
// SendEncryptedPiecePacket sends the encrypted packet to the pool
SendEncryptedPiecePacket(packet EncryptedSharePiecePacket) error
// SendMpkPacket sends the mpk packet to the pool
SendMpkPacket(packet MpkPacket) error
// SendOriginPiecePacket sends the origin packet to the pool
SendOriginPiecePacket(packet OriginSharePiecePacket) error
}