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