Skip to content

Commit

Permalink
Merge branch 'p-chain_units-fees-update' into p-chain_priority_fees
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Apr 4, 2024
2 parents 4d1556d + e4b750d commit b50be4b
Show file tree
Hide file tree
Showing 31 changed files with 711 additions and 321 deletions.
10 changes: 6 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ linters-settings:
rules:
packages:
deny:
- pkg: "io/ioutil"
desc: io/ioutil is deprecated. Use package io or os instead.
- pkg: "github.com/stretchr/testify/assert"
desc: github.com/stretchr/testify/require should be used instead.
- pkg: "container/list"
desc: github.com/ava-labs/avalanchego/utils/linked should be used instead.
- pkg: "github.com/golang/mock/gomock"
desc: go.uber.org/mock/gomock should be used instead.
- pkg: "github.com/stretchr/testify/assert"
desc: github.com/stretchr/testify/require should be used instead.
- pkg: "io/ioutil"
desc: io/ioutil is deprecated. Use package io or os instead.
errorlint:
# Check for plain type assertions and type switches.
asserts: false
Expand Down
8 changes: 4 additions & 4 deletions cache/lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"

"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
"github.com/ava-labs/avalanchego/utils/linked"
)

var _ Cacher[struct{}, struct{}] = (*LRU[struct{}, struct{}])(nil)
Expand All @@ -17,7 +17,7 @@ var _ Cacher[struct{}, struct{}] = (*LRU[struct{}, struct{}])(nil)
// done, based on evicting the least recently used value.
type LRU[K comparable, V any] struct {
lock sync.Mutex
elements linkedhashmap.LinkedHashmap[K, V]
elements *linked.Hashmap[K, V]
// If set to <= 0, will be set internally to 1.
Size int
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *LRU[K, _]) evict(key K) {
}

func (c *LRU[K, V]) flush() {
c.elements = linkedhashmap.New[K, V]()
c.elements = linked.NewHashmap[K, V]()
}

func (c *LRU[_, _]) len() int {
Expand All @@ -112,7 +112,7 @@ func (c *LRU[_, _]) portionFilled() float64 {
// in the cache == [c.size] if necessary.
func (c *LRU[K, V]) resize() {
if c.elements == nil {
c.elements = linkedhashmap.New[K, V]()
c.elements = linked.NewHashmap[K, V]()
}
if c.Size <= 0 {
c.Size = 1
Expand Down
8 changes: 4 additions & 4 deletions cache/lru_sized_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"

"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
"github.com/ava-labs/avalanchego/utils/linked"
)

var _ Cacher[struct{}, any] = (*sizedLRU[struct{}, any])(nil)
Expand All @@ -17,15 +17,15 @@ var _ Cacher[struct{}, any] = (*sizedLRU[struct{}, any])(nil)
// honored, based on evicting the least recently used value.
type sizedLRU[K comparable, V any] struct {
lock sync.Mutex
elements linkedhashmap.LinkedHashmap[K, V]
elements *linked.Hashmap[K, V]
maxSize int
currentSize int
size func(K, V) int
}

func NewSizedLRU[K comparable, V any](maxSize int, size func(K, V) int) Cacher[K, V] {
return &sizedLRU[K, V]{
elements: linkedhashmap.New[K, V](),
elements: linked.NewHashmap[K, V](),
maxSize: maxSize,
size: size,
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (c *sizedLRU[K, _]) evict(key K) {
}

func (c *sizedLRU[K, V]) flush() {
c.elements = linkedhashmap.New[K, V]()
c.elements = linked.NewHashmap[K, V]()
c.currentSize = 0
}

Expand Down
33 changes: 17 additions & 16 deletions cache/unique_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
package cache

import (
"container/list"
"sync"

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

var _ Deduplicator[struct{}, Evictable[struct{}]] = (*EvictableLRU[struct{}, Evictable[struct{}]])(nil)

// EvictableLRU is an LRU cache that notifies the objects when they are evicted.
type EvictableLRU[K comparable, _ Evictable[K]] struct {
type EvictableLRU[K comparable, V Evictable[K]] struct {
lock sync.Mutex
entryMap map[K]*list.Element
entryList *list.List
entryMap map[K]*linked.ListElement[V]
entryList *linked.List[V]
Size int
}

Expand All @@ -32,12 +33,12 @@ func (c *EvictableLRU[_, _]) Flush() {
c.flush()
}

func (c *EvictableLRU[K, _]) init() {
func (c *EvictableLRU[K, V]) init() {
if c.entryMap == nil {
c.entryMap = make(map[K]*list.Element)
c.entryMap = make(map[K]*linked.ListElement[V])
}
if c.entryList == nil {
c.entryList = list.New()
c.entryList = linked.NewList[V]()
}
if c.Size <= 0 {
c.Size = 1
Expand All @@ -49,9 +50,8 @@ func (c *EvictableLRU[_, V]) resize() {
e := c.entryList.Front()
c.entryList.Remove(e)

val := e.Value.(V)
delete(c.entryMap, val.Key())
val.Evict()
delete(c.entryMap, e.Value.Key())
e.Value.Evict()
}
}

Expand All @@ -65,20 +65,21 @@ func (c *EvictableLRU[_, V]) deduplicate(value V) V {
e = c.entryList.Front()
c.entryList.MoveToBack(e)

val := e.Value.(V)
delete(c.entryMap, val.Key())
val.Evict()
delete(c.entryMap, e.Value.Key())
e.Value.Evict()

e.Value = value
} else {
e = c.entryList.PushBack(value)
e = &linked.ListElement[V]{
Value: value,
}
c.entryList.PushBack(e)
}
c.entryMap[key] = e
} else {
c.entryList.MoveToBack(e)

val := e.Value.(V)
value = val
value = e.Value
}
return value
}
Expand Down
6 changes: 3 additions & 3 deletions network/throttling/inbound_msg_byte_throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
"github.com/ava-labs/avalanchego/utils/linked"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/metric"
"github.com/ava-labs/avalanchego/utils/wrappers"
Expand All @@ -39,7 +39,7 @@ func newInboundMsgByteThrottler(
nodeToVdrBytesUsed: make(map[ids.NodeID]uint64),
nodeToAtLargeBytesUsed: make(map[ids.NodeID]uint64),
},
waitingToAcquire: linkedhashmap.New[uint64, *msgMetadata](),
waitingToAcquire: linked.NewHashmap[uint64, *msgMetadata](),
nodeToWaitingMsgID: make(map[ids.NodeID]uint64),
}
return t, t.metrics.initialize(namespace, registerer)
Expand Down Expand Up @@ -67,7 +67,7 @@ type inboundMsgByteThrottler struct {
// Node ID --> Msg ID for a message this node is waiting to acquire
nodeToWaitingMsgID map[ids.NodeID]uint64
// Msg ID --> *msgMetadata
waitingToAcquire linkedhashmap.LinkedHashmap[uint64, *msgMetadata]
waitingToAcquire *linked.Hashmap[uint64, *msgMetadata]
// Invariant: The node is only waiting on a single message at a time
//
// Invariant: waitingToAcquire.Get(nodeToWaitingMsgIDs[nodeID])
Expand Down
3 changes: 3 additions & 0 deletions network/throttling/inbound_msg_byte_throttler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,16 @@ func TestMsgThrottlerNextMsg(t *testing.T) {

// Release 1 byte
throttler.release(&msgMetadata{msgSize: 1}, vdr1ID)

// Byte should have gone toward next validator message
throttler.lock.Lock()
require.Equal(2, throttler.waitingToAcquire.Len())
require.Contains(throttler.nodeToWaitingMsgID, vdr1ID)
firstMsgID := throttler.nodeToWaitingMsgID[vdr1ID]
firstMsg, exists := throttler.waitingToAcquire.Get(firstMsgID)
require.True(exists)
require.Equal(maxBytes-2, firstMsg.bytesNeeded)
throttler.lock.Unlock()

select {
case <-doneVdr:
Expand Down
6 changes: 3 additions & 3 deletions snow/consensus/snowman/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
"github.com/ava-labs/avalanchego/utils/linked"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/metric"
"github.com/ava-labs/avalanchego/utils/wrappers"
Expand All @@ -34,7 +34,7 @@ type metrics struct {
// processingBlocks keeps track of the [processingStart] that each block was
// issued into the consensus instance. This is used to calculate the amount
// of time to accept or reject the block.
processingBlocks linkedhashmap.LinkedHashmap[ids.ID, processingStart]
processingBlocks *linked.Hashmap[ids.ID, processingStart]

// numProcessing keeps track of the number of processing blocks
numProcessing prometheus.Gauge
Expand Down Expand Up @@ -90,7 +90,7 @@ func newMetrics(
Help: "timestamp of the last accepted block in unix seconds",
}),

processingBlocks: linkedhashmap.New[ids.ID, processingStart](),
processingBlocks: linked.NewHashmap[ids.ID, processingStart](),

// e.g.,
// "avalanche_X_blks_processing" reports how many blocks are currently processing
Expand Down
6 changes: 3 additions & 3 deletions snow/consensus/snowman/poll/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/bag"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
"github.com/ava-labs/avalanchego/utils/linked"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/metric"
)
Expand Down Expand Up @@ -48,7 +48,7 @@ type set struct {
durPolls metric.Averager
factory Factory
// maps requestID -> poll
polls linkedhashmap.LinkedHashmap[uint32, pollHolder]
polls *linked.Hashmap[uint32, pollHolder]
}

// NewSet returns a new empty set of polls
Expand Down Expand Up @@ -82,7 +82,7 @@ func NewSet(
numPolls: numPolls,
durPolls: durPolls,
factory: factory,
polls: linkedhashmap.New[uint32, pollHolder](),
polls: linked.NewHashmap[uint32, pollHolder](),
}, nil
}

Expand Down
12 changes: 7 additions & 5 deletions snow/networking/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,10 @@ func (h *handler) dispatchSync(ctx context.Context) {
// If there is an error handling the message, shut down the chain
if err := h.handleSyncMsg(ctx, msg); err != nil {
h.StopWithError(ctx, fmt.Errorf(
"%w while processing sync message: %s",
"%w while processing sync message: %s from %s",
err,
msg,
msg.Op(),
msg.NodeID(),
))
return
}
Expand Down Expand Up @@ -429,7 +430,7 @@ func (h *handler) dispatchChans(ctx context.Context) {
h.StopWithError(ctx, fmt.Errorf(
"%w while processing chan message: %s",
err,
msg,
msg.Op(),
))
return
}
Expand Down Expand Up @@ -766,9 +767,10 @@ func (h *handler) handleAsyncMsg(ctx context.Context, msg Message) {
h.asyncMessagePool.Go(func() error {
if err := h.executeAsyncMsg(ctx, msg); err != nil {
h.StopWithError(ctx, fmt.Errorf(
"%w while processing async message: %s",
"%w while processing async message: %s from %s",
err,
msg,
msg.Op(),
msg.NodeID(),
))
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions snow/networking/router/chain_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/ava-labs/avalanchego/snow/networking/handler"
"github.com/ava-labs/avalanchego/snow/networking/timeout"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
"github.com/ava-labs/avalanchego/utils/linked"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
Expand Down Expand Up @@ -83,7 +83,7 @@ type ChainRouter struct {
// Parameters for doing health checks
healthConfig HealthConfig
// aggregator of requests based on their time
timedRequests linkedhashmap.LinkedHashmap[ids.RequestID, requestEntry]
timedRequests *linked.Hashmap[ids.RequestID, requestEntry]
}

// Initialize the router.
Expand Down Expand Up @@ -112,7 +112,7 @@ func (cr *ChainRouter) Initialize(
cr.criticalChains = criticalChains
cr.sybilProtectionEnabled = sybilProtectionEnabled
cr.onFatal = onFatal
cr.timedRequests = linkedhashmap.New[ids.RequestID, requestEntry]()
cr.timedRequests = linked.NewHashmap[ids.RequestID, requestEntry]()
cr.peers = make(map[ids.NodeID]*peer)
cr.healthConfig = healthConfig

Expand Down
Loading

0 comments on commit b50be4b

Please sign in to comment.