Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restoring at the first block #691

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type sandbox struct {
accounts map[crypto.Address]*sandboxAccount
validators map[crypto.Address]*sandboxValidator
params param.Params
height uint32
totalAccounts int32
totalValidators int32
totalPower int64
Expand All @@ -43,10 +44,11 @@ type sandboxAccount struct {
updated bool
}

func NewSandbox(store store.Reader, params param.Params,
func NewSandbox(height uint32, store store.Reader, params param.Params,
committee committee.Reader, totalPower int64,
) Sandbox {
sb := &sandbox{
height: height,
store: store,
committee: committee,
totalPower: totalPower,
Expand Down Expand Up @@ -218,13 +220,7 @@ func (sb *sandbox) CurrentHeight() uint32 {
sb.lk.RLock()
defer sb.lk.RUnlock()

return sb.currentHeight()
}

func (sb *sandbox) currentHeight() uint32 {
h, _ := sb.store.LastCertificate()

return h + 1
return sb.height + 1
}

func (sb *sandbox) IterateAccounts(
Expand Down
5 changes: 2 additions & 3 deletions sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ func setup(t *testing.T) *testData {
totalPower += val.Power()
}

sandbox := NewSandbox(store, params, committee, totalPower).(*sandbox)

assert.Equal(t, sandbox.CurrentHeight(), uint32(1))
lastHeight := uint32(21)
for i := uint32(1); i < lastHeight; i++ {
b := ts.GenerateTestBlock(nil)
c := ts.GenerateTestCertificate()
store.SaveBlock(i, b, c)
}
sandbox := NewSandbox(store.LastHeight,
store, params, committee, totalPower).(*sandbox)
assert.Equal(t, sandbox.CurrentHeight(), lastHeight)
assert.Equal(t, sandbox.Params(), params)

Expand Down
11 changes: 6 additions & 5 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func LoadOrNewState(
st.logger = logger.NewSubLogger("_state", st)
st.store = store

// The first account is Treasury Account at the genesis time.
// So if we have more account, we are not in the genesis height anymore.
if store.TotalAccounts() > 1 {
// Check if the number of accounts is greater than the genesis time;
// this indicates we are not at the genesis height anymore.
if store.TotalAccounts() > int32(len(genDoc.Accounts())) {
err := st.tryLoadLastInfo()
if err != nil {
return nil, err
}
} else {
// We are at the genesis height
// We are at the genesis height.
err := st.makeGenesisState(genDoc)
if err != nil {
return nil, err
Expand All @@ -94,7 +94,8 @@ func LoadOrNewState(
}

func (st *state) concreteSandbox() sandbox.Sandbox {
return sandbox.NewSandbox(st.store, st.params, st.committee, st.totalPower)
return sandbox.NewSandbox(st.lastInfo.BlockHeight(),
st.store, st.params, st.committee, st.totalPower)
}

func (st *state) tryLoadLastInfo() error {
Expand Down
Loading