2021-03-15 20:39:52 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/contracts/dioneDispute"
|
|
|
|
"github.com/Secured-Finance/dione/contracts/dioneOracle"
|
|
|
|
"github.com/Secured-Finance/dione/ethclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DisputeManager struct {
|
|
|
|
ctx context.Context
|
|
|
|
ethClient *ethclient.EthereumClient
|
|
|
|
pcm *PBFTConsensusManager
|
2021-04-19 19:46:17 +00:00
|
|
|
submissionMap map[string]*dioneOracle.DioneOracleSubmittedOracleRequest
|
2021-03-15 20:39:52 +00:00
|
|
|
disputeMap map[string]*dioneDispute.DioneDisputeNewDispute
|
2021-04-20 21:38:54 +00:00
|
|
|
voteWindow time.Duration
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:38:54 +00:00
|
|
|
func NewDisputeManager(ctx context.Context, ethClient *ethclient.EthereumClient, pcm *PBFTConsensusManager, voteWindow int) (*DisputeManager, error) {
|
2021-03-15 20:39:52 +00:00
|
|
|
newSubmittionsChan, submSubscription, err := ethClient.SubscribeOnNewSubmittions(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newDisputesChan, dispSubscription, err := ethClient.SubscribeOnNewDisputes(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dm := &DisputeManager{
|
|
|
|
ethClient: ethClient,
|
|
|
|
pcm: pcm,
|
|
|
|
ctx: ctx,
|
2021-04-19 19:46:17 +00:00
|
|
|
submissionMap: map[string]*dioneOracle.DioneOracleSubmittedOracleRequest{},
|
|
|
|
disputeMap: map[string]*dioneDispute.DioneDisputeNewDispute{},
|
2021-04-20 21:38:54 +00:00
|
|
|
voteWindow: time.Duration(voteWindow) * time.Second,
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
{
|
|
|
|
submSubscription.Unsubscribe()
|
|
|
|
dispSubscription.Unsubscribe()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case s := <-newSubmittionsChan:
|
|
|
|
{
|
2021-04-19 19:46:17 +00:00
|
|
|
dm.onNewSubmission(s)
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|
|
|
|
case d := <-newDisputesChan:
|
|
|
|
{
|
|
|
|
dm.onNewDispute(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return dm, nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:46:17 +00:00
|
|
|
func (dm *DisputeManager) onNewSubmission(submittion *dioneOracle.DioneOracleSubmittedOracleRequest) {
|
2021-06-08 21:30:23 +00:00
|
|
|
//c := dm.pcm.GetConsensusInfo(submittion.ReqID.String())
|
|
|
|
//if c == nil {
|
|
|
|
// // todo: warn
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//dm.submissionMap[submittion.ReqID.String()] = submittion
|
|
|
|
//
|
|
|
|
//submHashBytes := sha3.Sum256(submittion.Data)
|
|
|
|
//localHashBytes := sha3.Sum256(c.Task.Payload)
|
|
|
|
//submHash := hex.EncodeToString(submHashBytes[:])
|
|
|
|
//localHash := hex.EncodeToString(localHashBytes[:])
|
|
|
|
//if submHash != localHash {
|
|
|
|
// logrus.Debugf("submission of request id %s isn't valid - beginning dispute", c.Task.RequestID)
|
|
|
|
// addr := common.HexToAddress(c.Task.MinerEth)
|
|
|
|
// reqID, ok := big.NewInt(0).SetString(c.Task.RequestID, 10)
|
|
|
|
// if !ok {
|
|
|
|
// logrus.Errorf("cannot parse request id: %s", c.Task.RequestID)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// err := dm.ethClient.BeginDispute(addr, reqID)
|
|
|
|
// if err != nil {
|
|
|
|
// logrus.Errorf(err.Error())
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// disputeFinishTimer := time.NewTimer(dm.voteWindow)
|
|
|
|
// go func() {
|
|
|
|
// for {
|
|
|
|
// select {
|
|
|
|
// case <-dm.ctx.Done():
|
|
|
|
// return
|
|
|
|
// case <-disputeFinishTimer.C:
|
|
|
|
// {
|
|
|
|
// d, ok := dm.disputeMap[reqID.String()]
|
|
|
|
// if !ok {
|
|
|
|
// logrus.Error("cannot finish dispute: it doesn't exist in manager's dispute map!")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// err := dm.ethClient.FinishDispute(d.Dhash)
|
|
|
|
// if err != nil {
|
|
|
|
// logrus.Errorf(err.Error())
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// disputeFinishTimer.Stop()
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }()
|
|
|
|
//}
|
|
|
|
// TODO refactor due to new architecture with blockchain
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *DisputeManager) onNewDispute(dispute *dioneDispute.DioneDisputeNewDispute) {
|
2021-06-08 21:30:23 +00:00
|
|
|
//c := dm.pcm.GetConsensusInfo(dispute.RequestID.String())
|
|
|
|
//if c == nil {
|
|
|
|
// // todo: warn
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//subm, ok := dm.submissionMap[dispute.RequestID.String()]
|
|
|
|
//if !ok {
|
|
|
|
// // todo: warn
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//dm.disputeMap[dispute.RequestID.String()] = dispute
|
|
|
|
//
|
|
|
|
//if dispute.DisputeInitiator.Hex() == dm.ethClient.GetEthAddress().Hex() {
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//submHashBytes := sha3.Sum256(subm.Data)
|
|
|
|
//localHashBytes := sha3.Sum256(c.Task.Payload)
|
|
|
|
//submHash := hex.EncodeToString(submHashBytes[:])
|
|
|
|
//localHash := hex.EncodeToString(localHashBytes[:])
|
|
|
|
//if submHash == localHash {
|
|
|
|
// err := dm.ethClient.VoteDispute(dispute.Dhash, false)
|
|
|
|
// if err != nil {
|
|
|
|
// logrus.Errorf(err.Error())
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//err := dm.ethClient.VoteDispute(dispute.Dhash, true)
|
|
|
|
//if err != nil {
|
|
|
|
// logrus.Errorf(err.Error())
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
// TODO refactor due to new architecture with blockchain
|
2021-03-15 20:39:52 +00:00
|
|
|
}
|