|
|
@ -6,6 +6,7 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
"github.com/harmony-one/bls/ffi/go/bls" |
|
|
|
"github.com/harmony-one/bls/ffi/go/bls" |
|
|
|
"github.com/harmony-one/harmony/shard" |
|
|
|
"github.com/harmony-one/harmony/shard" |
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/staking/slash" |
|
|
|
// "github.com/harmony-one/harmony/staking/effective"
|
|
|
|
// "github.com/harmony-one/harmony/staking/effective"
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -75,10 +76,16 @@ type DependencyInjectionWriter interface { |
|
|
|
SetShardIDProvider(func() (uint32, error)) |
|
|
|
SetShardIDProvider(func() (uint32, error)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DependencyInjectionReader ..
|
|
|
|
|
|
|
|
type DependencyInjectionReader interface { |
|
|
|
|
|
|
|
ShardIDProvider() func() (uint32, error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Decider ..
|
|
|
|
// Decider ..
|
|
|
|
type Decider interface { |
|
|
|
type Decider interface { |
|
|
|
SignatureReader |
|
|
|
SignatureReader |
|
|
|
DependencyInjectionWriter |
|
|
|
DependencyInjectionWriter |
|
|
|
|
|
|
|
slash.Slasher |
|
|
|
ToggleActive(*bls.PublicKey) bool |
|
|
|
ToggleActive(*bls.PublicKey) bool |
|
|
|
// UpdateVotingPower(keeper effective.StakeKeeper)
|
|
|
|
// UpdateVotingPower(keeper effective.StakeKeeper)
|
|
|
|
Policy() Policy |
|
|
|
Policy() Policy |
|
|
@ -96,17 +103,14 @@ type cIdentities struct { |
|
|
|
commit map[string]*bls.Sign |
|
|
|
commit map[string]*bls.Sign |
|
|
|
// viewIDSigs: every validator
|
|
|
|
// viewIDSigs: every validator
|
|
|
|
// sign on |viewID|blockHash| in view changing message
|
|
|
|
// sign on |viewID|blockHash| in view changing message
|
|
|
|
viewID map[string]*bls.Sign |
|
|
|
viewID map[string]*bls.Sign |
|
|
|
|
|
|
|
seenCounter map[[shard.PublicKeySizeInBytes]byte]int |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type depInject struct { |
|
|
|
type depInject struct { |
|
|
|
shardIDProvider func() (uint32, error) |
|
|
|
shardIDProvider func() (uint32, error) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (d *depInject) SetShardIDProvider(p func() (uint32, error)) { |
|
|
|
|
|
|
|
d.shardIDProvider = p |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cIdentities) IndexOf(pubKey *bls.PublicKey) int { |
|
|
|
func (s *cIdentities) IndexOf(pubKey *bls.PublicKey) int { |
|
|
|
idx := -1 |
|
|
|
idx := -1 |
|
|
|
for k, v := range s.publicKeys { |
|
|
|
for k, v := range s.publicKeys { |
|
|
@ -132,9 +136,23 @@ func (s *cIdentities) Participants() []*bls.PublicKey { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *cIdentities) UpdateParticipants(pubKeys []*bls.PublicKey) { |
|
|
|
func (s *cIdentities) UpdateParticipants(pubKeys []*bls.PublicKey) { |
|
|
|
|
|
|
|
// TODO - might need to put this in separate method
|
|
|
|
|
|
|
|
s.seenCounter = make(map[[shard.PublicKeySizeInBytes]byte]int, len(pubKeys)) |
|
|
|
|
|
|
|
for i := range pubKeys { |
|
|
|
|
|
|
|
k := shard.BlsPublicKey{} |
|
|
|
|
|
|
|
k.FromLibBLSPublicKey(pubKeys[i]) |
|
|
|
|
|
|
|
s.seenCounter[k] = 0 |
|
|
|
|
|
|
|
} |
|
|
|
s.publicKeys = append(pubKeys[:0:0], pubKeys...) |
|
|
|
s.publicKeys = append(pubKeys[:0:0], pubKeys...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *cIdentities) SlashThresholdMet(key shard.BlsPublicKey) bool { |
|
|
|
|
|
|
|
s.seenCounter[key]++ |
|
|
|
|
|
|
|
fmt.Println("Slash Map", s.seenCounter) |
|
|
|
|
|
|
|
return s.seenCounter[key] == slash.UnavailabilityInConsecutiveBlockSigning |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *cIdentities) DumpParticipants() []string { |
|
|
|
func (s *cIdentities) DumpParticipants() []string { |
|
|
|
keys := make([]string, len(s.publicKeys)) |
|
|
|
keys := make([]string, len(s.publicKeys)) |
|
|
|
for i := 0; i < len(s.publicKeys); i++ { |
|
|
|
for i := 0; i < len(s.publicKeys); i++ { |
|
|
@ -174,8 +192,8 @@ func (s *cIdentities) AddSignature(p Phase, PubKey *bls.PublicKey, sig *bls.Sign |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *cIdentities) Reset(ps []Phase) { |
|
|
|
func (s *cIdentities) Reset(ps []Phase) { |
|
|
|
for _, p := range ps { |
|
|
|
for i := range ps { |
|
|
|
switch m := map[string]*bls.Sign{}; p { |
|
|
|
switch m := map[string]*bls.Sign{}; ps[i] { |
|
|
|
case Prepare: |
|
|
|
case Prepare: |
|
|
|
s.prepare = m |
|
|
|
s.prepare = m |
|
|
|
case Commit: |
|
|
|
case Commit: |
|
|
@ -223,43 +241,45 @@ func (s *cIdentities) ReadAllSignatures(p Phase) []*bls.Sign { |
|
|
|
return sigs |
|
|
|
return sigs |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newMapBackedSignatureReader() cIdentities { |
|
|
|
func newMapBackedSignatureReader() *cIdentities { |
|
|
|
return cIdentities{ |
|
|
|
return &cIdentities{ |
|
|
|
[]*bls.PublicKey{}, map[string]*bls.Sign{}, |
|
|
|
[]*bls.PublicKey{}, map[string]*bls.Sign{}, |
|
|
|
map[string]*bls.Sign{}, map[string]*bls.Sign{}, |
|
|
|
map[string]*bls.Sign{}, map[string]*bls.Sign{}, |
|
|
|
|
|
|
|
map[[shard.PublicKeySizeInBytes]byte]int{}, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *composite) ShouldSlash(shard.BlsPublicKey) bool { |
|
|
|
type composite struct { |
|
|
|
s, _ := c.shardIDProvider() |
|
|
|
DependencyInjectionWriter |
|
|
|
switch s { |
|
|
|
SignatureReader |
|
|
|
case shard.BeaconChainShardID: |
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type composite struct { |
|
|
|
func (d *depInject) SetShardIDProvider(p func() (uint32, error)) { |
|
|
|
cIdentities |
|
|
|
d.shardIDProvider = p |
|
|
|
depInject |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (d *depInject) ShardIDProvider() func() (uint32, error) { |
|
|
|
|
|
|
|
return d.shardIDProvider |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewDecider ..
|
|
|
|
// NewDecider ..
|
|
|
|
func NewDecider(p Policy) Decider { |
|
|
|
func NewDecider(p Policy) Decider { |
|
|
|
signatureStore := newMapBackedSignatureReader() |
|
|
|
signatureStore := newMapBackedSignatureReader() |
|
|
|
dependencies := depInject{} |
|
|
|
deps := &depInject{} |
|
|
|
c := &composite{signatureStore, dependencies} |
|
|
|
c := &composite{deps, signatureStore} |
|
|
|
switch p { |
|
|
|
switch p { |
|
|
|
case SuperMajorityVote: |
|
|
|
case SuperMajorityVote: |
|
|
|
return &uniformVoteWeight{&c.cIdentities, &c.depInject} |
|
|
|
return &uniformVoteWeight{c.DependencyInjectionWriter, c} |
|
|
|
case SuperMajorityStake: |
|
|
|
case SuperMajorityStake: |
|
|
|
|
|
|
|
fmt.Println("HRS") |
|
|
|
return &stakedVoteWeight{ |
|
|
|
return &stakedVoteWeight{ |
|
|
|
&c.cIdentities, &c.depInject, |
|
|
|
c.SignatureReader, |
|
|
|
|
|
|
|
c.DependencyInjectionWriter, |
|
|
|
|
|
|
|
c.DependencyInjectionWriter.(DependencyInjectionReader), |
|
|
|
|
|
|
|
c.SignatureReader.(slash.ThresholdDecider), |
|
|
|
map[[shard.PublicKeySizeInBytes]byte]stakedVoter{}, |
|
|
|
map[[shard.PublicKeySizeInBytes]byte]stakedVoter{}, |
|
|
|
big.NewInt(0), |
|
|
|
big.NewInt(0), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
// Should not be possible
|
|
|
|
// Should not be possible
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|