-
Notifications
You must be signed in to change notification settings - Fork 321
/
delta_state.go
47 lines (41 loc) · 1.4 KB
/
delta_state.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
// Copyright (c) 2023 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package contractstaking
import "github.com/pkg/errors"
const (
// deltaState constants
// deltaStateUnchanged is the zero-value of the type deltaState
deltaStateUnchanged deltaState = iota
deltaStateAdded
deltaStateRemoved
deltaStateModified
)
type deltaState int
var (
deltaStateTransferMap = map[deltaState]map[deltaAction]deltaState{
deltaStateUnchanged: {
deltaActionAdd: deltaStateAdded,
deltaActionRemove: deltaStateRemoved,
deltaActionModify: deltaStateModified,
},
deltaStateAdded: {
deltaActionModify: deltaStateAdded,
deltaActionRemove: deltaStateUnchanged,
},
deltaStateModified: {
deltaActionModify: deltaStateModified,
deltaActionRemove: deltaStateRemoved,
},
}
)
func (s deltaState) Transfer(act deltaAction) (deltaState, error) {
if _, ok := deltaStateTransferMap[s]; !ok {
return s, errors.Errorf("invalid delta state %d", s)
}
if _, ok := deltaStateTransferMap[s][act]; !ok {
return s, errors.Errorf("invalid delta action %d on state %d", act, s)
}
return deltaStateTransferMap[s][act], nil
}