Skip to content

Commit

Permalink
sync checkTxn and insertTxn (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawliet-Chan committed Jul 30, 2024
1 parent 4f6c52f commit 2b6a9b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions common/yerror/basic_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
PoolOverflow error = errors.New("pool size is full")
TxnTimeoutErr error = errors.New("Txn time out")
TxnTooLarge error = errors.New("the size of txn is too large")
TxnDuplicated error = errors.New("Transaction duplicated")
)

var ErrBlockNotFound error = errors.New("block not found")
Expand Down
20 changes: 15 additions & 5 deletions core/kernel/handle_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kernel
import (
"github.com/sirupsen/logrus"
"github.com/yu-org/yu/common"
"github.com/yu-org/yu/common/yerror"
"github.com/yu-org/yu/core"
"github.com/yu-org/yu/core/context"
. "github.com/yu-org/yu/core/types"
Expand All @@ -21,11 +22,7 @@ func (k *Kernel) HandleTxn(signedWrCall *core.SignedWrCall) error {
return err
}

if k.CheckReplayAttack(stxn.TxnHash) {
return nil
}

err = k.Pool.CheckTxn(stxn)
err = k.handleTxnLocally(stxn)
if err != nil {
return err
}
Expand All @@ -37,6 +34,19 @@ func (k *Kernel) HandleTxn(signedWrCall *core.SignedWrCall) error {
}
}()

return nil
}

func (k *Kernel) handleTxnLocally(stxn *SignedTxn) error {
k.mutex.Lock()
defer k.mutex.Unlock()
if k.CheckReplayAttack(stxn.TxnHash) {
return yerror.TxnDuplicated
}
err := k.Pool.CheckTxn(stxn)
if err != nil {
return err
}
return k.Pool.Insert(stxn)
}

Expand Down
2 changes: 1 addition & 1 deletion core/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type Kernel struct {
sync.Mutex
mutex sync.Mutex

cfg *KernelConf

Expand Down

0 comments on commit 2b6a9b7

Please sign in to comment.