From 3979353111c66f17f90611186a24e2d3969e4d54 Mon Sep 17 00:00:00 2001 From: William Law Date: Fri, 3 May 2024 13:44:49 -0400 Subject: [PATCH] self review --- chain/transaction.go | 59 +++++++++++++++++++++++++------------------- codec/address.go | 24 +++++++++--------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/chain/transaction.go b/chain/transaction.go index be86cd14f4..3a0320877d 100644 --- a/chain/transaction.go +++ b/chain/transaction.go @@ -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) @@ -337,6 +341,7 @@ func (t *Transaction) Execute( return handleRevert(ErrInvalidObject) } resultOutputs = append(resultOutputs, outputs) + if !success { txSuccess = false ts.Rollback(ctx, actionStart) @@ -419,6 +424,7 @@ func (t *Transaction) Execute( return handleRevert(err) } totalFeeRequired += feeRequired + refund := maxFee - feeRequired if refund > 0 { ts.DisableAllocation() @@ -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 @@ -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 +} diff --git a/codec/address.go b/codec/address.go index 5b95154f55..5d056c3453 100644 --- a/codec/address.go +++ b/codec/address.go @@ -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 { @@ -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 {