Skip to content

Commit b1393ec

Browse files
authored
Create wrapper for syncable VM (#5480)
1 parent 4772ab3 commit b1393ec

3 files changed

Lines changed: 95 additions & 1 deletion

File tree

vms/saevm/adaptor/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
22

33
go_library(
44
name = "adaptor",
5-
srcs = ["adaptor.go"],
5+
srcs = [
6+
"sync.go",
7+
"vm.go",
8+
],
69
importpath = "github.com/ava-labs/avalanchego/vms/saevm/adaptor",
710
visibility = ["//visibility:public"],
811
deps = [

vms/saevm/adaptor/sync.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (C) 2019, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package adaptor
5+
6+
import (
7+
"context"
8+
9+
"github.com/ava-labs/avalanchego/ids"
10+
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
11+
)
12+
13+
// SyncableVM adapts a [block.StateSyncableVM] and [block.StateSummary] for
14+
// // stateless use of the summary. See the respective interfaces for more details.
15+
type SyncableVM[SP SummaryProperties] interface {
16+
StateSyncEnabled(context.Context) (bool, error)
17+
GetLastStateSummary(context.Context) (SP, error)
18+
GetOngoingSyncStateSummary(context.Context) (SP, error)
19+
GetStateSummary(context.Context, uint64) (SP, error)
20+
ParseStateSummary(context.Context, []byte) (SP, error)
21+
AcceptSummary(context.Context, SP) (block.StateSyncMode, error)
22+
}
23+
24+
// SummaryProperties is a read-only subset of [block.StateSummary].
25+
// [block.StateSummary.Accept] is not included, as it is handled by [SyncableVM].
26+
type SummaryProperties interface {
27+
ID() ids.ID
28+
Bytes() []byte
29+
Height() uint64
30+
}
31+
32+
// ConvertStateSync transforms a [SyncableVM] into a [block.StateSyncableVM].
33+
func ConvertStateSync[SP SummaryProperties](vm SyncableVM[SP]) block.StateSyncableVM {
34+
return syncAdaptor[SP]{
35+
SyncableVM: vm,
36+
}
37+
}
38+
39+
type syncAdaptor[SP SummaryProperties] struct {
40+
SyncableVM[SP]
41+
}
42+
43+
// Summary is an implementation of [block.StateSummary], used by chains returned
44+
// by [ConvertStateSync]. The [SummaryProperties] can be accessed with
45+
// [Summary.Unwrap].
46+
type Summary[SP SummaryProperties] struct {
47+
s SP
48+
vm SyncableVM[SP]
49+
}
50+
51+
// Unwrap returns the underlying [SummaryProperties] of the [Summary].
52+
func (s Summary[SP]) Unwrap() SP {
53+
return s.s
54+
}
55+
56+
func (vm syncAdaptor[SP]) newSummary(s SP, err error) (block.StateSummary, error) {
57+
if err != nil {
58+
return nil, err
59+
}
60+
return Summary[SP]{s, vm.SyncableVM}, nil
61+
}
62+
63+
func (vm syncAdaptor[SP]) GetLastStateSummary(ctx context.Context) (block.StateSummary, error) {
64+
return vm.newSummary(vm.SyncableVM.GetLastStateSummary(ctx))
65+
}
66+
67+
func (vm syncAdaptor[SP]) GetOngoingSyncStateSummary(ctx context.Context) (block.StateSummary, error) {
68+
return vm.newSummary(vm.SyncableVM.GetOngoingSyncStateSummary(ctx))
69+
}
70+
71+
func (vm syncAdaptor[SP]) GetStateSummary(ctx context.Context, summaryHeight uint64) (block.StateSummary, error) {
72+
return vm.newSummary(vm.SyncableVM.GetStateSummary(ctx, summaryHeight))
73+
}
74+
75+
func (vm syncAdaptor[SP]) ParseStateSummary(ctx context.Context, summaryBytes []byte) (block.StateSummary, error) {
76+
return vm.newSummary(vm.SyncableVM.ParseStateSummary(ctx, summaryBytes))
77+
}
78+
79+
// ID propagates the respective method from the [SummaryProperties] carried by s.
80+
func (s Summary[SP]) ID() ids.ID { return s.s.ID() }
81+
82+
// Bytes propagates the respective method from the [SummaryProperties] carried by s.
83+
func (s Summary[SP]) Bytes() []byte { return s.s.Bytes() }
84+
85+
// Height propagates the respective method from the [SummaryProperties] carried by s.
86+
func (s Summary[SP]) Height() uint64 { return s.s.Height() }
87+
88+
// Accept calls AcceptSummary(s) on the [SyncableVM] that created s.
89+
func (s Summary[SP]) Accept(ctx context.Context) (block.StateSyncMode, error) {
90+
return s.vm.AcceptSummary(ctx, s.s)
91+
}

0 commit comments

Comments
 (0)