Skip to content

Commit

Permalink
Replace type specific sets with a generic implementation (ava-labs#1861)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Dec 5, 2022
1 parent 42d6543 commit 87ce2da
Show file tree
Hide file tree
Showing 165 changed files with 1,320 additions and 1,959 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.19.3'
check-latest: true
- name: build_test
shell: bash
Expand Down
9 changes: 4 additions & 5 deletions api/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/password"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)

Expand Down Expand Up @@ -99,14 +100,13 @@ type auth struct {
// Can be changed via API call.
password password.Hash
// Set of token IDs that have been revoked
revoked map[string]struct{}
revoked set.Set[string]
}

func New(log logging.Logger, endpoint, pw string) (Auth, error) {
a := &auth{
log: log,
endpoint: endpoint,
revoked: make(map[string]struct{}),
}
return a, a.password.Set(pw)
}
Expand All @@ -116,7 +116,6 @@ func NewFromHash(log logging.Logger, endpoint string, pw password.Hash) Auth {
log: log,
endpoint: endpoint,
password: pw,
revoked: make(map[string]struct{}),
}
}

Expand Down Expand Up @@ -196,7 +195,7 @@ func (a *auth) RevokeToken(tokenStr, pw string) error {
if !ok {
return fmt.Errorf("expected auth token's claims to be type endpointClaims but is %T", token.Claims)
}
a.revoked[claims.Id] = struct{}{}
a.revoked.Add(claims.Id)
return nil
}

Expand Down Expand Up @@ -250,7 +249,7 @@ func (a *auth) ChangePassword(oldPW, newPW string) error {

// All the revoked tokens are now invalid; no need to mark specifically as
// revoked.
a.revoked = make(map[string]struct{})
a.revoked.Clear()
return nil
}

Expand Down
8 changes: 5 additions & 3 deletions chains/atomic/gsharedmemory/filtered_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
package gsharedmemory

import (
"github.com/ava-labs/avalanchego/utils/set"

sharedmemorypb "github.com/ava-labs/avalanchego/proto/pb/sharedmemory"
)

type filteredBatch struct {
writes map[string][]byte
deletes map[string]struct{}
deletes set.Set[string]
}

func (b *filteredBatch) Put(key []byte, value []byte) error {
keyStr := string(key)
delete(b.deletes, keyStr)
b.deletes.Remove(keyStr)
b.writes[keyStr] = value
return nil
}

func (b *filteredBatch) Delete(key []byte) error {
keyStr := string(key)
delete(b.writes, keyStr)
b.deletes[keyStr] = struct{}{}
b.deletes.Add(keyStr)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions chains/atomic/gsharedmemory/shared_memory_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ func (c *Client) makeBatches(rawBatches []database.Batch, currentSize int) ([][]
for _, batch := range rawBatches {
batch := batch.Inner()
fb := filteredBatch{
writes: make(map[string][]byte),
deletes: make(map[string]struct{}),
writes: make(map[string][]byte),
}
if err := batch.Replay(&fb); err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions chains/atomic/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/set"
)

var errDuplicatedOperation = errors.New("duplicated operation on provided value")
Expand Down Expand Up @@ -196,7 +197,7 @@ func (s *state) getKeys(traits [][]byte, startTrait, startKey []byte, limit int)
// this variable is declared, the map may not be initialized from the
// start. The first add to the underlying map of the set would then
// result in the map being initialized.
keySet := ids.Set{}
keySet := set.Set[ids.ID]{}
keys := [][]byte(nil)
lastTrait := startTrait
lastKey := startKey
Expand Down Expand Up @@ -235,7 +236,7 @@ func (s *state) getKeys(traits [][]byte, startTrait, startKey []byte, limit int)
// and adds keys that possess [trait] to [keys] until the iteration completes or
// limit hits 0. If a key possesses multiple traits, it will be de-duplicated
// with [keySet].
func (s *state) appendTraitKeys(keys *[][]byte, keySet *ids.Set, limit *int, trait, startKey []byte) ([]byte, error) {
func (s *state) appendTraitKeys(keys *[][]byte, keySet *set.Set[ids.ID], limit *int, trait, startKey []byte) ([]byte, error) {
lastKey := startKey

// Create the prefixDB for the specified trait.
Expand Down
3 changes: 2 additions & 1 deletion chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/metervm"
Expand Down Expand Up @@ -179,7 +180,7 @@ type ManagerConfig struct {
AVAXAssetID ids.ID
XChainID ids.ID // ID of the X-Chain,
CChainID ids.ID // ID of the C-Chain,
CriticalChains ids.Set // Chains that can't exit gracefully
CriticalChains set.Set[ids.ID] // Chains that can't exit gracefully
TimeoutManager timeout.Manager // Manages request timeouts when sending messages to other validators
Health health.Registerer
RetryBootstrap bool // Should Bootstrap be retried
Expand Down
5 changes: 3 additions & 2 deletions chains/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/networking/sender"
"github.com/ava-labs/avalanchego/utils/set"
)

var _ Subnet = (*subnet)(nil)
Expand Down Expand Up @@ -41,8 +42,8 @@ type SubnetConfig struct {

type subnet struct {
lock sync.RWMutex
bootstrapping ids.Set
bootstrapped ids.Set
bootstrapping set.Set[ids.ID]
bootstrapped set.Set[ids.ID]
once sync.Once
bootstrappedSema chan struct{}
}
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/ava-labs/avalanchego/utils/password"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/profiler"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/storage"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/vms"
Expand Down Expand Up @@ -870,8 +871,8 @@ func getGenesisData(v *viper.Viper, networkID uint32) ([]byte, ids.ID, error) {
return genesis.FromConfig(config)
}

func getWhitelistedSubnets(v *viper.Viper) (ids.Set, error) {
whitelistedSubnetIDs := ids.Set{}
func getWhitelistedSubnets(v *viper.Viper) (set.Set[ids.ID], error) {
whitelistedSubnetIDs := set.Set[ids.ID]{}
for _, subnet := range strings.Split(v.GetString(WhitelistedSubnetsKey), ",") {
if subnet == "" {
continue
Expand Down
7 changes: 4 additions & 3 deletions database/rpcdb/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/nodb"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/utils/wrappers"

Expand Down Expand Up @@ -188,14 +189,14 @@ func (b *batch) Write() error {
Continues: true,
}
currentSize := 0
keySet := make(map[string]struct{}, len(b.writes))
keySet := set.NewSet[string](len(b.writes))
for i := len(b.writes) - 1; i >= 0; i-- {
kv := b.writes[i]
key := string(kv.key)
if _, overwritten := keySet[key]; overwritten {
if keySet.Contains(key) {
continue
}
keySet[key] = struct{}{}
keySet.Add(key)

sizeChange := baseElementSize + len(kv.key) + len(kv.value)
if newSize := currentSize + sizeChange; newSize > maxBatchSize {
Expand Down
7 changes: 4 additions & 3 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
"github.com/ava-labs/avalanchego/vms/nftfx"
Expand Down Expand Up @@ -51,8 +52,8 @@ func validateInitialStakedFunds(config *Config) error {
return errNoInitiallyStakedFunds
}

allocationSet := ids.ShortSet{}
initialStakedFundsSet := ids.ShortSet{}
allocationSet := set.Set[ids.ShortID]{}
initialStakedFundsSet := set.Set[ids.ShortID]{}
for _, allocation := range config.Allocations {
// It is ok to have duplicates as different
// ethAddrs could claim to the same avaxAddr.
Expand Down Expand Up @@ -321,7 +322,7 @@ func FromConfig(config *Config) ([]byte, ids.ID, error) {
return nil, ids.ID{}, fmt.Errorf("couldn't calculate the initial supply: %w", err)
}

initiallyStaked := ids.ShortSet{}
initiallyStaked := set.Set[ids.ShortID]{}
initiallyStaked.Add(config.InitialStakedFunds...)
skippedAllocations := []Allocation(nil)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/NYTimes/gziphandler v1.1.1
github.com/ava-labs/avalanche-ledger-go v0.0.13
github.com/ava-labs/avalanche-network-runner-sdk v0.3.0
github.com/ava-labs/coreth v0.11.4-0.20221128181836-2d60c6abab59
github.com/ava-labs/coreth v0.11.4-0.20221129221227-18cf83fd48fc
github.com/btcsuite/btcd v0.23.1
github.com/btcsuite/btcd/btcutil v1.1.1
github.com/decred/dcrd/dcrec/secp256k1/v3 v3.0.0-20200627015759-01fd2de07837
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ github.com/ava-labs/avalanche-ledger-go v0.0.13 h1:YTdaSuaZS/1ct1RGirBEJeo2tiSfV
github.com/ava-labs/avalanche-ledger-go v0.0.13/go.mod h1:LolCV2cdtkD67V/BSfy/ELUqleG1sbVyNdo5qe1u4y4=
github.com/ava-labs/avalanche-network-runner-sdk v0.3.0 h1:TVi9JEdKNU/RevYZ9PyW4pULbEdS+KQDA9Ki2DUvuAs=
github.com/ava-labs/avalanche-network-runner-sdk v0.3.0/go.mod h1:SgKJvtqvgo/Bl/c8fxEHCLaSxEbzimYfBopcfrajxQk=
github.com/ava-labs/coreth v0.11.4-0.20221128181836-2d60c6abab59 h1:Nm9G7m/nmgUWp//zEm1E9Bjqcougy93RkN1/M+4jaFQ=
github.com/ava-labs/coreth v0.11.4-0.20221128181836-2d60c6abab59/go.mod h1:+K5iBfpEdOdi/InryzIBPgsS7aBTowqsWRapao4XLyM=
github.com/ava-labs/coreth v0.11.4-0.20221129221227-18cf83fd48fc h1:HhJZPY5kppuHJl1ipx75vQXKTnNPAiPaBt2B7W0eo8U=
github.com/ava-labs/coreth v0.11.4-0.20221129221227-18cf83fd48fc/go.mod h1:Tz/mPcqSk0MWqPfUtvk7TwZjktIw4+G5EGWfofEP7dE=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
6 changes: 4 additions & 2 deletions ids/bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/utils/set"
)

const (
Expand All @@ -26,7 +28,7 @@ type Bag struct {
modeFreq int

threshold int
metThreshold Set
metThreshold set.Set[ID]
}

func (b *Bag) init() {
Expand Down Expand Up @@ -117,7 +119,7 @@ func (b *Bag) Mode() (ID, int) {
}

// Threshold returns the ids that have been seen at least threshold times.
func (b *Bag) Threshold() Set {
func (b *Bag) Threshold() set.Set[ID] {
return b.metThreshold
}

Expand Down
4 changes: 3 additions & 1 deletion ids/bag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package ids

import (
"testing"

"github.com/ava-labs/avalanchego/utils/set"
)

func TestBagAdd(t *testing.T) {
Expand All @@ -26,7 +28,7 @@ func TestBagAdd(t *testing.T) {
} else if freq != 0 {
t.Fatalf("Bag.Mode[1] returned %d expected %d", freq, 0)
} else if threshold := bag.Threshold(); threshold.Len() != 0 {
t.Fatalf("Bag.Threshold returned %s expected %s", threshold, Set{})
t.Fatalf("Bag.Threshold returned %s expected %s", threshold, set.Set[ID]{})
}

bag.Add(id0)
Expand Down
Loading

0 comments on commit 87ce2da

Please sign in to comment.