2020-03-10 12:20:34 -07:00
|
|
|
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
|
|
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
|
|
|
|
package snowball
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// unarySnowball is the implementation of a unary snowball instance
|
|
|
|
type unarySnowball struct {
|
2020-03-20 23:26:22 -07:00
|
|
|
// wrap the unary snowflake logic
|
|
|
|
unarySnowflake
|
2020-03-10 12:20:34 -07:00
|
|
|
|
|
|
|
// numSuccessfulPolls tracks the total number of successful network polls
|
|
|
|
numSuccessfulPolls int
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecordSuccessfulPoll implements the UnarySnowball interface
|
|
|
|
func (sb *unarySnowball) RecordSuccessfulPoll() {
|
|
|
|
sb.numSuccessfulPolls++
|
2020-03-20 23:26:22 -07:00
|
|
|
sb.unarySnowflake.RecordSuccessfulPoll()
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extend implements the UnarySnowball interface
|
|
|
|
func (sb *unarySnowball) Extend(beta int, choice int) BinarySnowball {
|
|
|
|
bs := &binarySnowball{
|
2020-03-20 18:26:52 -07:00
|
|
|
binarySnowflake: binarySnowflake{
|
2020-03-20 23:26:22 -07:00
|
|
|
binarySlush: binarySlush{preference: choice},
|
|
|
|
beta: beta,
|
|
|
|
finalized: sb.Finalized(),
|
2020-03-10 12:20:34 -07:00
|
|
|
},
|
2020-03-20 18:26:52 -07:00
|
|
|
preference: choice,
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone implements the UnarySnowball interface
|
|
|
|
func (sb *unarySnowball) Clone() UnarySnowball {
|
2020-03-20 23:26:22 -07:00
|
|
|
newSnowball := *sb
|
|
|
|
return &newSnowball
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *unarySnowball) String() string {
|
2020-03-20 23:26:22 -07:00
|
|
|
return fmt.Sprintf("SB(NumSuccessfulPolls = %d, %s)",
|
2020-03-10 12:20:34 -07:00
|
|
|
sb.numSuccessfulPolls,
|
2020-03-20 23:26:22 -07:00
|
|
|
&sb.unarySnowflake)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|