forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
state_summary.go
66 lines (53 loc) · 2.04 KB
/
state_summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package proposervm
import (
"context"
"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
"github.com/MetalBlockchain/metalgo/vms/proposervm/summary"
)
var _ block.StateSummary = (*stateSummary)(nil)
// stateSummary implements block.StateSummary by layering three objects:
//
// 1. [statelessSummary] carries all summary marshallable content along with
// data immediately retrievable from it.
// 2. [innerSummary] reports the height of the summary as well as notifying the
// inner vm of the summary's acceptance.
// 3. [block] is used to update the proposervm's last accepted block upon
// Accept.
//
// Note: summary.StatelessSummary contains the data to build both [innerSummary]
// and [block].
type stateSummary struct {
summary.StateSummary
// inner summary, retrieved via Parse
innerSummary block.StateSummary
// block associated with the summary
block PostForkBlock
vm *VM
}
func (s *stateSummary) Height() uint64 {
return s.innerSummary.Height()
}
func (s *stateSummary) Accept(ctx context.Context) (block.StateSyncMode, error) {
// If we have already synced up to or past this state summary, we do not
// want to sync to it.
if s.vm.lastAcceptedHeight >= s.Height() {
return block.StateSyncSkipped, nil
}
// set fork height first, before accepting proposerVM full block
// which updates height index (among other indices)
if err := s.vm.State.SetForkHeight(s.StateSummary.ForkHeight()); err != nil {
return block.StateSyncSkipped, err
}
// We store the full proposerVM block associated with the summary
// and update height index with it, so that state sync could resume
// after a shutdown.
if err := s.block.acceptOuterBlk(); err != nil {
return block.StateSyncSkipped, err
}
// innerSummary.Accept may fail with the proposerVM block and index already
// updated. The error would be treated as fatal and the chain would then be
// repaired upon the VM restart.
return s.innerSummary.Accept(ctx)
}