summaryrefslogtreecommitdiff
path: root/docs/proposals/PIP-001.md
blob: c916a0768a33ebac425020f99dc644a56e559cbd (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Fetch and Restore VM State Interfaces

| Field | Value |
| --- | --- |
| ENIP | 1 |
| Title | Fetch and Restore VM State Interfaces |
| Author | Antonio Navarro |
| Status | Draft |
| Created | 2024-01-15 |
| Updated | 2024-01-15 |

## Abstract

This proposal introduces interfaces for saving and restoring the state of a parscan VM, a crucial feature for a variety of use cases.
We aim to provide a detailed description of these interfaces, focusing on their integration into the VM.
This will enable functionalities like snapshotting the VM state and tracking new and removed operations from the stack, offering greater control and flexibility in VM management.

## Rationale

There is an essential need to efficiently store and manage the state of VMs.
Different scenarios might require either a complete copy of the current state or an understanding of the changes made, facilitating storage in various formats like Merkle Trees, CRDTs, or others.
This functionality is critical for ensuring data consistency, enabling state recovery, and supporting complex computing tasks that require VM state manipulation.

## Specification

### Snapshotting

The snapshot interface aims to simplify the process of serializing and deserializing VM states.
It includes:

```go

// SnapshotValue is a serializable, simpler representation of *reflect.Value
type SnapshotValue struct {
    Kind  Kind
    Value *any
}

// Snapshot contains all needed data to continue previously halted VM execution.
type Snapshot struct {
    FramePointer, InstructionPointer int64
    InstructionCounter               int64
    Values                           []*SnapshotValue
}

// Snapshotter will be implemented by the VM to make it possible to get and load Snapshots into it. This interface might not really be needed, only the methods on the VM.
type Snapshotter interface {
    // Take halts VM execution and generates a *Snapshot from it.
    Take(context.Context) (*Snapshot, error)

    // Load halts VM execution and load the provided *Snapshot to it.
    Load(context.Context, *Snapshot) error
}

```

This interface halts execution to ensure consistency during state capture and restoration.
Note: VM version compatibility is critical as any changes to generated OP codes might impact state execution.

### Possible solution: OP Step Parser

The OP Step Parser will allow tracking changes on the VM stack, communicating these changes to an external entity.
This entity, in conjunction with the Snapshotter, will determine the data segments that require updating.

```go

// Implementation details to be added.

```

### Possible solution: track changes made on memory internally on the VM

To maintain optimum VM performance, an alternative is to track memory changes internally within the VM, incorporating these into the Snapshot output.

```go

// Implementation details to be added.

```


## Alternatives

While no other alternatives were identified, a thorough review was conducted to ensure the proposed solutions are the most effective and efficient for the intended purpose.

## Impact

The implementation of these interfaces is expected to marginally affect VM performance.
However, precise benchmarking is needed to quantify the impact.
The anticipated benefits in terms of improved state management and recovery capabilities are significant and are expected to outweigh the performance costs.

## References

[Actual GnoVM implementation](https://github.com/gnolang/gno).