Skip to content

Commit 57fb839

Browse files
Update local validator start time (#3224)
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
1 parent 1b4c632 commit 57fb839

File tree

4 files changed

+112
-11
lines changed

4 files changed

+112
-11
lines changed

genesis/config.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"os"
1414
"path/filepath"
15+
"time"
1516

1617
"github.com/ava-labs/avalanchego/ids"
1718
"github.com/ava-labs/avalanchego/utils"
@@ -21,6 +22,8 @@ import (
2122
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
2223
)
2324

25+
const localNetworkUpdateStartTimePeriod = 9 * 30 * 24 * time.Hour // 9 months
26+
2427
var (
2528
_ utils.Sortable[Allocation] = Allocation{}
2629

@@ -170,6 +173,10 @@ var (
170173
// LocalConfig is the config that should be used to generate a local
171174
// genesis.
172175
LocalConfig Config
176+
177+
// unmodifiedLocalConfig is the LocalConfig before advancing the StartTime
178+
// to a recent value.
179+
unmodifiedLocalConfig Config
173180
)
174181

175182
func init() {
@@ -196,10 +203,21 @@ func init() {
196203
panic(err)
197204
}
198205

199-
LocalConfig, err = unparsedLocalConfig.Parse()
206+
unmodifiedLocalConfig, err = unparsedLocalConfig.Parse()
200207
if err != nil {
201208
panic(err)
202209
}
210+
211+
// Renew the staking start time of the local config if required
212+
definedStartTime := time.Unix(int64(unmodifiedLocalConfig.StartTime), 0)
213+
recentStartTime := getRecentStartTime(
214+
definedStartTime,
215+
time.Now(),
216+
localNetworkUpdateStartTimePeriod,
217+
)
218+
219+
LocalConfig = unmodifiedLocalConfig
220+
LocalConfig.StartTime = uint64(recentStartTime.Unix())
203221
}
204222

205223
func GetConfig(networkID uint32) *Config {
@@ -247,3 +265,21 @@ func parseGenesisJSONBytesToConfig(bytes []byte) (*Config, error) {
247265
}
248266
return &config, nil
249267
}
268+
269+
// getRecentStartTime advances [definedStartTime] in chunks of [period]. It
270+
// returns the latest startTime that isn't after [now].
271+
func getRecentStartTime(
272+
definedStartTime time.Time,
273+
now time.Time,
274+
period time.Duration,
275+
) time.Time {
276+
startTime := definedStartTime
277+
for {
278+
nextStartTime := startTime.Add(period)
279+
if now.Before(nextStartTime) {
280+
break
281+
}
282+
startTime = nextStartTime
283+
}
284+
return startTime
285+
}

genesis/config_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package genesis
55

66
import (
77
"testing"
8+
"time"
89

910
"github.com/stretchr/testify/require"
1011

@@ -51,3 +52,68 @@ func TestAllocationCompare(t *testing.T) {
5152
})
5253
}
5354
}
55+
56+
func TestGetRecentStartTime(t *testing.T) {
57+
type test struct {
58+
name string
59+
defined time.Time
60+
now time.Time
61+
expected time.Time
62+
}
63+
tests := []test{
64+
{
65+
name: "before 1 period and 1 second",
66+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
67+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(-localNetworkUpdateStartTimePeriod - time.Second),
68+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
69+
},
70+
{
71+
name: "before 1 second",
72+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
73+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(-time.Second),
74+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
75+
},
76+
{
77+
name: "equal",
78+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
79+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
80+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
81+
},
82+
{
83+
name: "after 1 second",
84+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
85+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(time.Second),
86+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
87+
},
88+
{
89+
name: "after 1 period",
90+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
91+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
92+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
93+
},
94+
{
95+
name: "after 1 period and 1 second",
96+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
97+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod + time.Second),
98+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
99+
},
100+
{
101+
name: "after 2 periods",
102+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
103+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
104+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
105+
},
106+
{
107+
name: "after 2 periods and 1 second",
108+
defined: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
109+
now: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2*localNetworkUpdateStartTimePeriod + time.Second),
110+
expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
111+
},
112+
}
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
actual := getRecentStartTime(tt.defined, tt.now, localNetworkUpdateStartTimePeriod)
116+
require.Equal(t, tt.expected, actual)
117+
})
118+
}
119+
}

genesis/genesis_local.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
]
3939
}
4040
],
41-
"startTime": 1690862400,
41+
"startTime": 1721016000,
4242
"initialStakeDuration": 31536000,
4343
"initialStakeDurationOffset": 5400,
4444
"initialStakedFunds": [
@@ -93,4 +93,4 @@
9393
],
9494
"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\"}",
9595
"message": "{{ fun_quote }}"
96-
}
96+
}

genesis/genesis_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,28 +343,27 @@ func TestGenesisFromFlag(t *testing.T) {
343343

344344
func TestGenesis(t *testing.T) {
345345
tests := []struct {
346-
networkID uint32
346+
config *Config
347347
expectedID string
348348
}{
349349
{
350-
networkID: constants.MainnetID,
350+
config: &MainnetConfig,
351351
expectedID: "UUvXi6j7QhVvgpbKM89MP5HdrxKm9CaJeHc187TsDNf8nZdLk",
352352
},
353353
{
354-
networkID: constants.FujiID,
354+
config: &FujiConfig,
355355
expectedID: "MSj6o9TpezwsQx4Tv7SHqpVvCbJ8of1ikjsqPZ1bKRjc9zBy3",
356356
},
357357
{
358-
networkID: constants.LocalID,
359-
expectedID: "S4BvHv1XyihF9gXkJKXWWwQuuDWZqesRXz6wnqavQ9FrjGfAa",
358+
config: &unmodifiedLocalConfig,
359+
expectedID: "23DnViuN2kgePiBN4JxZXh1VrfXca2rwUp6XrKgNGdj3TSQjiN",
360360
},
361361
}
362362
for _, test := range tests {
363-
t.Run(constants.NetworkIDToNetworkName[test.networkID], func(t *testing.T) {
363+
t.Run(constants.NetworkIDToNetworkName[test.config.NetworkID], func(t *testing.T) {
364364
require := require.New(t)
365365

366-
config := GetConfig(test.networkID)
367-
genesisBytes, _, err := FromConfig(config)
366+
genesisBytes, _, err := FromConfig(test.config)
368367
require.NoError(err)
369368

370369
var genesisID ids.ID = hashing.ComputeHash256Array(genesisBytes)

0 commit comments

Comments
 (0)