-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a1b0fa
commit 478de31
Showing
7 changed files
with
417 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package v4 | ||
|
||
import "fmt" | ||
|
||
type PAOMarketStatus interface { | ||
GetMarketStatus() (uint32, bool) | ||
} | ||
|
||
// GetConsensusMarketStatus gets the most common status, provided that it is at least F+1. | ||
func GetConsensusMarketStatus(paos []PAOMarketStatus, f int) (uint32, error) { | ||
marketStatusCounts := make(map[uint32]int) | ||
for _, pao := range paos { | ||
marketStatus, valid := pao.GetMarketStatus() | ||
if valid { | ||
marketStatusCounts[marketStatus] += 1 | ||
} | ||
} | ||
|
||
var mostCommonMarketStatus uint32 | ||
var mostCommonCount int | ||
for marketStatus, count := range marketStatusCounts { | ||
if count > mostCommonCount { | ||
mostCommonMarketStatus = marketStatus | ||
mostCommonCount = count | ||
} else if count == mostCommonCount { | ||
// For stability, always prefer the smaller enum value in case of ties. | ||
// In practice this will prefer CLOSED over OPEN. | ||
if marketStatus < mostCommonMarketStatus { | ||
mostCommonMarketStatus = marketStatus | ||
} | ||
} | ||
} | ||
|
||
if mostCommonCount < f+1 { | ||
return 0, fmt.Errorf("market status has fewer than f+1 observations (status %d got %d/%d)", mostCommonMarketStatus, mostCommonCount, len(paos)) | ||
} | ||
|
||
return mostCommonMarketStatus, nil | ||
} |
Oops, something went wrong.