Skip to content

Commit

Permalink
refactor(execution): simplify executors and tests (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jul 21, 2024
1 parent 3a9a3be commit 18a084b
Show file tree
Hide file tree
Showing 25 changed files with 1,053 additions and 837 deletions.
2 changes: 2 additions & 0 deletions cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ func buildInitCmd(parentCmd *cobra.Command) {

return
}

var mnemonic string
if *restoreOpt == "" {
mnemonic, _ = wallet.GenerateMnemonic(*entropyOpt)

cmd.PrintLine()
cmd.PrintInfoMsgf("Your wallet seed is:")
cmd.PrintInfoMsgBoldf(" " + mnemonic)
Expand Down
26 changes: 7 additions & 19 deletions execution/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/types/amount"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
)

// TransactionCommittedError is returned when an attempt is made
Expand All @@ -22,36 +21,25 @@ func (e TransactionCommittedError) Error() string {
e.ID.String())
}

// UnknownPayloadTypeError is returned when transaction payload type
// is not valid.
type UnknownPayloadTypeError struct {
PayloadType payload.Type
}

func (e UnknownPayloadTypeError) Error() string {
return fmt.Sprintf("unknown payload type: %s",
e.PayloadType.String())
}

// PastLockTimeError is returned when the lock time of a transaction
// LockTimeExpiredError is returned when the lock time of a transaction
// is in the past and has expired,
// indicating the transaction can no longer be executed.
type PastLockTimeError struct {
type LockTimeExpiredError struct {
LockTime uint32
}

func (e PastLockTimeError) Error() string {
return fmt.Sprintf("lock time is in the past: %v", e.LockTime)
func (e LockTimeExpiredError) Error() string {
return fmt.Sprintf("lock time expired: %v", e.LockTime)
}

// FutureLockTimeError is returned when the lock time of a transaction
// LockTimeInFutureError is returned when the lock time of a transaction
// is in the future,
// indicating the transaction is not yet eligible for processing.
type FutureLockTimeError struct {
type LockTimeInFutureError struct {
LockTime uint32
}

func (e FutureLockTimeError) Error() string {
func (e LockTimeInFutureError) Error() string {
return fmt.Sprintf("lock time is in the future: %v", e.LockTime)
}

Expand Down
65 changes: 23 additions & 42 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,26 @@ import (
"github.com/pactus-project/pactus/execution/executor"
"github.com/pactus-project/pactus/sandbox"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
)

type Executor interface {
Execute(trx *tx.Tx, sb sandbox.Sandbox) error
}
type Execution struct {
executors map[payload.Type]Executor
strict bool
}

func newExecution(strict bool) *Execution {
execs := make(map[payload.Type]Executor)
execs[payload.TypeTransfer] = executor.NewTransferExecutor(strict)
execs[payload.TypeBond] = executor.NewBondExecutor(strict)
execs[payload.TypeSortition] = executor.NewSortitionExecutor(strict)
execs[payload.TypeUnbond] = executor.NewUnbondExecutor(strict)
execs[payload.TypeWithdraw] = executor.NewWithdrawExecutor(strict)

return &Execution{
executors: execs,
strict: strict,
func Execute(trx *tx.Tx, sb sandbox.Sandbox) error {
exe, err := executor.MakeExecutor(trx, sb)
if err != nil {
return err
}
}

func NewExecutor() *Execution {
return newExecution(true)
}
exe.Execute()
sb.CommitTransaction(trx)

func NewChecker() *Execution {
return newExecution(false)
return nil
}

func (exe *Execution) Execute(trx *tx.Tx, sb sandbox.Sandbox) error {
func CheckAndExecute(trx *tx.Tx, sb sandbox.Sandbox, strict bool) error {
exe, err := executor.MakeExecutor(trx, sb)
if err != nil {
return err
}

if sb.IsBanned(trx.Payload().Signer()) {
return SignerBannedError{
addr: trx.Payload().Signer(),
Expand All @@ -50,31 +36,25 @@ func (exe *Execution) Execute(trx *tx.Tx, sb sandbox.Sandbox) error {
}
}

if err := exe.checkLockTime(trx, sb); err != nil {
if err := CheckLockTime(trx, sb, strict); err != nil {
return err
}

if err := exe.checkFee(trx); err != nil {
if err := CheckFee(trx); err != nil {
return err
}

e, ok := exe.executors[trx.Payload().Type()]
if !ok {
return UnknownPayloadTypeError{
PayloadType: trx.Payload().Type(),
}
}

if err := e.Execute(trx, sb); err != nil {
if err := exe.Check(strict); err != nil {
return err
}

exe.Execute()
sb.CommitTransaction(trx)

return nil
}

func (exe *Execution) checkLockTime(trx *tx.Tx, sb sandbox.Sandbox) error {
func CheckLockTime(trx *tx.Tx, sb sandbox.Sandbox, strict bool) error {
interval := sb.Params().TransactionToLiveInterval

if trx.IsSubsidyTx() {
Expand All @@ -85,18 +65,18 @@ func (exe *Execution) checkLockTime(trx *tx.Tx, sb sandbox.Sandbox) error {

if sb.CurrentHeight() > interval {
if trx.LockTime() < sb.CurrentHeight()-interval {
return PastLockTimeError{
return LockTimeExpiredError{
LockTime: trx.LockTime(),
}
}
}

if exe.strict {
if strict {
// In strict mode, transactions with future lock times are rejected.
// In non-strict mode, they are added to the transaction pool and
// processed once eligible.
if trx.LockTime() > sb.CurrentHeight() {
return FutureLockTimeError{
return LockTimeInFutureError{
LockTime: trx.LockTime(),
}
}
Expand All @@ -105,7 +85,8 @@ func (exe *Execution) checkLockTime(trx *tx.Tx, sb sandbox.Sandbox) error {
return nil
}

func (*Execution) checkFee(trx *tx.Tx) error {
func CheckFee(trx *tx.Tx) error {
// TODO: This check maybe can be done in BasicCheck?
if trx.IsFreeTx() {
if trx.Fee() != 0 {
return InvalidFeeError{
Expand Down
Loading

0 comments on commit 18a084b

Please sign in to comment.