/
migrations.go
79 lines (65 loc) · 2.26 KB
/
migrations.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
67
68
69
70
71
72
73
74
75
76
77
78
79
// Package migrations implements upgrade migration handlers.
package migrations
import (
"fmt"
"sync"
"github.com/oasisprotocol/oasis-core/go/common/logging"
upgradeApi "github.com/oasisprotocol/oasis-core/go/upgrade/api"
)
const (
// ModuleName is the migration module name.
ModuleName = "upgrade-migrations"
)
var (
registeredHandlers sync.Map
// ErrMissingMigrationHandler is error returned when a migration handler is not registered.
ErrMissingMigrationHandler = fmt.Errorf("missing migration handler")
)
// Handler is the interface used by migration handlers.
type Handler interface {
// StartupUpgrade is called by the upgrade manager to perform
// the node startup portion of the upgrade.
StartupUpgrade(*Context) error
// ConsensusUpgrade is called by the upgrade manager to perform
// the consensus portion of the upgrade. The interface argument is
// a private structure passed to Backend.ConsensusUpgrade by the
// consensus backend.
//
// This method will be called twice, once in BeginBlock and once in
// EndBlock.
ConsensusUpgrade(*Context, interface{}) error
}
// Context defines the common context used by migration handlers.
type Context struct {
// Upgrade is the currently pending upgrade structure.
Upgrade *upgradeApi.PendingUpgrade
// DataDir is the node's data directory.
DataDir string
Logger *logging.Logger
}
// Register registers a new migration handler, by upgrade name.
func Register(name upgradeApi.HandlerName, handler Handler) {
if err := name.ValidateBasic(); err != nil {
panic(fmt.Errorf("migration handler name error: %w", err))
}
if _, isRegistered := registeredHandlers.Load(name); isRegistered {
panic(fmt.Errorf("migration handler already registered: %s", name))
}
registeredHandlers.Store(name, handler)
}
// NewContext returns a new upgrade migration context.
func NewContext(upgrade *upgradeApi.PendingUpgrade, dataDir string) *Context {
return &Context{
Upgrade: upgrade,
DataDir: dataDir,
Logger: logging.GetLogger(ModuleName),
}
}
// GetHandler returns the handler associated with the upgrade described in the context.
func GetHandler(name upgradeApi.HandlerName) (Handler, error) {
h, exists := registeredHandlers.Load(name)
if !exists {
return nil, ErrMissingMigrationHandler
}
return h.(Handler), nil
}