Skip to content

Commit

Permalink
resolve feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj committed Sep 12, 2022
1 parent 8bcf96a commit 4aec383
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 74 deletions.
4 changes: 3 additions & 1 deletion dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ func (s *Service) validateTransaction(peerID peer.ID, head *types.Header, rt Run
Value: peerset.BadTransactionValue,
Reason: peerset.BadTransactionReason,
}, peerID)
return nil, false, nil
case errors.Is(err, runtimererrors.ErrUnknownTxn):
return nil, false, nil
}
return nil, false, nil
return nil, false, err
}

vtx := transaction.NewValidTransaction(tx, validity)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestAuthorModule_SubmitExtrinsic_invalid(t *testing.T) {

res := new(ExtrinsicHashResponse)
err := auth.SubmitExtrinsic(nil, &Extrinsic{extHex}, res)
expMsg := fmt.Errorf("%w: %s", runtimererrors.ErrInvalidTxn, "ancient birth block").Error()
expMsg := fmt.Sprintf("%s: %s", runtimererrors.ErrInvalidTxn, "ancient birth block")
require.EqualError(t, err, expMsg)

txOnPool := integrationTestController.stateSrv.Transaction.PendingInPool()
Expand Down
6 changes: 4 additions & 2 deletions dot/rpc/subscription/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,11 @@ func (c *WSConn) initExtrinsicWatch(reqID float64, params interface{}) (Listener

err = c.CoreAPI.HandleSubmittedExtrinsic(extBytes)
if err != nil {
if errors.Is(err, runtimererrors.ErrInvalidTxn) || errors.Is(err, runtimererrors.ErrUnknownTxn) {
switch {
case errors.Is(err, runtimererrors.ErrInvalidTxn),
errors.Is(err, runtimererrors.ErrUnknownTxn):
c.safeSend(newSubscriptionResponse(authorExtrinsicUpdatesMethod, extSubmitListener.subID, "invalid"))
} else {
default:
c.safeSendError(reqID, nil, err.Error())
}
return nil, fmt.Errorf("handling submitted extrinsic: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/subscription/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestWSConn_HandleConn(t *testing.T) {
errMsg := fmt.Errorf("%w: %s", errors.ErrInvalidTxn, transactionValidityErr.Error())

require.NoError(t, err)
coreAPI := new(mocks.CoreAPI)
coreAPI := mocks.NewCoreAPI(t)
coreAPI.On("HandleSubmittedExtrinsic", mock.AnythingOfType("types.Extrinsic")).
Return(errMsg)
wsconn.CoreAPI = coreAPI
Expand Down
105 changes: 70 additions & 35 deletions lib/runtime/errors/invalid_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,129 @@
package errors

import (
"fmt"

"github.com/ChainSafe/gossamer/pkg/scale"
)

// InvalidTransaction is child VDT of TransactionValidityError
// InvalidTransaction is child VDT of transactionValidityError
type InvalidTransaction scale.VaryingDataType

// Index fulfils the VaryingDataTypeValue interface. T
// Index returns the VDT index
func (InvalidTransaction) Index() uint {
return 0
}

// Call The call of the transaction is not expected
type Call struct{}

// Index Returns VDT index
// Index returns the VDT index
func (Call) Index() uint { return 0 }

// Error returns the error message associated with the Call
func (Call) Error() string {
return "call of the transaction is not expected"
}

// Payment General error to do with the inability to pay some fees (e.g. account balance too low)
type Payment struct{}

// Index Returns VDT index
// Index returns the VDT index
func (Payment) Index() uint { return 1 }

// Error returns the error message associated with the Payment
func (Payment) Error() string {
return "invalid payment"
}

// Future General error to do with the transaction not yet being valid (e.g. nonce too high)
type Future struct{}

// Index Returns VDT index
// Index returns the VDT index
func (Future) Index() uint { return 2 }

// Error returns the error message associated with the Future
func (Future) Error() string {
return "invalid transaction"
}

// Stale General error to do with the transaction being outdated (e.g. nonce too low)
type Stale struct{}

// Index Returns VDT index
// Index returns the VDT index
func (Stale) Index() uint { return 3 }

// Error returns the error message associated with the Stale
func (Stale) Error() string {
return "outdated transaction"
}

// BadProof General error to do with the transaction’s proofs (e.g. signature)
type BadProof struct{}

// Index Returns VDT index
// Index returns the VDT index
func (BadProof) Index() uint { return 4 }

// Error returns the error message associated with the BadProof
func (BadProof) Error() string {
return "bad proof"
}

// AncientBirthBlock The transaction birth block is ancient
type AncientBirthBlock struct{}

// Index Returns VDT index
// Index returns the VDT index
func (AncientBirthBlock) Index() uint { return 5 }

// Error returns the error message associated with the AncientBirthBlock
func (AncientBirthBlock) Error() string {
return "ancient birth block"
}

// ExhaustsResources The transaction would exhaust the resources of current block
type ExhaustsResources struct{}

// Index Returns VDT index
// Index returns the VDT index
func (ExhaustsResources) Index() uint { return 6 }

// Error returns the error message associated with the ExhaustsResources
func (ExhaustsResources) Error() string {
return "exhausts resources"
}

// InvalidCustom Any other custom invalid validity that is not covered
type InvalidCustom uint8

// Index Returns VDT index
// Index returns the VDT index
func (InvalidCustom) Index() uint { return 7 }

// Error returns the error message associated with the Call
func (i InvalidCustom) Error() string {
return newUnknownError(i).Error()
}

// BadMandatory An extrinsic with a Mandatory dispatch resulted in Error
type BadMandatory struct{}

// Index Returns VDT index
// Index returns the VDT index
func (BadMandatory) Index() uint { return 8 }

// Error returns the error message associated with the BadMandatory
func (BadMandatory) Error() string {
return "mandatory dispatch error"
}

// MandatoryDispatch A transaction with a mandatory dispatch
type MandatoryDispatch struct{}

// Index Returns VDT index
// Index returns the VDT index
func (MandatoryDispatch) Index() uint { return 9 }

// Error returns the error message associated with the MandatoryDispatch
func (MandatoryDispatch) Error() string {
return "invalid mandatory dispatch"
}

// Set will set a VaryingDataTypeValue using the underlying VaryingDataType
func (i *InvalidTransaction) Set(val scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*i)
Expand Down Expand Up @@ -102,29 +154,12 @@ func NewInvalidTransaction() InvalidTransaction {
return InvalidTransaction(vdt)
}

// Error returns the error message associated with the InvalidTransaction
func (i *InvalidTransaction) Error() string {
switch val := i.Value().(type) {
case Call:
return "call of the transaction is not expected"
case Payment:
return "invalid payment"
case Future:
return "invalid transaction"
case Stale:
return "outdated transaction"
case BadProof:
return "bad proof"
case AncientBirthBlock:
return "ancient birth block"
case ExhaustsResources:
return "exhausts resources"
case InvalidCustom:
return newUnknownError(val).Error()
case BadMandatory:
return "mandatory dispatch error"
case MandatoryDispatch:
return "invalid mandatory dispatch"
default:
panic("invalidTransaction: invalid error value")
value := i.Value()
err, ok := value.(error)
if !ok {
panic(fmt.Sprintf("%T does not implement the error type", value))
}
return err.Error()
}
35 changes: 17 additions & 18 deletions lib/runtime/errors/transaction_validity.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/ChainSafe/gossamer/pkg/scale"
)

// TransactionValidityError Information on a transaction's validity and, if valid,
// transactionValidityError Information on a transaction's validity and, if valid,
// on how it relates to other transactions. It is a result of the form:
// Result<ValidTransaction, TransactionValidityError>
type TransactionValidityError scale.VaryingDataType
// Result<ValidTransaction, transactionValidityError>
type transactionValidityError scale.VaryingDataType

var (
errInvalidType = errors.New("invalid validity type")
Expand All @@ -23,24 +23,24 @@ var (
)

// Set will set a VaryingDataTypeValue using the underlying VaryingDataType
func (tve *TransactionValidityError) Set(val scale.VaryingDataTypeValue) (err error) {
func (tve *transactionValidityError) Set(val scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*tve)
err = vdt.Set(val)
if err != nil {
return err
}
*tve = TransactionValidityError(vdt)
*tve = transactionValidityError(vdt)
return nil
}

// Value will return the value from the underlying VaryingDataType
func (tve *TransactionValidityError) Value() (val scale.VaryingDataTypeValue) {
func (tve *transactionValidityError) Value() (val scale.VaryingDataTypeValue) {
vdt := scale.VaryingDataType(*tve)
return vdt.Value()
}

// Error will return the error underlying TransactionValidityError
func (tve *TransactionValidityError) Error() string {
// Error will return the error underlying transactionValidityError
func (tve *transactionValidityError) Error() string {
invalidTxn, ok := tve.Value().(InvalidTransaction)
if !ok {
unknownTxn, ok2 := tve.Value().(UnknownTransaction)
Expand All @@ -52,37 +52,36 @@ func (tve *TransactionValidityError) Error() string {
return invalidTxn.Error()
}

// NewTransactionValidityError is constructor for TransactionValidityError
func NewTransactionValidityError() TransactionValidityError {
// NewTransactionValidityError is constructor for transactionValidityError
func NewTransactionValidityError() transactionValidityError {
vdt, err := scale.NewVaryingDataType(NewInvalidTransaction(), NewUnknownTransaction())
if err != nil {
panic(err)
}
return TransactionValidityError(vdt)
return transactionValidityError(vdt)
}

var (
ErrInvalidTxn = errors.New("invalid transaction")
ErrUnknownTxn = errors.New("unknown transaction")
)

// UnmarshalTransactionValidity Takes the result of the validateTransaction runtime call and unmarshalls it
// UnmarshalTransactionValidity takes the result of the validateTransaction runtime call and unmarshalls it
// TODO use custom result issue #2780
func UnmarshalTransactionValidity(res []byte) (*transaction.Validity, error) {
validTxn := transaction.Validity{}
txnValidityErrResult := NewTransactionValidityError()
txnValidityResult := scale.NewResult(validTxn, txnValidityErrResult)
err := scale.Unmarshal(res, &txnValidityResult)
if err != nil {
return nil, err
return nil, fmt.Errorf("scale decoding transaction validity result: %w", err)
}
txnValidityRes, err := txnValidityResult.Unwrap()
if err != nil {
scaleWrappedErr, ok := err.(scale.WrappedErr)
if ok {
txnValidityErr, ok := scaleWrappedErr.Err.(TransactionValidityError)
txnValidityErr, ok := scaleWrappedErr.Err.(transactionValidityError)
if !ok {
fmt.Println("here")
return nil, fmt.Errorf("%w: %T", errInvalidTypeCast, scaleWrappedErr.Err)
}

Expand All @@ -99,8 +98,8 @@ func UnmarshalTransactionValidity(res []byte) (*transaction.Validity, error) {
return nil, fmt.Errorf("%w: %T", errInvalidResult, err)
}
validity, ok := txnValidityRes.(transaction.Validity)
if ok {
return &validity, nil
if !ok {
return nil, fmt.Errorf("%w", errInvalidType)
}
return nil, fmt.Errorf("%w", errInvalidType)
return &validity, nil
}
2 changes: 1 addition & 1 deletion lib/runtime/errors/transaction_validity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_ErrorsAs_Function(t *testing.T) {
err = transactionValidityErr.Set(unknownTransaction)
require.NoError(t, err)

var txnValErr *TransactionValidityError
var txnValErr *transactionValidityError
isTxnValErr := errors.As(&transactionValidityErr, &txnValErr)
require.True(t, isTxnValErr)
}
Expand Down
Loading

0 comments on commit 4aec383

Please sign in to comment.