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 3979353
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 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
}
24 changes: 13 additions & 11 deletions codec/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ const (
maxBech32Size = 90
)

type Address LID

// Long ID
type LID [AddressLen]byte
type (
LID [AddressLen]byte // Long ID
Address LID
)

var EmptyAddress = [AddressLen]byte{}

// CreateLID returns [LID] made from concatenating
// some [i] with an [id]
func CreateLID(i uint8, id ids.ID) LID {
a := make([]byte, AddressLen)
a[0] = i
copy(a[1:], id[:])
return LID(a)
}

// CreateAddress returns [Address] made from concatenating
// [typeID] with [id].
func CreateAddress(typeID uint8, id ids.ID) Address {
Expand Down Expand Up @@ -82,13 +91,6 @@ func ParseAddressBech32(hrp, saddr string) (Address, error) {
return Address(p[:AddressLen]), nil
}

func CreateLID(idx uint8, txID ids.ID) LID {
a := make([]byte, AddressLen)
a[0] = idx
copy(a[1:], txID[:])
return LID(a)
}

func LIDToString(hrp string, lid LID) string {
addr, err := AddressBech32(hrp, Address(lid))
if err != nil {
Expand Down

0 comments on commit 3979353

Please sign in to comment.