github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/storage/primary/snapshot_tree.go (about)

     1  package primary
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onflow/flow-go/fvm/storage/logical"
     7  	"github.com/onflow/flow-go/fvm/storage/snapshot"
     8  )
     9  
    10  type timestampedSnapshotTree struct {
    11  	currentSnapshotTime logical.Time
    12  	baseSnapshotTime    logical.Time
    13  
    14  	snapshot.SnapshotTree
    15  
    16  	fullLog snapshot.UpdateLog
    17  }
    18  
    19  func newTimestampedSnapshotTree(
    20  	storageSnapshot snapshot.StorageSnapshot,
    21  	snapshotTime logical.Time,
    22  ) timestampedSnapshotTree {
    23  	return timestampedSnapshotTree{
    24  		currentSnapshotTime: snapshotTime,
    25  		baseSnapshotTime:    snapshotTime,
    26  		SnapshotTree:        snapshot.NewSnapshotTree(storageSnapshot),
    27  		fullLog:             nil,
    28  	}
    29  }
    30  
    31  func (tree timestampedSnapshotTree) Append(
    32  	executionSnapshot *snapshot.ExecutionSnapshot,
    33  ) timestampedSnapshotTree {
    34  	return timestampedSnapshotTree{
    35  		currentSnapshotTime: tree.currentSnapshotTime + 1,
    36  		baseSnapshotTime:    tree.baseSnapshotTime,
    37  		SnapshotTree:        tree.SnapshotTree.Append(executionSnapshot),
    38  		fullLog:             append(tree.fullLog, executionSnapshot.WriteSet),
    39  	}
    40  }
    41  
    42  func (tree timestampedSnapshotTree) SnapshotTime() logical.Time {
    43  	return tree.currentSnapshotTime
    44  }
    45  
    46  func (tree timestampedSnapshotTree) UpdatesSince(
    47  	snapshotTime logical.Time,
    48  ) (
    49  	snapshot.UpdateLog,
    50  	error,
    51  ) {
    52  	if snapshotTime < tree.baseSnapshotTime {
    53  		// This should never happen.
    54  		return nil, fmt.Errorf(
    55  			"missing update log range [%v, %v)",
    56  			snapshotTime,
    57  			tree.baseSnapshotTime)
    58  	}
    59  
    60  	if snapshotTime > tree.currentSnapshotTime {
    61  		// This should never happen.
    62  		return nil, fmt.Errorf(
    63  			"missing update log range (%v, %v]",
    64  			tree.currentSnapshotTime,
    65  			snapshotTime)
    66  	}
    67  
    68  	return tree.fullLog[int(snapshotTime-tree.baseSnapshotTime):], nil
    69  }
    70  
    71  type rebaseableTimestampedSnapshotTree struct {
    72  	timestampedSnapshotTree
    73  }
    74  
    75  func newRebaseableTimestampedSnapshotTree(
    76  	snapshotTree timestampedSnapshotTree,
    77  ) *rebaseableTimestampedSnapshotTree {
    78  	return &rebaseableTimestampedSnapshotTree{
    79  		timestampedSnapshotTree: snapshotTree,
    80  	}
    81  }
    82  
    83  func (tree *rebaseableTimestampedSnapshotTree) Rebase(
    84  	base timestampedSnapshotTree,
    85  ) {
    86  	tree.timestampedSnapshotTree = base
    87  }