/
fsm.go
65 lines (52 loc) · 1.54 KB
/
fsm.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
package raft
import (
"fmt"
"io"
_ethCommon "github.com/ethereum/go-ethereum/common"
_raft "github.com/hashicorp/raft"
"github.com/sirupsen/logrus"
"github.com/Fantom-foundation/go-evm/src/state"
)
// FSM wraps a state object and implements the Raft FSM interface
type FSM struct {
state *state.State
logger *logrus.Entry
}
// NewFSM returns a new FSM
func NewFSM(state *state.State, logger *logrus.Entry) *FSM {
return &FSM{
state: state,
logger: logger,
}
}
/*******************************************************************************
IMPLEMENT RAFT FSM INTERFACE
*******************************************************************************/
// Apply is invoked once a log entry is committed.
// It applies the log data to the state as a transaction.
func (f *FSM) Apply(log *_raft.Log) interface{} {
f.logger.WithFields(logrus.Fields{
"index": log.Index,
"term": log.Term,
"type": log.Type,
"data": log.Data,
}).Debug("Apply")
if err := f.state.ApplyTransaction(log.Data, int(log.Index), _ethCommon.Hash{}); err != nil {
f.logger.WithError(err).Error("Error applying transaction")
return nil
}
hash, err := f.state.Commit()
if err != nil {
f.logger.WithError(err).Error("Error committing")
return nil
}
return hash.Bytes()
}
// Snapshot is not implemented yet
func (f *FSM) Snapshot() (_raft.FSMSnapshot, error) {
return nil, fmt.Errorf("snapshot function not implemented")
}
// Restore is not implemented yet
func (f *FSM) Restore(io.ReadCloser) error {
return fmt.Errorf("restore function not implemented")
}