2020-11-15 13:46:58 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2020-12-04 18:30:03 +00:00
|
|
|
"bytes"
|
2020-11-27 16:16:08 +00:00
|
|
|
"fmt"
|
2020-12-07 16:01:10 +00:00
|
|
|
"sync"
|
2020-11-27 16:16:08 +00:00
|
|
|
|
2020-12-04 18:30:03 +00:00
|
|
|
"github.com/Secured-Finance/dione/cache"
|
2020-12-02 13:42:02 +00:00
|
|
|
|
2020-12-04 18:30:03 +00:00
|
|
|
"github.com/Secured-Finance/dione/consensus/validation"
|
2020-12-12 12:44:01 +00:00
|
|
|
_ "github.com/Secured-Finance/dione/consensus/validation/filecoin" // enable filecoin validation
|
2020-11-27 16:16:08 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/consensus/types"
|
2020-11-15 13:46:58 +00:00
|
|
|
"github.com/Secured-Finance/dione/sigs"
|
|
|
|
"github.com/Secured-Finance/dione/types"
|
2020-12-02 14:09:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-11-27 16:16:08 +00:00
|
|
|
"github.com/mitchellh/hashstructure/v2"
|
2020-11-18 18:14:56 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-11-15 13:46:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PrePreparePool struct {
|
2020-12-07 16:01:10 +00:00
|
|
|
mut sync.RWMutex
|
2020-11-18 19:53:52 +00:00
|
|
|
prePrepareMsgs map[string][]*types2.Message
|
2020-11-27 16:16:08 +00:00
|
|
|
miner *Miner
|
2021-03-05 19:29:09 +00:00
|
|
|
eventLogCache cache.EventCache
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 19:29:09 +00:00
|
|
|
func NewPrePreparePool(miner *Miner, evc cache.EventCache) *PrePreparePool {
|
2020-11-15 13:46:58 +00:00
|
|
|
return &PrePreparePool{
|
2020-11-18 19:53:52 +00:00
|
|
|
prePrepareMsgs: map[string][]*types2.Message{},
|
2020-11-27 16:16:08 +00:00
|
|
|
miner: miner,
|
2020-12-02 13:42:02 +00:00
|
|
|
eventLogCache: evc,
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:39:52 +00:00
|
|
|
func (pp *PrePreparePool) CreatePrePrepare(task *types.DioneTask, requestID string, callbackAddress, callbackMethodID, privateKey []byte) (*types2.Message, error) {
|
2020-11-18 19:53:52 +00:00
|
|
|
var message types2.Message
|
|
|
|
message.Type = types2.MessageTypePrePrepare
|
|
|
|
var consensusMsg types2.ConsensusMessage
|
2021-03-15 20:39:52 +00:00
|
|
|
consensusMsg.Task.ConsensusID = requestID
|
|
|
|
consensusMsg.Task.RequestID = requestID
|
|
|
|
consensusMsg.Task.CallbackAddress = callbackAddress
|
|
|
|
consensusMsg.Task.CallbackMethodID = callbackMethodID
|
|
|
|
consensusMsg.Task = *task
|
|
|
|
cHash, err := hashstructure.Hash(task, hashstructure.FormatV2, nil)
|
2020-11-27 16:16:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signature, err := sigs.Sign(types.SigTypeEd25519, privateKey, []byte(fmt.Sprintf("%v", cHash)))
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-15 20:39:52 +00:00
|
|
|
consensusMsg.Task.Signature = signature.Data
|
2020-11-15 13:46:58 +00:00
|
|
|
message.Payload = consensusMsg
|
|
|
|
return &message, nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (ppp *PrePreparePool) IsExistingPrePrepare(prepareMsg *types2.Message) bool {
|
2020-12-07 16:01:10 +00:00
|
|
|
ppp.mut.RLock()
|
|
|
|
defer ppp.mut.RUnlock()
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
consensusMessage := prepareMsg.Payload
|
|
|
|
var exists bool
|
2021-03-15 20:39:52 +00:00
|
|
|
for _, v := range ppp.prePrepareMsgs[consensusMessage.Task.ConsensusID] {
|
2020-11-15 13:46:58 +00:00
|
|
|
if v.From == prepareMsg.From {
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (ppp *PrePreparePool) IsValidPrePrepare(prePrepare *types2.Message) bool {
|
2020-11-15 13:46:58 +00:00
|
|
|
// TODO here we need to do validation of tx itself
|
|
|
|
consensusMsg := prePrepare.Payload
|
2020-11-27 16:16:08 +00:00
|
|
|
|
|
|
|
// === verify task signature ===
|
|
|
|
err := verifyTaskSignature(consensusMsg)
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
2020-11-18 18:14:56 +00:00
|
|
|
logrus.Errorf("unable to verify signature: %v", err)
|
2020-11-15 13:46:58 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-11-27 16:16:08 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
|
2020-12-02 13:42:02 +00:00
|
|
|
// === verify if request exists in event log cache ===
|
2021-03-15 20:39:52 +00:00
|
|
|
requestEvent, err := ppp.eventLogCache.GetOracleRequestEvent("request_" + consensusMsg.Task.RequestID)
|
2020-12-02 13:42:02 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("the incoming request task event doesn't exist in the EVC, or is broken: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2021-03-15 20:39:52 +00:00
|
|
|
if bytes.Compare(requestEvent.CallbackAddress.Bytes(), consensusMsg.Task.CallbackAddress) != 0 ||
|
|
|
|
bytes.Compare(requestEvent.CallbackMethodID[:], consensusMsg.Task.CallbackMethodID) != 0 ||
|
2020-12-02 13:42:02 +00:00
|
|
|
requestEvent.OriginChain != consensusMsg.Task.OriginChain ||
|
|
|
|
requestEvent.RequestType != consensusMsg.Task.RequestType ||
|
|
|
|
requestEvent.RequestParams != consensusMsg.Task.RequestParams {
|
|
|
|
|
|
|
|
logrus.Errorf("the incoming task and cached request event don't match!")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2020-11-27 16:16:08 +00:00
|
|
|
// === verify election proof wincount preliminarily ===
|
|
|
|
if consensusMsg.Task.ElectionProof.WinCount < 1 {
|
|
|
|
logrus.Error("miner isn't a winner!")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
/////////////////////////////////
|
|
|
|
|
|
|
|
// === verify miner's eligibility to propose this task ===
|
|
|
|
err = ppp.miner.IsMinerEligibleToProposeTask(common.HexToAddress(consensusMsg.Task.MinerEth))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("miner is not eligible to propose task: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
/////////////////////////////////
|
|
|
|
|
|
|
|
// === verify election proof vrf ===
|
|
|
|
minerAddressMarshalled, err := prePrepare.Payload.Task.Miner.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to marshal miner address: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
electionProofRandomness, err := DrawRandomness(
|
|
|
|
consensusMsg.Task.BeaconEntries[1].Data,
|
|
|
|
crypto.DomainSeparationTag_ElectionProofProduction,
|
|
|
|
consensusMsg.Task.DrandRound,
|
|
|
|
minerAddressMarshalled,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to draw electionProofRandomness: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
err = VerifyVRF(consensusMsg.Task.Miner, electionProofRandomness, consensusMsg.Task.ElectionProof.VRFProof)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to verify election proof vrf: %v", err)
|
|
|
|
}
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
|
|
|
// === verify ticket vrf ===
|
|
|
|
ticketRandomness, err := DrawRandomness(
|
|
|
|
consensusMsg.Task.BeaconEntries[1].Data,
|
|
|
|
crypto.DomainSeparationTag_TicketProduction,
|
|
|
|
consensusMsg.Task.DrandRound-types.TicketRandomnessLookback,
|
|
|
|
minerAddressMarshalled,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to draw ticket electionProofRandomness: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err = VerifyVRF(consensusMsg.Task.Miner, ticketRandomness, consensusMsg.Task.Ticket.VRFProof)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to verify ticket vrf: %v", err)
|
|
|
|
}
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
|
|
|
// === compute wincount locally and verify values ===
|
|
|
|
mStake, nStake, err := ppp.miner.GetStakeInfo(common.HexToAddress(consensusMsg.Task.MinerEth))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to get miner stake: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
actualWinCount := consensusMsg.Task.ElectionProof.ComputeWinCount(*mStake, *nStake)
|
|
|
|
if consensusMsg.Task.ElectionProof.WinCount != actualWinCount {
|
|
|
|
logrus.Errorf("locally computed wincount isn't matching received value!", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
2020-12-02 21:18:47 +00:00
|
|
|
// === validate payload by specific-chain checks ===
|
2020-12-02 21:21:10 +00:00
|
|
|
if validationFunc := validation.GetValidationMethod(consensusMsg.Task.OriginChain, consensusMsg.Task.RequestType); validationFunc != nil {
|
2020-12-02 21:18:47 +00:00
|
|
|
err := validationFunc(consensusMsg.Task.Payload)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("payload validation has failed: %v", err)
|
2020-12-02 14:09:46 +00:00
|
|
|
}
|
2020-12-02 21:21:10 +00:00
|
|
|
} else {
|
|
|
|
logrus.Warnf("Origin chain [%v]/request type[%v] doesn't have any payload validation!", consensusMsg.Task.OriginChain, consensusMsg.Task.RequestType)
|
2020-12-02 14:09:46 +00:00
|
|
|
}
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (ppp *PrePreparePool) AddPrePrepare(prePrepare *types2.Message) {
|
2020-12-07 16:01:10 +00:00
|
|
|
ppp.mut.Lock()
|
|
|
|
defer ppp.mut.Unlock()
|
|
|
|
|
2021-03-15 20:39:52 +00:00
|
|
|
consensusID := prePrepare.Payload.Task.ConsensusID
|
2020-11-15 13:46:58 +00:00
|
|
|
if _, ok := ppp.prePrepareMsgs[consensusID]; !ok {
|
2020-11-18 19:53:52 +00:00
|
|
|
ppp.prePrepareMsgs[consensusID] = []*types2.Message{}
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ppp.prePrepareMsgs[consensusID] = append(ppp.prePrepareMsgs[consensusID], prePrepare)
|
|
|
|
}
|