2022-09-27 16:08:04 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-29 10:56:57 +00:00
|
|
|
|
2022-09-27 16:08:04 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2022-09-29 10:56:57 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-09-27 16:08:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/messagesigner"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RaftAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
MessageSigner *messagesigner.MessageSignerConsensus `optional:"true"`
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:56:57 +00:00
|
|
|
func (r *RaftAPI) GetRaftState(ctx context.Context) (*api.RaftStateData, error) {
|
2022-09-27 16:08:04 +00:00
|
|
|
if r.MessageSigner == nil {
|
|
|
|
return nil, xerrors.Errorf("Raft consensus not enabled. Please check your configuration")
|
|
|
|
}
|
2022-09-29 10:56:57 +00:00
|
|
|
raftState, err := r.MessageSigner.GetRaftState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &api.RaftStateData{NonceMap: raftState.NonceMap, MsgUuids: raftState.MsgUuids}, nil
|
2022-09-27 16:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RaftAPI) Leader(ctx context.Context) (peer.ID, error) {
|
|
|
|
if r.MessageSigner == nil {
|
|
|
|
return "", xerrors.Errorf("Raft consensus not enabled. Please check your configuration")
|
|
|
|
}
|
|
|
|
return r.MessageSigner.Leader(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RaftAPI) IsLeader(ctx context.Context) bool {
|
|
|
|
if r.MessageSigner == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return r.MessageSigner.IsLeader(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RaftAPI) RedirectToLeader(ctx context.Context, method string, arg interface{}, ret interface{}) (bool, error) {
|
|
|
|
if r.MessageSigner == nil {
|
|
|
|
return false, xerrors.Errorf("Raft consensus not enabled. Please check your configuration")
|
|
|
|
}
|
|
|
|
return r.MessageSigner.RedirectToLeader(ctx, method, arg, ret)
|
|
|
|
}
|