-
Notifications
You must be signed in to change notification settings - Fork 0
/
localcoinmsgstate.go
110 lines (88 loc) · 3.53 KB
/
localcoinmsgstate.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
/*
github.com/tcrain/cons - Experimental project for testing and scaling consensus algorithms.
Copyright (C) 2020 The project authors - tcrain
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 localcoin
import (
"github.com/tcrain/cons/consensus/consinterface"
"github.com/tcrain/cons/consensus/deserialized"
"github.com/tcrain/cons/consensus/generalconfig"
"github.com/tcrain/cons/consensus/types"
"github.com/tcrain/cons/consensus/utils"
"math/rand"
"sync"
)
// LocalCoinMsgState represents the state of the local coin.
type MsgState struct {
mutex sync.Mutex
coinVal map[types.ConsensusRound]types.BinVal
fixedCoinRand *rand.Rand // Used if we are using a fixed coin values for experiment repeatability
fixedCoinUint uint64 // Used if we are using a fixed coin values for experiment repeatability
}
// NewLocalCoinMsgState generates a new LocalCoinMsgState object.
func NewLocalCoinMsgState(isMv bool, seed int64,
_ *generalconfig.GeneralConfig) *MsgState {
_ = isMv // TODO
var fixedRand *rand.Rand
var fixedCoinUint uint64
fixedRand = rand.New(rand.NewSource(seed))
fixedCoinUint = fixedRand.Uint64()
return &MsgState{
fixedCoinRand: fixedRand,
fixedCoinUint: fixedCoinUint}
}
// New creates a new empty LocalCoinMsgState object for the consensus index idx.
func (sms *MsgState) New(_ types.ConsensusIndex, presets []struct {
Round types.ConsensusRound
Val types.BinVal
}) consinterface.CoinMessageStateInterface {
var fixedCoinUint uint64
fixedCoinUint = sms.fixedCoinRand.Uint64()
ret := &MsgState{
fixedCoinRand: sms.fixedCoinRand,
fixedCoinUint: fixedCoinUint,
coinVal: make(map[types.ConsensusRound]types.BinVal)}
for _, nxt := range presets {
ret.coinVal[nxt.Round] = nxt.Val
}
return ret
}
// GetCoinSignType returns what type of signature is used to sign coin messages
func (sms *MsgState) GetCoinSignType() types.SignType {
return types.NormalSignature
}
// GetCoins returns the set of binary coin values that are currently valid.
func (sms *MsgState) GetCoins(round types.ConsensusRound) []types.BinVal {
sms.mutex.Lock()
defer sms.mutex.Unlock()
return []types.BinVal{sms.getCoin(round)}
}
func (sms *MsgState) getCoin(round types.ConsensusRound) types.BinVal {
if val, ok := sms.coinVal[round]; ok {
return val
}
val := utils.GetBitAt(uint(round)%64, sms.fixedCoinUint)
sms.coinVal[round] = val
return val
}
// CheckFinishedMessage checks if the message is for the coin and if the coin is already known.
// If so true is returned, false otherwise.
func (sms *MsgState) CheckFinishedMessage(deser *deserialized.DeserializedItem) bool {
return false
}
// GotMsg processes messages of type CoinMessage. Once an n-t threshold of these messages have been received
// from different processes the value of the coin is revealed.
func (sms *MsgState) GotMsg(consinterface.MessageState, *deserialized.DeserializedItem,
*generalconfig.GeneralConfig, *consinterface.MemCheckers) (types.ConsensusRound, error) {
return 0, types.ErrInvalidMsgSize
}