Skip to content

Commit

Permalink
self review
Browse files Browse the repository at this point in the history
  • Loading branch information
wlawt committed May 3, 2024
1 parent 6b3435e commit 7bb262b
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions chain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,14 @@ func (t *Transaction) Execute(
ts.Rollback(ctx, actionStart)
return &Result{false, [][][]byte{{utils.ErrBytes(rerr)}}, maxUnits, maxFee}, nil
}
resultOutputs := [][][]byte{}
totalUsed := fees.Dimensions{}
var totalFeeRequired uint64
txSuccess := true

var (
totalFeeRequired uint64

resultOutputs = [][][]byte{}
totalUsed = fees.Dimensions{}
txSuccess = true
)
for i, action := range t.Actions {
actionID := action.GetActionID(uint8(i), t.id)
success, actionCUs, outputs, err := action.Execute(ctx, r, ts, timestamp, t.Auth.Actor(), actionID)
Expand All @@ -337,6 +341,7 @@ func (t *Transaction) Execute(
return handleRevert(ErrInvalidObject)
}
resultOutputs = append(resultOutputs, outputs)

if !success {
txSuccess = false
ts.Rollback(ctx, actionStart)
Expand Down Expand Up @@ -419,6 +424,7 @@ func (t *Transaction) Execute(
return handleRevert(err)
}
totalFeeRequired += feeRequired

refund := maxFee - feeRequired
if refund > 0 {
ts.DisableAllocation()
Expand Down Expand Up @@ -461,28 +467,6 @@ func (t *Transaction) marshalActions(p *codec.Packer) error {
return p.Err()
}

// todo: move below UnmarshalTx
func unmarshalActions(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action, bool],
) ([]Action, error) {
actionCount := p.UnpackInt(true)
actions := make([]Action, 0)
for i := 0; i < actionCount; i++ {
actionType := p.UnpackByte()
unmarshalAction, ok := actionRegistry.LookupIndex(actionType)
if !ok {
return nil, fmt.Errorf("%w: %d is unknown action type", ErrInvalidObject, actionType)
}
action, err := unmarshalAction(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal action", err)
}
actions = append(actions, action)
}
return actions, nil
}

func MarshalTxs(txs []*Transaction) ([]byte, error) {
if len(txs) == 0 {
return nil, ErrNoTxs
Expand Down Expand Up @@ -568,3 +552,26 @@ func UnmarshalTx(
tx.id = utils.ToID(tx.bytes)
return &tx, nil
}

func unmarshalActions(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action, bool],
) ([]Action, error) {
actionCount := p.UnpackInt(true)
actions := make([]Action, 0)

for i := 0; i < actionCount; i++ {
actionType := p.UnpackByte()
unmarshalAction, ok := actionRegistry.LookupIndex(actionType)
if !ok {
return nil, fmt.Errorf("%w: %d is unknown action type", ErrInvalidObject, actionType)
}

action, err := unmarshalAction(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal action", err)
}
actions = append(actions, action)
}
return actions, nil
}

0 comments on commit 7bb262b

Please sign in to comment.