github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/consense/dpoa/implement.go (about)

     1  package dpoa
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/sixexorg/magnetic-ring/common"
     8  	"github.com/sixexorg/magnetic-ring/consense/dpoa/comm"
     9  	"github.com/sixexorg/magnetic-ring/crypto"
    10  	"github.com/sixexorg/magnetic-ring/log"
    11  )
    12  
    13  func (t *TransAction) ConsenseDone(peerIdx string, msg interface{}, msgHash common.Hash) {
    14  	if t.dpoaMgr.store.isEarth() {
    15  		return
    16  	}
    17  	pMsg, ok := msg.(*comm.ConsedoneMsg)
    18  	if !ok {
    19  		//log.Error("invalid msg with proposal msg type")
    20  		return
    21  	}
    22  	//log.Info("Server run receiveFromPeer ConsenseDone", "peerIdx", peerIdx, "GetBlockNum", pMsg.GetBlockNum(), "getLatestBlockNumber", t.dpoaMgr.store.getLatestBlockNumber()+1)
    23  	if pMsg.BlockNumber == t.GetCurrentBlockNo() { //
    24  		st := t.dpoaMgr.GetpaxState()
    25  		if st.state != PaxStateProcessing {
    26  			//log.Info("Server run receiveFromPeer ConsenseDone stateerr", "peerIdx", peerIdx, "state", st.state)
    27  			return
    28  		}
    29  		if st.patiCfg.View != pMsg.View {
    30  			//log.Info("Server run receiveFromPeer ConsenseDone viewerr", "peerIdx", peerIdx, "patiCfg.View", st.patiCfg.View, "pMsg.View", pMsg.View)
    31  			return
    32  		}
    33  
    34  
    35  		buf, err := common.Hex2Bytes(pMsg.PublicKey)
    36  		if err != nil {
    37  			return
    38  		}
    39  
    40  		publicKey, _ := crypto.UnmarshalPubkey(buf)
    41  		if _, err := publicKey.Verify(pMsg.BlockData, pMsg.SigData); err != nil {
    42  			//log.Info("-------Server run receiveFromPeer ConsenseDone Verify err", "peerIdx", peerIdx, "err", err)
    43  			return
    44  		}
    45  
    46  		block := &comm.Block{}
    47  		block.Deserialize(pMsg.BlockData)
    48  		if block != nil && block.Block != nil && block.Block.Header != nil {
    49  			log.Info("func dpoa ConsenseDone yes 1", "blockHeight", block.Block.Header.Height, "txlen", block.Block.Transactions.Len())
    50  		} else {
    51  			log.Info("func dpoa ConsenseDone no block nil")
    52  		}
    53  
    54  		/*log.Info("Server run receiveFromPeer ConsenseDone", "peerIdx", peerIdx, "GetBlockNum", pMsg.GetBlockNum(),
    55  		"len(ProcSigs)", len(block.Block.Sigs.ProcSigs), "len(FailerSigs)", len(block.Block.Sigs.FailerSigs), "len(TimeoutSigs)", len(block.Block.Sigs.TimeoutSigs))
    56  		*/
    57  
    58  		if t.dpoaMgr.role == PaxRoleProcesser {
    59  
    60  			consenseblk := &comm.Block{}
    61  			if t.dpoaMgr.paxosIns.dataPhase2 == nil {
    62  				return
    63  			}
    64  			consenseblk.Deserialize(t.dpoaMgr.paxosIns.dataPhase2.Av)
    65  			//fmt.Println("PaxRoleProcesser=====================!!!===============:", len(consenseblk.Block.Sigs.ProcSigs), len(consenseblk.Block.Sigs.FailerSigs), len(consenseblk.Block.Sigs.TimeoutSigs))
    66  			if consenseblk.Block.Hash() != block.Block.Hash() {
    67  				log.Info("-------Server run receiveFromPeer ConsenseDone blkhash err", "peerIdx", peerIdx, "consense hash", consenseblk.Block.Hash(), "hash", block.Block.Hash())
    68  				return
    69  			}
    70  
    71  			//fmt.Println("-------------------ConsenseDone sealProposal")
    72  			log.Info("func dpoa ConsenseDone yes 2", "blockHeight", block.Block.Header.Height, "txlen", block.Block.Transactions.Len())
    73  			t.dpoaMgr.store.sealBlock(block)
    74  		} else if t.dpoaMgr.role == PaxRoleObserver {
    75  
    76  		} else if t.dpoaMgr.role == PaxRoleFail {
    77  
    78  		}
    79  	}
    80  }
    81  
    82  func (t *TransAction) ConsensePrepare(peerIdx string, msg interface{}, msgHash common.Hash) {
    83  	pMsg, ok := msg.(*comm.PrepareMsg)
    84  	if !ok {
    85  		//log.Error("invalid msg with proposal msg type")
    86  		return
    87  	}
    88  	p, _ := json.Marshal(pMsg)
    89  	log.Info("-------Server run receiveFromPeer ConsensePrepare", "peerIdx", peerIdx, "msg", string(p), "CurrentBlock", t.GetCurrentBlockNo())
    90  
    91  	msgBlkNum := pMsg.Msg.BlockNumber
    92  	if msgBlkNum > t.GetCurrentBlockNo() {
    93  	} else if msgBlkNum < t.GetCurrentBlockNo() {
    94  
    95  		if msgBlkNum <= t.GetCommittedBlockNo() {
    96  			if msgBlkNum+10 < t.GetCommittedBlockNo() {
    97  				log.Info("server get proposal msg for block, from, current committed",
    98  					"accountStr", t.cfg.accountStr, "from", msgBlkNum, "committed", t.GetCommittedBlockNo())
    99  				t.heartbeat()
   100  			}
   101  			return
   102  		}
   103  	} else {
   104  		if err := t.msgpool.AddMsg(pMsg, msgHash); err != nil {
   105  			log.Error("failed to add proposal msg to pool", "msgBlkNum", msgBlkNum)
   106  			return
   107  		}
   108  		b, _ := json.Marshal(pMsg.Msg)
   109  
   110  		buf, err := common.Hex2Bytes(peerIdx)
   111  		if err != nil {
   112  			return
   113  		}
   114  
   115  		publicKey, _ := crypto.UnmarshalPubkey(buf)
   116  		if fg, err := publicKey.Verify(b, pMsg.Sig); !fg && err != nil {
   117  			log.Info("-------Server run receiveFromPeer ConsenseDone Verify", "peerIdx", peerIdx, "err", err)
   118  			return
   119  		}
   120  		t.dpoaMgr.SendData(pMsg.Msg)
   121  	}
   122  }
   123  
   124  func (t *TransAction) ConsensePromise(peerIdx string, msg interface{}, msgHash common.Hash) {
   125  	pMsg, ok := msg.(*comm.PromiseMsg)
   126  	if !ok {
   127  		log.Error("invalid msg with proposal msg type")
   128  		return
   129  	}
   130  	p, _ := json.Marshal(pMsg)
   131  	log.Info("-------Server run receiveFromPeer ConsensePromise", "peerIdx", peerIdx, "msg", string(p))
   132  
   133  	msgBlkNum := pMsg.Msg.BlockNumber
   134  	if msgBlkNum > t.GetCurrentBlockNo() {
   135  	} else if msgBlkNum < t.GetCurrentBlockNo() {
   136  
   137  		if msgBlkNum <= t.GetCommittedBlockNo() {
   138  			if msgBlkNum+10 < t.GetCommittedBlockNo() {
   139  				log.Info("server %d get proposal msg for block %d, from %d, current committed %d",
   140  					t.cfg.account.PublicKey, msgBlkNum, t.GetCommittedBlockNo())
   141  				t.heartbeat()
   142  			}
   143  			return
   144  		}
   145  	} else {
   146  		if err := t.msgpool.AddMsg(pMsg, msgHash); err != nil {
   147  			log.Error("failed to add proposal msg (%d) to pool", msgBlkNum)
   148  			return
   149  		}
   150  		b, _ := json.Marshal(pMsg.Msg)
   151  
   152  		buf, err := common.Hex2Bytes(peerIdx)
   153  		if err != nil {
   154  			return
   155  		}
   156  
   157  		publicKey, _ := crypto.UnmarshalPubkey(buf)
   158  		if _, err := publicKey.Verify(b, pMsg.Sig); err != nil {
   159  			log.Info("-------Server run receiveFromPeer ConsenseDone Verify err", "peerIdx", peerIdx, "err", err)
   160  			return
   161  		}
   162  		t.dpoaMgr.SendData(pMsg.Msg)
   163  	}
   164  }
   165  
   166  func (t *TransAction) ConsenseProposer(peerIdx string, msg interface{}, msgHash common.Hash) {
   167  	pMsg, ok := msg.(*comm.ProposerMsg)
   168  	if !ok {
   169  		log.Error("invalid msg with proposal msg type")
   170  		return
   171  	}
   172  	p, _ := json.Marshal(pMsg)
   173  	log.Info("-------Server run receiveFromPeer ConsenseProposer", "peerIdx", peerIdx, "msg", string(p))
   174  
   175  	msgBlkNum := pMsg.Msg.BlockNumber
   176  	if msgBlkNum > t.GetCurrentBlockNo() {
   177  	} else if msgBlkNum < t.GetCurrentBlockNo() {
   178  
   179  		if msgBlkNum <= t.GetCommittedBlockNo() {
   180  			if msgBlkNum+10 < t.GetCommittedBlockNo() {
   181  				log.Info("server %d get proposal msg for block %d, from %d, current committed %d",
   182  					t.cfg.account.PublicKey, msgBlkNum, t.GetCommittedBlockNo())
   183  				t.heartbeat()
   184  			}
   185  			return
   186  		}
   187  	} else {
   188  		if err := t.msgpool.AddMsg(pMsg, msgHash); err != nil {
   189  			log.Error("failed to add proposal msg (%d) to pool", msgBlkNum)
   190  			return
   191  		}
   192  		b, _ := json.Marshal(pMsg.Msg)
   193  
   194  		buf, err := common.Hex2Bytes(peerIdx)
   195  		if err != nil {
   196  			return
   197  		}
   198  
   199  		publicKey, _ := crypto.UnmarshalPubkey(buf)
   200  		if _, err := publicKey.Verify(b, pMsg.Sig); err != nil {
   201  			log.Info("-------Server run receiveFromPeer ConsenseDone Verify err:%v %v", peerIdx, err)
   202  			return
   203  		}
   204  		t.dpoaMgr.SendData(pMsg.Msg)
   205  	}
   206  }
   207  
   208  func (t *TransAction) ConsenseAccept(peerIdx string, msg interface{}, msgHash common.Hash) {
   209  	pMsg, ok := msg.(*comm.AcceptMsg)
   210  	if !ok {
   211  		log.Error("invalid msg with proposal msg type")
   212  		return
   213  	}
   214  	p, _ := json.Marshal(pMsg)
   215  	log.Info("-------Server run receiveFromPeer ConsenseAccept", "peerIdx", peerIdx, "msg", string(p))
   216  
   217  	msgBlkNum := pMsg.Msg.BlockNumber
   218  	if msgBlkNum > t.GetCurrentBlockNo() {
   219  	} else if msgBlkNum < t.GetCurrentBlockNo() {
   220  
   221  		if msgBlkNum <= t.GetCommittedBlockNo() {
   222  			if msgBlkNum+10 < t.GetCommittedBlockNo() {
   223  				log.Info("server get proposal msg for block from current committed",
   224  					"accountStr", t.cfg.accountStr, "block", msgBlkNum, "committed", t.GetCommittedBlockNo())
   225  				t.heartbeat()
   226  			}
   227  			return
   228  		}
   229  	} else {
   230  		if err := t.msgpool.AddMsg(pMsg, msgHash); err != nil {
   231  			log.Error("failed to add proposal msg to pool", "msgBlkNum", msgBlkNum)
   232  			return
   233  		}
   234  		//publicKey, _ := vconfig.Pubkey(peerIdx)
   235  		b, _ := json.Marshal(pMsg.Msg)
   236  		//if err := signature.Verify(publicKey, b, pMsg.Sig); err != nil{
   237  		//	log.Info("-------Server run receiveFromPeer ConsenseAccept Verify err:%v %v", peerIdx, err)
   238  		//	return
   239  		//}
   240  
   241  		buf, err := common.Hex2Bytes(peerIdx)
   242  		if err != nil {
   243  			return
   244  		}
   245  
   246  		publicKey, _ := crypto.UnmarshalPubkey(buf)
   247  		if _, err := publicKey.Verify(b, pMsg.Sig); err != nil {
   248  			log.Info("-------Server run receiveFromPeer ConsenseDone Verify err", "peerIdx", peerIdx, "err", err)
   249  			return
   250  		}
   251  		t.dpoaMgr.SendData(pMsg.Msg)
   252  	}
   253  }
   254  
   255  func (t *TransAction) ViewTimeout(peerIdx string, msg interface{}, msgHash common.Hash) {
   256  	pMsg, ok := msg.(*comm.ViewtimeoutMsg)
   257  	if !ok {
   258  		log.Error("invalid msg with heartbeat msg type")
   259  		return
   260  	}
   261  	log.Info("-------Server run receiveFromPeer ViewTimeout", "peerIdx", peerIdx, "GetCurrentBlockNo", t.GetCurrentBlockNo(), "GetBlockNum", pMsg.GetBlockNum(), "View", pMsg.RawData.View)
   262  	if t.GetCurrentBlockNo() != pMsg.GetBlockNum() { // 丢弃
   263  		return
   264  	}
   265  
   266  	err := CheckSigners(pMsg, peerIdx)
   267  	if err != nil {
   268  		log.Info("-------Server run receiveFromPeer ViewTimeout CheckSigners err", "peerIdx", peerIdx, "err", err)
   269  		return
   270  	}
   271  	st := t.dpoaMgr.GetpaxState()
   272  	log.Info("~~~~~~~~~~~~~~~~~~~~~~-------Server run receiveFromPeer ViewTimeout GetpaxState", "peerIdx", peerIdx, "state", st.state, "time", time.Now().String())
   273  	if st.state != PaxStateTimeout || st.patiCfg.BlkNum != pMsg.GetBlockNum() || st.patiCfg.View != pMsg.RawData.View {
   274  		log.Info("-------Server run receiveFromPeer ViewTimeout GetpaxState", "peerIdx", peerIdx, "state", st.state, "patiCfg", st.patiCfg)
   275  		time.Sleep(2 * time.Second) // 容错
   276  		st = t.dpoaMgr.GetpaxState()
   277  		if st.state != PaxStateTimeout || st.patiCfg.BlkNum != pMsg.GetBlockNum() || st.patiCfg.View != pMsg.RawData.View {
   278  			return
   279  		}
   280  	}
   281  	isfound := false
   282  	for _, pubkey := range t.dpoaMgr.partiCfg.ObserNodes {
   283  		if pubkey == peerIdx {
   284  			isfound = true
   285  			break
   286  		}
   287  	}
   288  	if !isfound {
   289  		log.Info("-------Server run receiveFromPeer ViewTimeout isnotfound err", peerIdx)
   290  		return
   291  	}
   292  	log.Info("-------Server run receiveFromPeer ViewTimeout add", "peerIdx", peerIdx)
   293  	t.msgpool.AddMsg(pMsg, msgHash, t.dpoaMgr.partiCfg)
   294  }
   295  
   296  func (t *TransAction) EarthFetchRspSigs(peerIdx string, msg interface{}, msgHash common.Hash) {
   297  	if !t.dpoaMgr.store.isEarth() {
   298  		return
   299  	}
   300  	pMsg, ok := msg.(*comm.EarthSigsFetchRspMsg)
   301  	if !ok {
   302  		log.Error("invalid msg with heartbeat msg type")
   303  		return
   304  	}
   305  	p, _ := json.Marshal(pMsg)
   306  	log.Info("-------Server run receiveFromPeer", "peerIdx", peerIdx, "EarthFetchRspSigs", string(p))
   307  	if t.dpoaMgr.store.inFailers(t.dpoaMgr.store.EpochBegin(), pMsg.PubKey) {
   308  		t.msgpool.AddMsg(pMsg, msgHash)
   309  	}
   310  }
   311  
   312  func (t *TransAction) PeerHeartbeatMessage(peerIdx string, msg interface{}, msgHash common.Hash) {
   313  	pMsg, ok := msg.(*comm.PeerHeartbeatMsg)
   314  	if !ok {
   315  		log.Error("invalid msg with heartbeat msg type")
   316  		return
   317  	}
   318  	if err := t.processHeartbeatMsg(peerIdx, pMsg); err != nil {
   319  		log.Error("server failed to process heartbeat", "accountStr", t.cfg.accountStr, "peerIdx", peerIdx, "err", err)
   320  	}
   321  	if pMsg.CommittedBlockNumber+10 < t.GetCommittedBlockNo() {
   322  		// delayed peer detected, response heartbeat with our chain Info
   323  		t.heartbeat()
   324  	}
   325  }
   326  
   327  func (t *TransAction) BlockFetchMessage(peerIdx string, msg interface{}, msgHash common.Hash) {
   328  	pMsg, ok := msg.(*comm.BlockFetchMsg)
   329  	if !ok {
   330  		log.Error("invalid msg with blockfetch msg type")
   331  		return
   332  	}
   333  	p, _ := json.Marshal(pMsg)
   334  	log.Info("-------Server run receiveFromPeer BlockFetchMessage", "peerIdx", peerIdx, "BlockFetchMessage", string(p))
   335  	blk, err := t.dpoaMgr.store.getSealedBlock(pMsg.BlockNum)
   336  	if err != nil {
   337  		log.Error("server failed to handle blockfetch from",
   338  			"accountStr", t.cfg.accountStr, "BlockNum", pMsg.BlockNum, "peerIdx", peerIdx, "err", err)
   339  		return
   340  	}
   341  	msg1, err := constructBlockFetchRespMsg(pMsg.BlockNum, blk, blk.Block.Hash())
   342  	if err != nil {
   343  		log.Error("server %v, failed to handle blockfetch %d from %v: %s",
   344  			"accountStr", t.cfg.accountStr, "BlockNum", pMsg.BlockNum, "peerIdx", peerIdx, "err", err)
   345  	} else {
   346  		//log.Infof("server %d, handle blockfetch %d from %d",
   347  		//	self.Index, pMsg.BlockNum, peerIdx)
   348  		t.msgSendC <- &SendMsgEvent{
   349  			ToPeer: peerIdx,
   350  			Msg:    msg1,
   351  		}
   352  	}
   353  }
   354  
   355  func (t *TransAction) BlockInfoFetchMessage(peerIdx string, msg interface{}, msgHash common.Hash) {
   356  	// handle block Info fetch msg
   357  	pMsg, ok := msg.(*comm.BlockInfoFetchMsg)
   358  	if !ok {
   359  		log.Error("invalid msg with blockinfo fetch msg type")
   360  		return
   361  	}
   362  	p, _ := json.Marshal(pMsg)
   363  	log.Info("-------Server run receiveFromPeer BlockInfoFetchMessage:%v %v", peerIdx, string(p))
   364  
   365  	maxCnt := 64
   366  	blkInfos := make([]*comm.BlockInfo_, 0)
   367  	targetBlkNum := t.GetCommittedBlockNo()
   368  	for startBlkNum := pMsg.StartBlockNum; startBlkNum <= targetBlkNum; startBlkNum++ {
   369  		blk, _ := t.dpoaMgr.store.getSealedBlock(startBlkNum)
   370  		if blk == nil {
   371  			break
   372  		}
   373  		blkInfos = append(blkInfos, &comm.BlockInfo_{
   374  			BlockNum: startBlkNum,
   375  			//Proposer: blk.getProposer(),
   376  		})
   377  		if len(blkInfos) >= maxCnt {
   378  			break
   379  		}
   380  	}
   381  	msg1, err := constructBlockInfoFetchRespMsg(blkInfos)
   382  	if err != nil {
   383  		//log.Errorf("server %d, failed to handle blockinfo fetch %d to %d: %s",
   384  		//	self.Index, pMsg.StartBlockNum, peerIdx, err)
   385  	} else {
   386  		//log.Infof("server %d, response blockinfo fetch to %d, blk %d, len %d",
   387  		//	self.Index, peerIdx, pMsg.StartBlockNum, len(blkInfos))
   388  		t.msgSendC <- &SendMsgEvent{
   389  			ToPeer: peerIdx,
   390  			Msg:    msg1,
   391  		}
   392  	}
   393  }