github.com/iotexproject/iotex-core@v1.14.1-rc1/blockindex/contractstaking/delta_state.go (about) 1 // Copyright (c) 2023 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package contractstaking 7 8 import "github.com/pkg/errors" 9 10 const ( 11 // deltaState constants 12 // deltaStateUnchanged is the zero-value of the type deltaState 13 deltaStateUnchanged deltaState = iota 14 deltaStateAdded 15 deltaStateRemoved 16 deltaStateModified 17 ) 18 19 type deltaState int 20 21 var ( 22 deltaStateTransferMap = map[deltaState]map[deltaAction]deltaState{ 23 deltaStateUnchanged: { 24 deltaActionAdd: deltaStateAdded, 25 deltaActionRemove: deltaStateRemoved, 26 deltaActionModify: deltaStateModified, 27 }, 28 deltaStateAdded: { 29 deltaActionModify: deltaStateAdded, 30 deltaActionRemove: deltaStateUnchanged, 31 }, 32 deltaStateModified: { 33 deltaActionModify: deltaStateModified, 34 deltaActionRemove: deltaStateRemoved, 35 }, 36 } 37 ) 38 39 func (s deltaState) Transfer(act deltaAction) (deltaState, error) { 40 if _, ok := deltaStateTransferMap[s]; !ok { 41 return s, errors.Errorf("invalid delta state %d", s) 42 } 43 if _, ok := deltaStateTransferMap[s][act]; !ok { 44 return s, errors.Errorf("invalid delta action %d on state %d", act, s) 45 } 46 return deltaStateTransferMap[s][act], nil 47 }