-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Raft consensus for lotus nodes in a cluster
- Loading branch information
Shrenuj Bansal
committed
Sep 12, 2022
1 parent
2532300
commit 1dc9115
Showing
26 changed files
with
2,317 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package messagesigner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/google/uuid" | ||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
libp2pconsensus "github.com/libp2p/go-libp2p-consensus" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/filecoin-project/lotus/api" | ||
"github.com/filecoin-project/lotus/chain/types" | ||
consensus "github.com/filecoin-project/lotus/lib/consensus/raft" | ||
"github.com/filecoin-project/lotus/node/modules/dtypes" | ||
) | ||
|
||
type MessageSignerConsensus struct { | ||
//msgSigner MessageSigner | ||
MsgSigner | ||
consensus *consensus.Consensus | ||
} | ||
|
||
//var _ full.MsgSigner = &MessageSignerConsensus{} | ||
|
||
func NewMessageSignerConsensus( | ||
wallet api.Wallet, | ||
mpool MpoolNonceAPI, | ||
ds dtypes.MetadataDS, | ||
consensus *consensus.Consensus) *MessageSignerConsensus { | ||
|
||
ds = namespace.Wrap(ds, datastore.NewKey("/message-signer-consensus/")) | ||
return &MessageSignerConsensus{ | ||
MsgSigner: &MessageSigner{ | ||
wallet: wallet, | ||
mpool: mpool, | ||
ds: ds, | ||
}, | ||
consensus: consensus, | ||
} | ||
} | ||
|
||
func (ms *MessageSignerConsensus) IsLeader(ctx context.Context) bool { | ||
return ms.consensus.IsLeader(ctx) | ||
} | ||
|
||
func (ms *MessageSignerConsensus) RedirectToLeader(ctx context.Context, method string, arg interface{}, ret interface{}) (bool, error) { | ||
ok, err := ms.consensus.RedirectToLeader(method, arg, ret.(*types.SignedMessage)) | ||
if err != nil { | ||
return ok, err | ||
} | ||
return ok, nil | ||
} | ||
|
||
func (ms *MessageSignerConsensus) SignMessage( | ||
ctx context.Context, | ||
msg *types.Message, | ||
spec *api.MessageSendSpec, | ||
cb func(*types.SignedMessage) error) (*types.SignedMessage, error) { | ||
|
||
signedMsg, err := ms.MsgSigner.SignMessage(ctx, msg, spec, cb) | ||
if err != nil { | ||
return nil, err | ||
} | ||
//u := uuid.New() | ||
//if spec != nil { | ||
// u = spec.MsgUuid | ||
//} | ||
|
||
//leader, err := ms.consensus.Leader(ctx) | ||
////curr := ms.consensus.IsLeader(ctx) | ||
//log.Infof("Consensus leader: ", leader, "current node is leader: ", ms.consensus.IsLeader(ctx)) | ||
|
||
op := &consensus.ConsensusOp{signedMsg.Message.Nonce, spec.MsgUuid, signedMsg.Message.From, signedMsg} | ||
err = ms.consensus.Commit(ctx, op) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return signedMsg, nil | ||
} | ||
|
||
func (ms *MessageSignerConsensus) GetSignedMessage(ctx context.Context, uuid uuid.UUID) (*types.SignedMessage, error) { | ||
state, err := ms.consensus.State(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Infof("!!!!!!!!!!!!!!!!!!!!!!!STate type: %v", reflect.TypeOf(state)) | ||
|
||
cstate := state.(consensus.RaftState) | ||
msg, ok := cstate.MsgUuids[uuid] | ||
if !ok { | ||
return nil, xerrors.Errorf("Msg with Uuid %s not available", uuid) | ||
} | ||
return msg, nil | ||
} | ||
|
||
func (ms *MessageSignerConsensus) GetRaftState(ctx context.Context) (libp2pconsensus.State, error) { | ||
fmt.Println("Gets to message signer consensus raft state") | ||
return ms.consensus.State(ctx) | ||
} | ||
|
||
//func (ms *MessageSignerConsensus) StoreSignedMessage(ctx context.Context, uuid uuid.UUID, message *types.SignedMessage) error { | ||
// | ||
// ms.consensus | ||
// return ms.MsgSigner.StoreSignedMessage(ctx, uuid, message) | ||
//} | ||
// | ||
//func (ms *MessageSignerConsensus) NextNonce(ctx context.Context, addr address.Address) (uint64, error) { | ||
// return ms.msgSigner.NextNonce(ctx, addr) | ||
//} | ||
// | ||
//func (ms *MessageSignerConsensus) SaveNonce(ctx context.Context, addr address.Address, nonce uint64) error { | ||
// return ms.msgSigner.SaveNonce(ctx, addr, nonce) | ||
//} | ||
// | ||
//func (ms *MessageSignerConsensus) DstoreKey(addr address.Address) datastore.Key { | ||
// return ms.msgSigner.DstoreKey(addr) | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.