github.com/MetalBlockchain/metalgo@v1.11.9/snow/consensus/snowball/unary_snowball.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package snowball 5 6 import ( 7 "fmt" 8 "slices" 9 ) 10 11 var _ Unary = (*unarySnowball)(nil) 12 13 func newUnarySnowball(alphaPreference int, terminationConditions []terminationCondition) unarySnowball { 14 return unarySnowball{ 15 unarySnowflake: newUnarySnowflake(alphaPreference, terminationConditions), 16 } 17 } 18 19 // unarySnowball is the implementation of a unary snowball instance 20 type unarySnowball struct { 21 // wrap the unary snowflake logic 22 unarySnowflake 23 24 // preferenceStrength tracks the total number of polls with a preference 25 preferenceStrength int 26 } 27 28 func (sb *unarySnowball) RecordPoll(count int) { 29 if count >= sb.alphaPreference { 30 sb.preferenceStrength++ 31 } 32 sb.unarySnowflake.RecordPoll(count) 33 } 34 35 func (sb *unarySnowball) Extend(choice int) Binary { 36 bs := &binarySnowball{ 37 binarySnowflake: binarySnowflake{ 38 binarySlush: binarySlush{preference: choice}, 39 confidence: slices.Clone(sb.confidence), 40 alphaPreference: sb.alphaPreference, 41 terminationConditions: sb.terminationConditions, 42 finalized: sb.Finalized(), 43 }, 44 preference: choice, 45 } 46 bs.preferenceStrength[choice] = sb.preferenceStrength 47 return bs 48 } 49 50 func (sb *unarySnowball) Clone() Unary { 51 newSnowball := *sb 52 newSnowball.confidence = slices.Clone(sb.confidence) 53 return &newSnowball 54 } 55 56 func (sb *unarySnowball) String() string { 57 return fmt.Sprintf("SB(PreferenceStrength = %d, %s)", 58 sb.preferenceStrength, 59 &sb.unarySnowflake) 60 }