Skip to content

Commit

Permalink
Modify to process CR proposal withdraw transaction first
Browse files Browse the repository at this point in the history
  • Loading branch information
RainFallsSilent committed Jun 28, 2020
1 parent 5d623f4 commit f4e8f9f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
3 changes: 2 additions & 1 deletion blockchain/txvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,8 @@ func (b *BlockChain) checkCRCProposalTransaction(txn *Transaction,
return errors.New("budgets exceeds 10% of CRC committee balance")
} else if amount > b.crCommittee.CRCCurrentStageAmount-
b.crCommittee.CRCCommitteeUsedAmount-proposalsUsedAmount {
return errors.New("budgets exceeds the balance of CRC committee")
return errors.New(fmt.Sprintf("budgets exceeds the balance of CRC committee, tx hash:%s, budgets:%s, need <= %s",
txn.Hash().String(), amount, b.crCommittee.CRCCurrentStageAmount-b.crCommittee.CRCCommitteeUsedAmount-proposalsUsedAmount))
} else if amount < 0 {
return errors.New("budgets is invalid")
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/txvalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,7 @@ func (s *txValidatorTestSuite) TestCheckCRCProposalTransaction() {
s.Chain.crCommittee.CRCCurrentStageAmount = common.Fixed64(100 * 1e8)
s.Chain.crCommittee.CRCCommitteeUsedAmount = common.Fixed64(99 * 1e8)
err = s.Chain.checkCRCProposalTransaction(txn, tenureHeight, 0)
s.EqualError(err, "budgets exceeds the balance of CRC committee")
s.Error(err, "budgets exceeds the balance of CRC committee")

s.Chain.crCommittee.CRCCommitteeUsedAmount = common.Fixed64(0)

Expand Down
25 changes: 24 additions & 1 deletion cr/state/committee_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2017-2020 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//
//

package state

Expand All @@ -17,6 +17,29 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSortTransactions(t *testing.T) {
txs := []*types.Transaction{
&types.Transaction{TxType: types.CoinBase},
&types.Transaction{TxType: types.TransferAsset},
&types.Transaction{TxType: types.TransferAsset},
&types.Transaction{TxType: types.CRCProposalTracking},
&types.Transaction{TxType: types.CRCProposalWithdraw},
&types.Transaction{TxType: types.CRCProposalWithdraw},
&types.Transaction{TxType: types.TransferAsset},
&types.Transaction{TxType: types.CRCProposalWithdraw},
}

sortTransactions(txs[1:])
assert.Equal(t, txs[0].TxType.Name(), "CoinBase")
assert.Equal(t, txs[1].TxType.Name(), "CRCProposalWithdraw")
assert.Equal(t, txs[2].TxType.Name(), "CRCProposalWithdraw")
assert.Equal(t, txs[3].TxType.Name(), "CRCProposalWithdraw")
assert.Equal(t, txs[4].TxType.Name(), "TransferAsset")
assert.Equal(t, txs[5].TxType.Name(), "TransferAsset")
assert.Equal(t, txs[6].TxType.Name(), "CRCProposalTracking")
assert.Equal(t, txs[7].TxType.Name(), "TransferAsset")
}

func TestNewCRCommittee(t *testing.T) {
committee := NewCommittee(&config.DefaultParams)

Expand Down
19 changes: 19 additions & 0 deletions cr/state/committeeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package state

import (
"bytes"
"sort"

"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/core/types"
Expand All @@ -19,11 +20,29 @@ import (
// packed into a block. Then loop through the transactions to update CR
// state and votes according to transactions content.
func (c *Committee) processTransactions(txs []*types.Transaction, height uint32) {
sortedTxs := make([]*types.Transaction, 0)
if len(txs) < 1 {
return
}
for _, tx := range txs {
sortedTxs = append(sortedTxs, tx)
}
sortTransactions(sortedTxs[1:])
for _, tx := range sortedTxs {
c.processTransaction(tx, height)
}
}

// sortTransactions purpose is to process some transaction first.
func sortTransactions(txs []*types.Transaction) {
sort.Slice(txs, func(i, j int) bool {
if txs[i].IsCRCProposalWithdrawTx() {
return true
}
return !txs[j].IsCRCProposalWithdrawTx()
})
}

// processTransaction take a transaction and the height it has been packed into
// a block, then update producers state and votes according to the transaction
// content.
Expand Down
1 change: 1 addition & 0 deletions mempool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func (mp *TxPool) checkAndCleanAllTransactions() {
log.Warn("[checkAndCleanAllTransactions] check transaction context failed,", err)
deleteCount++
mp.doRemoveTransaction(tx)
continue
}
if tx.IsCRCProposalTx() {
blockchain.RecordCRCProposalAmount(&proposalsUsedAmount, tx)
Expand Down

0 comments on commit f4e8f9f

Please sign in to comment.