2020-11-27 16:16:08 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2021-04-30 19:55:12 +00:00
|
|
|
"encoding/binary"
|
2020-11-27 16:16:08 +00:00
|
|
|
"fmt"
|
2021-06-08 21:30:23 +00:00
|
|
|
"math/big"
|
2021-06-03 21:15:32 +00:00
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/pubsub"
|
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/consensus/types"
|
|
|
|
|
|
|
|
"github.com/minio/blake2b-simd"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/types"
|
2021-06-08 21:30:23 +00:00
|
|
|
crypto2 "github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2021-04-30 19:55:12 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-11-27 16:16:08 +00:00
|
|
|
)
|
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
type SignFunc func(peer.ID, []byte) (*types.Signature, error)
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func ComputeVRF(privKey crypto.PrivKey, sigInput []byte) ([]byte, error) {
|
|
|
|
return privKey.Sign(sigInput)
|
2021-04-30 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyVRF(worker peer.ID, vrfBase, vrfproof []byte) error {
|
2021-06-11 11:40:32 +00:00
|
|
|
pk, err := worker.ExtractPublicKey()
|
2021-04-30 19:55:12 +00:00
|
|
|
if err != nil {
|
2021-06-11 11:40:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
ok, err := pk.Verify(vrfproof, vrfBase)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("vrf was invalid")
|
2021-04-30 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func IsRoundWinner(round uint64,
|
|
|
|
worker peer.ID, randomness []byte, minerStake, networkStake *big.Int, privKey crypto.PrivKey) (*types.ElectionProof, error) {
|
2021-04-30 19:55:12 +00:00
|
|
|
|
|
|
|
buf, err := worker.MarshalBinary()
|
|
|
|
if err != nil {
|
2021-06-11 11:40:32 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal address: %w", err)
|
2021-04-30 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
electionRand, err := DrawRandomness(randomness, crypto2.DomainSeparationTag_ElectionProofProduction, round, buf)
|
2021-04-30 19:55:12 +00:00
|
|
|
if err != nil {
|
2021-06-11 11:40:32 +00:00
|
|
|
return nil, fmt.Errorf("failed to draw randomness: %w", err)
|
2021-04-30 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
vrfout, err := ComputeVRF(privKey, electionRand)
|
2021-04-30 19:55:12 +00:00
|
|
|
if err != nil {
|
2021-06-11 11:40:32 +00:00
|
|
|
return nil, fmt.Errorf("failed to compute VRF: %w", err)
|
2021-04-30 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ep := &types.ElectionProof{VRFProof: vrfout}
|
|
|
|
j := ep.ComputeWinCount(minerStake, networkStake)
|
|
|
|
ep.WinCount = j
|
|
|
|
if j < 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep, nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func DrawRandomness(rbase []byte, pers crypto2.DomainSeparationTag, round uint64, entropy []byte) ([]byte, error) {
|
2021-04-30 19:55:12 +00:00
|
|
|
h := blake2b.New256()
|
|
|
|
if err := binary.Write(h, binary.BigEndian, int64(pers)); err != nil {
|
|
|
|
return nil, xerrors.Errorf("deriving randomness: %v", err)
|
|
|
|
}
|
|
|
|
VRFDigest := blake2b.Sum256(rbase)
|
|
|
|
_, err := h.Write(VRFDigest[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("hashing VRFDigest: %w", err)
|
|
|
|
}
|
|
|
|
if err := binary.Write(h, binary.BigEndian, round); err != nil {
|
|
|
|
return nil, xerrors.Errorf("deriving randomness: %v", err)
|
|
|
|
}
|
|
|
|
_, err = h.Write(entropy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("hashing entropy: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.Sum(nil), nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func NewMessage(cmsg types2.ConsensusMessage, typ types2.ConsensusMessageType, privKey crypto.PrivKey) (*pubsub.GenericMessage, error) {
|
2021-06-03 21:15:32 +00:00
|
|
|
var message pubsub.GenericMessage
|
2021-06-08 21:30:23 +00:00
|
|
|
switch typ {
|
|
|
|
case types2.ConsensusMessageTypePrePrepare:
|
|
|
|
{
|
|
|
|
message.Type = pubsub.PrePrepareMessageType
|
|
|
|
message.Payload = types2.PrePrepareMessage{
|
|
|
|
Block: cmsg.Block,
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case types2.ConsensusMessageTypePrepare:
|
|
|
|
{
|
|
|
|
message.Type = pubsub.PrepareMessageType
|
|
|
|
pm := types2.PrepareMessage{
|
|
|
|
Blockhash: cmsg.Blockhash,
|
|
|
|
}
|
|
|
|
signature, err := privKey.Sign(cmsg.Blockhash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create signature: %v", err)
|
|
|
|
}
|
|
|
|
pm.Signature = signature
|
|
|
|
message.Payload = pm
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case types2.ConsensusMessageTypeCommit:
|
|
|
|
{
|
|
|
|
message.Type = pubsub.CommitMessageType
|
|
|
|
pm := types2.CommitMessage{
|
|
|
|
Blockhash: cmsg.Blockhash,
|
|
|
|
}
|
|
|
|
signature, err := privKey.Sign(cmsg.Blockhash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create signature: %v", err)
|
|
|
|
}
|
|
|
|
pm.Signature = signature
|
|
|
|
message.Payload = pm
|
|
|
|
break
|
|
|
|
}
|
2021-06-03 21:15:32 +00:00
|
|
|
}
|
2021-06-08 21:30:23 +00:00
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
return &message, nil
|
|
|
|
}
|