Skip to content

Commit

Permalink
fix(bpos): CheckBlockContext and CheckTransactionContext add Lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Houshoupei84 committed Aug 14, 2023
1 parent 1ec586a commit a562b33
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ func (b *BlockChain) connectBlock(node *BlockNode, block *Block, confirm *payloa
}
// The block must pass all of the validation rules which depend on the
// position of the block within the block chain.
if err := b.CheckBlockContext(block, node.Parent); err != nil {
if err := b.checkBlockContext(block, node.Parent); err != nil {
log.Error("PowCheckBlockContext error!", err)
return err
}
Expand Down
10 changes: 8 additions & 2 deletions blockchain/blockvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ func (b *BlockChain) checkTxsContext(block *Block) error {

var proposalsUsedAmount Fixed64
for i := 1; i < len(block.Transactions); i++ {
references, errCode := b.CheckTransactionContext(block.Height,
references, errCode := b.checkTransactionContext(block.Height,
block.Transactions[i], proposalsUsedAmount, block.Timestamp)
if errCode != nil {
return elaerr.SimpleWithMessage(elaerr.ErrBlockValidation, errCode,
"CheckTransactionContext failed when verify block")
"checkTransactionContext failed when verify block")
}

// Calculate transaction fee
Expand Down Expand Up @@ -293,6 +293,12 @@ func (b *BlockChain) checkTxsContext(block *Block) error {
}

func (b *BlockChain) CheckBlockContext(block *Block, prevNode *BlockNode) error {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.checkBlockContext(block, prevNode)
}

func (b *BlockChain) checkBlockContext(block *Block, prevNode *BlockNode) error {
// The genesis block is valid by definition.
if prevNode == nil {
return nil
Expand Down
10 changes: 9 additions & 1 deletion blockchain/txvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ func (b *BlockChain) CheckTransactionSanity(blockHeight uint32,
return txn.SanityCheck(para)
}

// CheckTransactionContext verifies a transaction with history transaction in ledger
func (b *BlockChain) CheckTransactionContext(blockHeight uint32,
tx interfaces.Transaction, proposalsUsedAmount common.Fixed64, timeStamp uint32) (
map[*common2.Input]common2.Output, elaerr.ELAError) {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.checkTransactionContext(blockHeight, tx, proposalsUsedAmount, timeStamp)
}

// CheckTransactionContext verifies a transaction with history transaction in ledger
func (b *BlockChain) checkTransactionContext(blockHeight uint32,
tx interfaces.Transaction, proposalsUsedAmount common.Fixed64, timeStamp uint32) (
map[*common2.Input]common2.Output, elaerr.ELAError) {

para := functions.GetTransactionParameters(
tx, blockHeight, timeStamp, b.chainParams, b, proposalsUsedAmount)
Expand Down
8 changes: 4 additions & 4 deletions core/transaction/coinbasetransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ func (a *CoinBaseTransaction) SpecialContextCheck() (result elaerr.ELAError, end
func (a *CoinBaseTransaction) ContextCheck(paras interfaces.Parameters) (map[*common2.Input]common2.Output, elaerr.ELAError) {

if err := a.SetParameters(paras); err != nil {
log.Warn("[CheckTransactionContext] set parameters failed.")
log.Warn("[checkTransactionContext] set parameters failed.")
return nil, elaerr.Simple(elaerr.ErrTxDuplicate, errors.New("invalid parameters"))
}

if err := a.HeightVersionCheck(); err != nil {
log.Warn("[CheckTransactionContext] height version check failed.")
log.Warn("[checkTransactionContext] height version check failed.")
return nil, elaerr.Simple(elaerr.ErrTxHeightVersion, nil)
}

// check if duplicated with transaction in ledger
if exist := a.IsTxHashDuplicate(*a.txHash); exist {
log.Warn("[CheckTransactionContext] duplicate transaction check failed.")
log.Warn("[checkTransactionContext] duplicate transaction check failed.")
return nil, elaerr.Simple(elaerr.ErrTxDuplicate, nil)
}

err, end := a.SpecialContextCheck()
if end {
log.Warn("[CheckTransactionContext] SpecialContextCheck failed:", err)
log.Warn("[checkTransactionContext] SpecialContextCheck failed:", err)
return nil, err
}

Expand Down
14 changes: 7 additions & 7 deletions core/transaction/transactionchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,37 @@ func (t *DefaultChecker) ContextCheck(params interfaces.Parameters) (
map[*common2.Input]common2.Output, elaerr.ELAError) {

if err := t.SetParameters(params); err != nil {
log.Warn("[CheckTransactionContext] set parameters failed.")
log.Warn("[checkTransactionContext] set parameters failed.")
return nil, elaerr.Simple(elaerr.ErrTxDuplicate, errors.New("invalid parameters"))
}

if err := t.parameters.Transaction.HeightVersionCheck(); err != nil {
log.Warn("[CheckTransactionContext] height version check failed.")
log.Warn("[checkTransactionContext] height version check failed.")
return nil, elaerr.Simple(elaerr.ErrTxHeightVersion, nil)
}

if exist := t.IsTxHashDuplicate(t.parameters.Transaction.Hash()); exist {
log.Warn("[CheckTransactionContext] duplicate transaction check failed.")
log.Warn("[checkTransactionContext] duplicate transaction check failed.")
return nil, elaerr.Simple(elaerr.ErrTxDuplicate, nil)
}

references, err := t.GetTxReference(t.parameters.Transaction)
if err != nil {
log.Warn("[CheckTransactionContext] get transaction reference failed")
log.Warn("[checkTransactionContext] get transaction reference failed")
return nil, elaerr.Simple(elaerr.ErrTxUnknownReferredTx, nil)
}
t.references = references

if t.parameters.BlockChain.GetState().GetConsensusAlgorithm() == state.POW {
if !t.parameters.Transaction.IsAllowedInPOWConsensus() {
log.Warnf("[CheckTransactionContext], %s transaction is not allowed in POW", t.parameters.Transaction.TxType().Name())
log.Warnf("[checkTransactionContext], %s transaction is not allowed in POW", t.parameters.Transaction.TxType().Name())
return nil, elaerr.Simple(elaerr.ErrTxValidation, nil)
}
}

// check double spent transaction
if blockchain.DefaultLedger.IsDoubleSpend(t.parameters.Transaction) {
log.Warn("[CheckTransactionContext] IsDoubleSpend check failed")
log.Warn("[checkTransactionContext] IsDoubleSpend check failed")
return nil, elaerr.Simple(elaerr.ErrTxDoubleSpend, nil)
}

Expand Down Expand Up @@ -656,7 +656,7 @@ func checkDestructionAddress(references map[*common2.Input]common2.Output) error
return nil
}

//tx interfaces.Transaction,
// tx interfaces.Transaction,
func (t *DefaultChecker) CheckTransactionFee(references map[*common2.Input]common2.Output) error {
log.Debug("DefaultChecker checkTransactionFee begin")
txn := t.parameters.Transaction
Expand Down
2 changes: 1 addition & 1 deletion mempool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (mp *TxPool) appendToTxPool(tx interfaces.Transaction) elaerr.ELAError {
}
if _, err := chain.CheckTransactionContext(
bestHeight+1, tx, mp.proposalsUsedAmount, 0); err != nil {
log.Warnf("[TxPool CheckTransactionContext] failed, hash: %s, err: %s", tx.Hash(),
log.Warnf("[TxPool checkTransactionContext] failed, hash: %s, err: %s", tx.Hash(),
err)
return err
}
Expand Down

0 comments on commit a562b33

Please sign in to comment.