Skip to content

Commit

Permalink
Merge branch 'master' into GRC-243-migrate-static-aws-credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
adutchak authored Sep 13, 2024
2 parents 161c9f0 + d366a13 commit 98673ab
Show file tree
Hide file tree
Showing 18 changed files with 1,312 additions and 26 deletions.
4 changes: 2 additions & 2 deletions snow/snowtest/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/validators/validatorstest"
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/upgrade/upgradetest"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/logging"
Expand Down Expand Up @@ -84,7 +84,7 @@ func Context(tb testing.TB, chainID ids.ID) *snow.Context {
ChainID: chainID,
NodeID: ids.EmptyNodeID,
PublicKey: publicKey,
NetworkUpgrades: upgrade.Default,
NetworkUpgrades: upgradetest.GetConfig(upgradetest.Latest),

XChainID: XChainID,
CChainID: CChainID,
Expand Down
14 changes: 0 additions & 14 deletions tests/upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
package upgrade

import (
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/config"
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/ava-labs/avalanchego/upgrade/upgradetest"
)

func TestUpgrade(t *testing.T) {
Expand Down Expand Up @@ -55,16 +51,6 @@ var _ = ginkgo.Describe("[Upgrade]", func() {
require.NoError(err)
network.Genesis = genesis

// Configure network upgrade flag
latestUnscheduled := upgradetest.GetConfig(upgradetest.Latest - 1)
upgradeJSON, err := json.Marshal(latestUnscheduled)
require.NoError(err)
upgradeBase64 := base64.StdEncoding.EncodeToString(upgradeJSON)
if network.DefaultFlags == nil {
network.DefaultFlags = tmpnet.FlagsMap{}
}
network.DefaultFlags[config.UpgradeFileContentKey] = upgradeBase64

e2e.StartNetwork(tc, network, avalancheGoExecPath, "" /* pluginDir */, 0 /* shutdownDelay */, false /* reuseNetwork */)

tc.By(fmt.Sprintf("restarting all nodes with %q binary", avalancheGoExecPathToUpgradeTo))
Expand Down
4 changes: 3 additions & 1 deletion upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ var (
CortinaTime: InitiallyActiveTime,
CortinaXChainStopVertexID: ids.Empty,
DurangoTime: InitiallyActiveTime,
EtnaTime: InitiallyActiveTime,
// Etna is left unactivated by default on local networks. It can be configured to
// activate by overriding the activation time in the upgrade file.
EtnaTime: UnscheduledActivationTime,
}

ErrInvalidUpgradeTimes = errors.New("invalid upgrade configuration")
Expand Down
15 changes: 15 additions & 0 deletions utils/iterator/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package iterator

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

var _ Iterator[any] = (*filtered[any])(nil)

type filtered[T any] struct {
Expand All @@ -19,6 +21,19 @@ func Filter[T any](it Iterator[T], filter func(T) bool) Iterator[T] {
}
}

// Deduplicate returns an iterator that skips the elements that have already
// been returned from [it].
func Deduplicate[T comparable](it Iterator[T]) Iterator[T] {
var seen set.Set[T]
return Filter(it, func(e T) bool {
if seen.Contains(e) {
return true
}
seen.Add(e)
return false
})
}

func (i *filtered[_]) Next() bool {
for i.it.Next() {
element := i.it.Value()
Expand Down
15 changes: 12 additions & 3 deletions utils/iterator/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/iterator"
"github.com/ava-labs/avalanchego/vms/platformvm/state"

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

func TestFilter(t *testing.T) {
Expand Down Expand Up @@ -40,8 +41,8 @@ func TestFilter(t *testing.T) {
stakers[3].TxID: stakers[3],
}

it := iterator.Filter(
iterator.FromSlice(stakers[:3]...),
it := Filter(
FromSlice(stakers[:3]...),
func(staker *state.Staker) bool {
_, ok := maskedStakers[staker.TxID]
return ok
Expand All @@ -55,3 +56,11 @@ func TestFilter(t *testing.T) {
it.Release()
require.False(it.Next())
}

func TestDeduplicate(t *testing.T) {
require.Equal(
t,
[]int{0, 1, 2, 3},
ToSlice(Deduplicate(FromSlice(0, 1, 2, 1, 2, 0, 3))),
)
}
4 changes: 1 addition & 3 deletions utils/math/continuous_averager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"time"
)

var convertEToBase2 = math.Log(2)

type continuousAverager struct {
halflife float64
weightedSum float64
Expand All @@ -34,7 +32,7 @@ func NewAverager(
currentTime time.Time,
) Averager {
return &continuousAverager{
halflife: float64(halflife) / convertEToBase2,
halflife: float64(halflife) / math.Ln2,
weightedSum: initialPrediction,
normalizer: 1,
lastUpdated: currentTime,
Expand Down
4 changes: 1 addition & 3 deletions utils/math/meter/continuous_meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

var (
convertEToBase2 = math.Log(2)

_ Factory = (*ContinuousFactory)(nil)
_ Meter = (*continuousMeter)(nil)
)
Expand All @@ -34,7 +32,7 @@ type continuousMeter struct {
// NewMeter returns a new Meter with the provided halflife
func NewMeter(halflife time.Duration) Meter {
return &continuousMeter{
halflife: float64(halflife) / convertEToBase2,
halflife: float64(halflife) / math.Ln2,
}
}

Expand Down
45 changes: 45 additions & 0 deletions vms/platformvm/state/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type diff struct {
// Subnet ID --> supply of native asset of the subnet
currentSupply map[ids.ID]uint64

expiryDiff *expiryDiff

currentStakerDiffs diffStakers
// map of subnetID -> nodeID -> total accrued delegatee rewards
modifiedDelegateeRewards map[ids.ID]map[ids.NodeID]uint64
Expand Down Expand Up @@ -79,6 +81,7 @@ func NewDiff(
timestamp: parentState.GetTimestamp(),
feeState: parentState.GetFeeState(),
accruedFees: parentState.GetAccruedFees(),
expiryDiff: newExpiryDiff(),
subnetOwners: make(map[ids.ID]fx.Owner),
subnetManagers: make(map[ids.ID]chainIDAndAddr),
}, nil
Expand Down Expand Up @@ -146,6 +149,41 @@ func (d *diff) SetCurrentSupply(subnetID ids.ID, currentSupply uint64) {
}
}

func (d *diff) GetExpiryIterator() (iterator.Iterator[ExpiryEntry], error) {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}

parentIterator, err := parentState.GetExpiryIterator()
if err != nil {
return nil, err
}

return d.expiryDiff.getExpiryIterator(parentIterator), nil
}

func (d *diff) HasExpiry(entry ExpiryEntry) (bool, error) {
if has, modified := d.expiryDiff.modified[entry]; modified {
return has, nil
}

parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return false, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}

return parentState.HasExpiry(entry)
}

func (d *diff) PutExpiry(entry ExpiryEntry) {
d.expiryDiff.PutExpiry(entry)
}

func (d *diff) DeleteExpiry(entry ExpiryEntry) {
d.expiryDiff.DeleteExpiry(entry)
}

func (d *diff) GetCurrentValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, error) {
// If the validator was modified in this diff, return the modified
// validator.
Expand Down Expand Up @@ -451,6 +489,13 @@ func (d *diff) Apply(baseState Chain) error {
for subnetID, supply := range d.currentSupply {
baseState.SetCurrentSupply(subnetID, supply)
}
for entry, isAdded := range d.expiryDiff.modified {
if isAdded {
baseState.PutExpiry(entry)
} else {
baseState.DeleteExpiry(entry)
}
}
for _, subnetValidatorDiffs := range d.currentStakerDiffs.validatorDiffs {
for _, validatorDiff := range subnetValidatorDiffs {
switch validatorDiff.validatorStatus {
Expand Down
Loading

0 comments on commit 98673ab

Please sign in to comment.