Skip to content

Commit

Permalink
Update local validator start time (#3224)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
ceyonur and StephenButtolph authored Jul 25, 2024
1 parent 1b4c632 commit 57fb839
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
38 changes: 37 additions & 1 deletion genesis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
Expand All @@ -21,6 +22,8 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
)

const localNetworkUpdateStartTimePeriod = 9 * 30 * 24 * time.Hour // 9 months

var (
_ utils.Sortable[Allocation] = Allocation{}

Expand Down Expand Up @@ -170,6 +173,10 @@ var (
// LocalConfig is the config that should be used to generate a local
// genesis.
LocalConfig Config

// unmodifiedLocalConfig is the LocalConfig before advancing the StartTime
// to a recent value.
unmodifiedLocalConfig Config
)

func init() {
Expand All @@ -196,10 +203,21 @@ func init() {
panic(err)
}

LocalConfig, err = unparsedLocalConfig.Parse()
unmodifiedLocalConfig, err = unparsedLocalConfig.Parse()
if err != nil {
panic(err)
}

// Renew the staking start time of the local config if required
definedStartTime := time.Unix(int64(unmodifiedLocalConfig.StartTime), 0)
recentStartTime := getRecentStartTime(
definedStartTime,
time.Now(),
localNetworkUpdateStartTimePeriod,
)

LocalConfig = unmodifiedLocalConfig
LocalConfig.StartTime = uint64(recentStartTime.Unix())
}

func GetConfig(networkID uint32) *Config {
Expand Down Expand Up @@ -247,3 +265,21 @@ func parseGenesisJSONBytesToConfig(bytes []byte) (*Config, error) {
}
return &config, nil
}

// getRecentStartTime advances [definedStartTime] in chunks of [period]. It
// returns the latest startTime that isn't after [now].
func getRecentStartTime(
definedStartTime time.Time,
now time.Time,
period time.Duration,
) time.Time {
startTime := definedStartTime
for {
nextStartTime := startTime.Add(period)
if now.Before(nextStartTime) {
break
}
startTime = nextStartTime
}
return startTime
}
66 changes: 66 additions & 0 deletions genesis/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package genesis

import (
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -51,3 +52,68 @@ func TestAllocationCompare(t *testing.T) {
})
}
}

func TestGetRecentStartTime(t *testing.T) {
type test struct {
name string
defined time.Time
now time.Time
expected time.Time
}
tests := []test{
{
name: "before 1 period and 1 second",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(-localNetworkUpdateStartTimePeriod - time.Second),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
},
{
name: "before 1 second",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(-time.Second),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
},
{
name: "equal",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
},
{
name: "after 1 second",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(time.Second),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
},
{
name: "after 1 period",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
},
{
name: "after 1 period and 1 second",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod + time.Second),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
},
{
name: "after 2 periods",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
},
{
name: "after 2 periods and 1 second",
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2*localNetworkUpdateStartTimePeriod + time.Second),
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := getRecentStartTime(tt.defined, tt.now, localNetworkUpdateStartTimePeriod)
require.Equal(t, tt.expected, actual)
})
}
}
4 changes: 2 additions & 2 deletions genesis/genesis_local.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
]
}
],
"startTime": 1690862400,
"startTime": 1721016000,
"initialStakeDuration": 31536000,
"initialStakeDurationOffset": 5400,
"initialStakedFunds": [
Expand Down Expand Up @@ -93,4 +93,4 @@
],
"cChainGenesis": "{\"config\":{\"chainId\":43112,\"homesteadBlock\":0,\"daoForkBlock\":0,\"daoForkSupport\":true,\"eip150Block\":0,\"eip150Hash\":\"0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0\",\"eip155Block\":0,\"eip158Block\":0,\"byzantiumBlock\":0,\"constantinopleBlock\":0,\"petersburgBlock\":0,\"istanbulBlock\":0,\"muirGlacierBlock\":0,\"apricotPhase1BlockTimestamp\":0,\"apricotPhase2BlockTimestamp\":0},\"nonce\":\"0x0\",\"timestamp\":\"0x0\",\"extraData\":\"0x00\",\"gasLimit\":\"0x5f5e100\",\"difficulty\":\"0x0\",\"mixHash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"coinbase\":\"0x0000000000000000000000000000000000000000\",\"alloc\":{\"8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC\":{\"balance\":\"0x295BE96E64066972000000\"}},\"number\":\"0x0\",\"gasUsed\":\"0x0\",\"parentHash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\"}",
"message": "{{ fun_quote }}"
}
}
15 changes: 7 additions & 8 deletions genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,28 +343,27 @@ func TestGenesisFromFlag(t *testing.T) {

func TestGenesis(t *testing.T) {
tests := []struct {
networkID uint32
config *Config
expectedID string
}{
{
networkID: constants.MainnetID,
config: &MainnetConfig,
expectedID: "UUvXi6j7QhVvgpbKM89MP5HdrxKm9CaJeHc187TsDNf8nZdLk",
},
{
networkID: constants.FujiID,
config: &FujiConfig,
expectedID: "MSj6o9TpezwsQx4Tv7SHqpVvCbJ8of1ikjsqPZ1bKRjc9zBy3",
},
{
networkID: constants.LocalID,
expectedID: "S4BvHv1XyihF9gXkJKXWWwQuuDWZqesRXz6wnqavQ9FrjGfAa",
config: &unmodifiedLocalConfig,
expectedID: "23DnViuN2kgePiBN4JxZXh1VrfXca2rwUp6XrKgNGdj3TSQjiN",
},
}
for _, test := range tests {
t.Run(constants.NetworkIDToNetworkName[test.networkID], func(t *testing.T) {
t.Run(constants.NetworkIDToNetworkName[test.config.NetworkID], func(t *testing.T) {
require := require.New(t)

config := GetConfig(test.networkID)
genesisBytes, _, err := FromConfig(config)
genesisBytes, _, err := FromConfig(test.config)
require.NoError(err)

var genesisID ids.ID = hashing.ComputeHash256Array(genesisBytes)
Expand Down

0 comments on commit 57fb839

Please sign in to comment.