Skip to content

Commit

Permalink
Modify to check the total amount of all CRCProposal transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
RainFallsSilent committed Jun 28, 2020
1 parent d81fe4f commit 5d623f4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
16 changes: 15 additions & 1 deletion blockchain/blockvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,20 @@ func checkDuplicateTx(block *Block) error {
return nil
}

func RecordCRCProposalAmount(usedAmount *Fixed64, txn *Transaction) {
proposal, ok := txn.Payload.(*payload.CRCProposal)
if !ok {
return
}
for _, b := range proposal.Budgets {
*usedAmount += b.Amount
}
}

func (b *BlockChain) checkTxsContext(block *Block) error {
var totalTxFee = Fixed64(0)

var proposalsUsedAmount Fixed64
for i := 1; i < len(block.Transactions); i++ {
references, err := b.UTXOCache.GetTxReference(block.Transactions[i])
if err != nil {
Expand All @@ -248,13 +259,16 @@ func (b *BlockChain) checkTxsContext(block *Block) error {
}

if errCode := b.CheckTransactionContext(block.Height,
block.Transactions[i], references, 0); errCode != nil {
block.Transactions[i], references, proposalsUsedAmount); errCode != nil {
return elaerr.SimpleWithMessage(elaerr.ErrBlockValidation, errCode,
"CheckTransactionContext failed when verify block")
}

// Calculate transaction fee
totalTxFee += GetTxFee(block.Transactions[i], config.ELAAssetID, references)
if block.Transactions[i].IsCRCProposalTx() {
RecordCRCProposalAmount(&proposalsUsedAmount, block.Transactions[i])
}
}

err := b.checkCoinbaseTransactionContext(block.Height,
Expand Down
14 changes: 11 additions & 3 deletions mempool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func (mp *TxPool) checkAndCleanAllTransactions() {

txCount := len(mp.txnList)
var deleteCount int
var proposalsUsedAmount Fixed64
for _, tx := range mp.txnList {
references, err := chain.UTXOCache.GetTxReference(tx)
if err != nil {
Expand All @@ -248,12 +249,15 @@ func (mp *TxPool) checkAndCleanAllTransactions() {
deleteCount++
continue
}
err = chain.CheckTransactionContext(bestHeight+1, tx, references, 0)
err = chain.CheckTransactionContext(bestHeight+1, tx, references, proposalsUsedAmount)
if err != nil {
log.Warn("[checkAndCleanAllTransactions] check transaction context failed,", err)
deleteCount++
mp.doRemoveTransaction(tx)
}
if tx.IsCRCProposalTx() {
blockchain.RecordCRCProposalAmount(&proposalsUsedAmount, tx)
}
}

log.Debug(fmt.Sprintf("[checkAndCleanAllTransactions],transaction %d "+
Expand Down Expand Up @@ -474,7 +478,9 @@ func (mp *TxPool) doAddTransaction(tx *Transaction) elaerr.ELAError {
return err
}
mp.txnList[tx.Hash()] = tx
mp.dealAddProposalTx(tx)
if tx.IsCRCProposalTx() {
mp.dealAddProposalTx(tx)
}
return nil
}

Expand All @@ -485,7 +491,9 @@ func (mp *TxPool) doRemoveTransaction(tx *Transaction) {

if _, exist := mp.txnList[hash]; exist {
delete(mp.txnList, hash)
mp.dealDelProposalTx(tx)
if tx.IsCRCProposalTx() {
mp.dealDelProposalTx(tx)
}
mp.txFees.RemoveTx(hash, uint64(txSize), feeRate)
mp.removeTx(tx)
}
Expand Down
6 changes: 5 additions & 1 deletion pow/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func (pow *Service) GenerateBlock(minerAddr string,
return txs[i].FeePerKB > txs[j].FeePerKB
})

var proposalsUsedAmount common.Fixed64
for _, tx := range txs {
size := totalTxsSize + tx.GetSize()
if size > int(pact.MaxBlockContextSize) {
Expand All @@ -285,13 +286,16 @@ func (pow *Service) GenerateBlock(minerAddr string,
log.Warn("check transaction context failed, get transaction reference failed")
break
}
errCode := pow.chain.CheckTransactionContext(nextBlockHeight, tx, references, 0)
errCode := pow.chain.CheckTransactionContext(nextBlockHeight, tx, references, proposalsUsedAmount)
if errCode != nil {
log.Warn("check transaction context failed, wrong transaction:", tx.Hash().String())
continue
}
msgBlock.Transactions = append(msgBlock.Transactions, tx)
totalTxFee += tx.Fee
if tx.IsCRCProposalTx() {
blockchain.RecordCRCProposalAmount(&proposalsUsedAmount, tx)
}
txCount++
}

Expand Down

0 comments on commit 5d623f4

Please sign in to comment.