Skip to content

Commit

Permalink
Remove error from funcs that panic instead
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Mar 11, 2024
1 parent 9c46f17 commit 5f56f3d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 34 deletions.
21 changes: 9 additions & 12 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,22 @@ type UTXOIDChainID interface {
FromOutputID(id OutputID) ChainID
}

func newAddress(addressType AddressType) (address Address, err error) {
func newAddress(addressType AddressType) Address {
switch addressType {
case AddressEd25519:
return &Ed25519Address{}, nil
return &Ed25519Address{}
case AddressAccount:
return &AccountAddress{}, nil
return &AccountAddress{}
case AddressNFT:
return &NFTAddress{}, nil
return &NFTAddress{}
case AddressAnchor:
return &AnchorAddress{}, nil
return &AnchorAddress{}
case AddressImplicitAccountCreation:
return &ImplicitAccountCreationAddress{}, nil
return &ImplicitAccountCreationAddress{}
case AddressMulti:
return &MultiAddress{}, nil
return &MultiAddress{}
case AddressRestricted:
return &RestrictedAddress{}, nil
return &RestrictedAddress{}
default:
panic(fmt.Sprintf("unknown address type %d", addressType))
}
Expand Down Expand Up @@ -252,10 +252,7 @@ func ParseBech32(s string) (NetworkPrefix, Address, error) {
}
}

addr, err := newAddress(addrType)
if err != nil {
return "", nil, err
}
addr := newAddress(addrType)

serixAPI := CommonSerixAPI()
n, err := serixAPI.Decode(context.TODO(), addrData, addr)
Expand Down
5 changes: 1 addition & 4 deletions signed_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ func (t *SignedTransaction) String() string {
// syntacticallyValidate syntactically validates the SignedTransaction.
func (t *SignedTransaction) syntacticallyValidate() error {
// limit unlock block count = input count
inputs, err := t.Transaction.Inputs()
if err != nil {
return err
}
inputs := t.Transaction.Inputs()

if len(t.Unlocks) != len(inputs) {
return ierrors.WithMessagef(ErrInputUnlockCountMismatch, "unlock count %d does not match inputs count %d", len(t.Unlocks), len(inputs))
Expand Down
9 changes: 3 additions & 6 deletions signed_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,14 @@ func TestTransaction_InputTypes(t *testing.T) {
}),
))

utxoInputs, err := signedTransaction.Transaction.Inputs()
require.NoError(t, err)
utxoInputs := signedTransaction.Transaction.Inputs()

commitmentInput := signedTransaction.Transaction.CommitmentInput()
require.NotNil(t, commitmentInput)

bicInputs, err := signedTransaction.Transaction.BICInputs()
require.NoError(t, err)
bicInputs := signedTransaction.Transaction.BICInputs()

rewardInputs, err := signedTransaction.Transaction.RewardInputs()
require.NoError(t, err)
rewardInputs := signedTransaction.Transaction.RewardInputs()

require.Equal(t, 2, len(utxoInputs))
require.Equal(t, 2, len(bicInputs))
Expand Down
Empty file added testlog.txt
Empty file.
16 changes: 8 additions & 8 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (t *Transaction) Clone() *Transaction {
}
}

func (t *Transaction) Inputs() ([]*UTXOInput, error) {
func (t *Transaction) Inputs() []*UTXOInput {
references := make([]*UTXOInput, 0, len(t.TransactionEssence.Inputs))
for _, input := range t.TransactionEssence.Inputs {
switch castInput := input.(type) {
Expand All @@ -144,7 +144,7 @@ func (t *Transaction) Inputs() ([]*UTXOInput, error) {
}
}

return references, nil
return references
}

// OutputsSet returns an OutputSet from the Transaction's outputs, mapped by their OutputID.
Expand All @@ -161,7 +161,7 @@ func (t *Transaction) OutputsSet() (OutputSet, error) {
return set, nil
}

func (t *Transaction) ContextInputs() (TransactionContextInputs, error) {
func (t *Transaction) ContextInputs() TransactionContextInputs {
references := make(TransactionContextInputs, 0, len(t.TransactionEssence.ContextInputs))
for _, input := range t.TransactionEssence.ContextInputs {
switch castInput := input.(type) {
Expand All @@ -172,10 +172,10 @@ func (t *Transaction) ContextInputs() (TransactionContextInputs, error) {
}
}

return references, nil
return references
}

func (t *Transaction) BICInputs() ([]*BlockIssuanceCreditInput, error) {
func (t *Transaction) BICInputs() []*BlockIssuanceCreditInput {
references := make([]*BlockIssuanceCreditInput, 0, len(t.TransactionEssence.ContextInputs))
for _, input := range t.TransactionEssence.ContextInputs {
switch castInput := input.(type) {
Expand All @@ -188,10 +188,10 @@ func (t *Transaction) BICInputs() ([]*BlockIssuanceCreditInput, error) {
}
}

return references, nil
return references
}

func (t *Transaction) RewardInputs() ([]*RewardInput, error) {
func (t *Transaction) RewardInputs() []*RewardInput {
references := make([]*RewardInput, 0, len(t.TransactionEssence.ContextInputs))
for _, input := range t.TransactionEssence.ContextInputs {
switch castInput := input.(type) {
Expand All @@ -204,7 +204,7 @@ func (t *Transaction) RewardInputs() ([]*RewardInput, error) {
}
}

return references, nil
return references
}

// Returns the first commitment input in the transaction if it exists or nil.
Expand Down
5 changes: 1 addition & 4 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,7 @@ type ExecFunc func(vm VirtualMachine, svCtx *Params) error
// ValidateUnlocks produces the UnlockedAddresses which will be set into the given Params and verifies that inputs are
// correctly unlocked and that the inputs commitment matches.
func ValidateUnlocks(signedTransaction *iotago.SignedTransaction, resolvedInputs ResolvedInputs) (unlockedAddrs UnlockedAddresses, err error) {
utxoInputs, err := signedTransaction.Transaction.Inputs()
if err != nil {
return nil, ierrors.Wrap(err, "failed to get inputs from transaction")
}
utxoInputs := signedTransaction.Transaction.Inputs()

var inputs iotago.Outputs[iotago.Output]
for _, input := range utxoInputs {
Expand Down

0 comments on commit 5f56f3d

Please sign in to comment.