Skip to content

Commit

Permalink
remove legacy code about useless pipeline, fix vote bug (#137)
Browse files Browse the repository at this point in the history
* remove legacy code about useless pipeline, fix vote bug

* remove legacy code about useless pipeline, fix vote bug

* fix bug of vote

* fix bug of vote
  • Loading branch information
Sun Junyi authored and yucaowang committed Jun 25, 2019
1 parent ccf43da commit 8d9efa8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 221 deletions.
8 changes: 5 additions & 3 deletions contract/proposal/proposal.go
Expand Up @@ -209,10 +209,12 @@ func (prp *Proposal) runVote(desc *contract.TxDesc) error {
}
proposalTx, err := prp.ledger.QueryTransaction(proposalTxid)
if err != nil {
prp.log.Warn("vote fail, because proposal tx not found", "proposalTxid", fmt.Sprintf("%x", proposalTxid))
return err
proposalTx = prp.context.Block.GetTx(proposalTxid)
if proposalTx == nil {
prp.log.Warn("vote fail, because proposal tx not found", "proposalTxid", fmt.Sprintf("%x", proposalTxid))
return err
}
}

argValue, err := prp.getDescArg(proposalTx, "stop_vote_height")
if err != nil {
return err
Expand Down
177 changes: 0 additions & 177 deletions core/pipeline.go

This file was deleted.

43 changes: 12 additions & 31 deletions core/xchaincore.go
Expand Up @@ -96,7 +96,6 @@ type XChainCore struct {
stopFlag bool
proposal *proposal.Proposal
NativeCodeMgr *native.GeneralSCFramework
pipelineM *PipelineMiner

// isCoreMiner if current node is one of the core miners
isCoreMiner bool
Expand Down Expand Up @@ -277,8 +276,6 @@ func (xc *XChainCore) Init(bcname string, xlog log.Logger, cfg *config.NodeConfi
xc.Utxovm.RegisterVAT("consensus", xc.con, xc.con.GetVATWhiteList())
xc.Utxovm.RegisterVAT("kernel", ker, ker.GetVATWhiteList())

xc.pipelineM = NewPipelineMiner(xc)
go xc.pipelineM.Start()
go xc.Speed.ShowLoop(xc.log)
go xc.repostOfflineTx()
return nil
Expand Down Expand Up @@ -394,11 +391,6 @@ func (xc *XChainCore) SendBlock(in *pb.Block, hd *global.XContext) error {
xc.log.Debug("End to Find the same", "logid", in.Header.Logid, "blocks size", len(blocksIds), "cost", hd.Timer.Print(),
"genesis", global.F(xc.Ledger.GetMeta().RootBlockid),
"prehash", global.F(preblkhash), "utxo", global.F(xc.Utxovm.GetLatestBlockid()))
rbErr := xc.pipelineM.RollbackPrePlay()
if rbErr != nil {
xc.log.Warn("fail to rollback preplay contract", "err", rbErr)
return rbErr
}
// preblk 是跟区块同步的交点,判断preblk是不是当前utxo的位置
if bytes.Equal(xc.Utxovm.GetLatestBlockid(), preblkhash) {
xc.log.Debug("Equal The Same", "logid", in.Header.Logid, "cost", hd.Timer.Print())
Expand Down Expand Up @@ -500,11 +492,6 @@ func (xc *XChainCore) doMiner() {
// 如果Walk一直失败,建议不要挖矿了,而是报警处理
for !bytes.Equal(ledgerLastID, utxovmLastID) {
xc.log.Warn("ledger last blockid is not equal utxovm last id")
rbErr := xc.pipelineM.RollbackPrePlay()
if rbErr != nil {
xc.log.Warn("fail to rollback preplay contract", "err", rbErr)
return
}
err := xc.Utxovm.Walk(ledgerLastID)
if err != nil {
xc.log.Error("Walk error ", "ledger blockid", global.F(ledgerLastID),
Expand Down Expand Up @@ -557,37 +544,34 @@ func (xc *XChainCore) doMiner() {
}
xc.log.Trace("[Minning] get vatList success", "vatList", vatList)
txs = append(txs, vatList...)
// make fake block
txsUnconf, err := xc.Utxovm.GetUnconfirmedTx(false)
if err != nil {
xc.log.Warn("[Minning] fail to get unconfirmedtx")
return
}
txs = append(txs, txsUnconf...)
fakeBlock, err := xc.Ledger.FormatFakeBlock(txs, xc.address, xc.privateKey,
t.UnixNano(), curTerm, curBlockNum, meta.TipBlockid, xc.Utxovm.GetTotal())
if err != nil {
xc.log.Warn("[Minning] format block error", "logid")
xc.log.Warn("[Minning] format fake block error", "logid")
return
}
allFailedTxs := map[string]string{}
if txs, _, err = xc.Utxovm.TxOfRunningContractGenerate(txs, fakeBlock, xc.pipelineM.batch, xc.pipelineM.NeedInitCtx()); err != nil {
//2. pre-execute the contract
batch := xc.Utxovm.NewBatch()
if txs, _, err = xc.Utxovm.TxOfRunningContractGenerate(txs, fakeBlock, batch, true); err != nil {
if err.Error() != common.ErrContractExecutionTimeout.Error() {
xc.log.Warn("PrePlay failed", "error", err)
xc.log.Warn("PrePlay fake block failed", "error", err) //unexpected error
return
}
}
for txid, txErr := range fakeBlock.FailedTxs {
allFailedTxs[txid] = txErr
}
//2. 打包已经预执行过的未确认交易
batch, txsUnconf, failedTxs := xc.pipelineM.FetchTxs()
txs = append(txs, txsUnconf...)
for txid, txErr := range failedTxs {
allFailedTxs[txid] = txErr
}
minerTimer.Mark("PrePlay")
//3. 统一在最后插入矿工奖励
blockAward := xc.Ledger.GenesisBlock.CalcAward(xc.Ledger.GetMeta().TrunkHeight + 1)
awardtx, err := xc.Utxovm.GenerateAwardTx(xc.address, blockAward.String(), []byte{'1'})
minerTimer.Mark("GenAwardTx")
txs = append(txs, awardtx)
b, err := xc.Ledger.FormatPOWBlock(txs, xc.address, xc.privateKey,
t.UnixNano(), curTerm, curBlockNum, meta.TipBlockid, targetBits, xc.Utxovm.GetTotal(), allFailedTxs)
t.UnixNano(), curTerm, curBlockNum, meta.TipBlockid, targetBits, xc.Utxovm.GetTotal(), fakeBlock.FailedTxs)
if err != nil {
xc.log.Warn("[Minning] format block error", "logid", header.Logid, "err", err)
return
Expand Down Expand Up @@ -676,10 +660,7 @@ func (xc *XChainCore) Miner() int {
if s {
xc.SyncBlocks()
}
xc.pipelineM.Resume()
xc.doMiner()
} else {
xc.pipelineM.Pause()
}
meta := xc.Ledger.GetMeta()
xc.log.Info("Minner", "genesis", fmt.Sprintf("%x", meta.RootBlockid), "last", fmt.Sprintf("%x", meta.TipBlockid), "height", meta.TrunkHeight, "utxovm", fmt.Sprintf("%x", xc.Utxovm.GetLatestBlockid()))
Expand Down
10 changes: 0 additions & 10 deletions core/xchainmg_test.go
Expand Up @@ -354,16 +354,6 @@ func TestXChainMgBasic(t *testing.T) {
t.Log("is accepted: ", rootXCore.syncConfirm(blkStatus))
res2, _ := rootXCore.syncForOnce()
t.Log("sync for once", res2)
// test for xc.pipelineM
rollBackErr := rootXCore.pipelineM.RollbackPrePlay()
if rollBackErr != nil {
t.Error("roll back error ", rollBackErr.Error())
}
rootXCore.pipelineM.FetchTxs()
rootXCore.pipelineM.Pause()
rootXCore.pipelineM.Resume()
t.Log(rootXCore.pipelineM.NeedInitCtx())
rootXCore.pipelineM.doPrePlay()
rootXCore.doMiner()
// test fot BroadCastGetBlock
rootXCore.BroadCastGetBlock(blockID)
Expand Down
20 changes: 20 additions & 0 deletions pb/helper.go
Expand Up @@ -81,3 +81,23 @@ func (txIn *TxInputExt) GetVersion() string {
}
return fmt.Sprintf("%x_%d", txIn.RefTxid, txIn.RefOffset)
}

// ContainsTx returns whether a tx is included in a block
func (ib *InternalBlock) ContainsTx(txid []byte) bool {
for _, tx := range ib.Transactions {
if bytes.Equal(txid, tx.Txid) {
return true
}
}
return false
}

// GetTx returns a tx included in a block
func (ib *InternalBlock) GetTx(txid []byte) *Transaction {
for _, tx := range ib.Transactions {
if bytes.Equal(txid, tx.Txid) {
return tx
}
}
return nil
}

0 comments on commit 8d9efa8

Please sign in to comment.