Skip to content

Commit

Permalink
Merge branch 'development' into fix-max-value
Browse files Browse the repository at this point in the history
  • Loading branch information
noot authored Jul 9, 2021
2 parents 399329d + cf93ad3 commit de7f62d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
4 changes: 2 additions & 2 deletions dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func TestBlockFinalizedListener_Listen(t *testing.T) {
}

func TestExtrinsicSubmitListener_Listen(t *testing.T) {
notifyImportedChan := make(chan *types.Block)
notifyFinalizedChan := make(chan *types.FinalisationInfo)
notifyImportedChan := make(chan *types.Block, 100)
notifyFinalizedChan := make(chan *types.FinalisationInfo, 100)

mockConnection := &MockWSConnAPI{}
esl := ExtrinsicSubmitListener{
Expand Down
11 changes: 7 additions & 4 deletions dot/rpc/subscription/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ var errCannotReadFromWebsocket = errors.New("cannot read message from websocket"
var errCannotUnmarshalMessage = errors.New("cannot unmarshal webasocket message data")
var logger = log.New("pkg", "rpc/subscription")

// DEFAULT_BUFFER_SIZE buffer size for channels
const DEFAULT_BUFFER_SIZE = 100

// WSConn struct to hold WebSocket Connection references
type WSConn struct {
Wsconn *websocket.Conn
Expand Down Expand Up @@ -228,7 +231,7 @@ func (c *WSConn) unsubscribeStorageListener(reqID float64, l Listener, _ interfa

func (c *WSConn) initBlockListener(reqID float64, _ interface{}) (Listener, error) {
bl := &BlockListener{
Channel: make(chan *types.Block),
Channel: make(chan *types.Block, DEFAULT_BUFFER_SIZE),
wsconn: c,
}

Expand Down Expand Up @@ -260,7 +263,7 @@ func (c *WSConn) initBlockListener(reqID float64, _ interface{}) (Listener, erro

func (c *WSConn) initBlockFinalizedListener(reqID float64, _ interface{}) (Listener, error) {
bfl := &BlockFinalizedListener{
channel: make(chan *types.FinalisationInfo),
channel: make(chan *types.FinalisationInfo, DEFAULT_BUFFER_SIZE),
wsconn: c,
}

Expand Down Expand Up @@ -299,10 +302,10 @@ func (c *WSConn) initExtrinsicWatch(reqID float64, params interface{}) (Listener

// listen for built blocks
esl := &ExtrinsicSubmitListener{
importedChan: make(chan *types.Block),
importedChan: make(chan *types.Block, DEFAULT_BUFFER_SIZE),
wsconn: c,
extrinsic: types.Extrinsic(extBytes),
finalisedChan: make(chan *types.FinalisationInfo),
finalisedChan: make(chan *types.FinalisationInfo, DEFAULT_BUFFER_SIZE),
}

if c.BlockAPI == nil {
Expand Down
19 changes: 14 additions & 5 deletions lib/babe/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ func (b *BlockBuilder) buildBlockExtrinsics(slot Slot) []*transaction.ValidTrans
txn := b.transactionState.Pop()
// Transaction queue is empty.
if txn == nil {
return included
}

// Move to next extrinsic.
if txn.Extrinsic == nil {
continue
}

Expand All @@ -270,6 +265,20 @@ func (b *BlockBuilder) buildBlockExtrinsics(slot Slot) []*transaction.ValidTrans
if _, ok := err.(*DispatchOutcomeError); !ok {
continue
}

// don't drop transactions that may be valid in a later block ie.
// run out of gas for this block or have a nonce that may be valid in a later block
var e *TransactionValidityError
if !errors.As(err, &e) {
continue
}

if errors.Is(e.msg, errExhaustsResources) || errors.Is(e.msg, errInvalidTransaction) {
hash, err := b.transactionState.Push(txn)
if err != nil {
logger.Debug("failed to re-add transaction to queue", "tx", hash, "error", err)
}
}
}

logger.Debug("build block applied extrinsic", "extrinsic", extrinsic)
Expand Down
46 changes: 32 additions & 14 deletions lib/babe/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,31 @@ func (e DispatchOutcomeError) Error() string {

// A TransactionValidityError is possible errors while checking the validity of a transaction
type TransactionValidityError struct {
msg string // description of error
msg error // description of error
}

func (e TransactionValidityError) Error() string {
return fmt.Sprintf("transaction validity error: %s", e.msg)
}

var (
errUnexpectedTxCall = errors.New("call of the transaction is not expected")
errInvalidPayment = errors.New("invalid payment")
errInvalidTransaction = errors.New("invalid transaction")
errOutdatedTransaction = errors.New("outdated transaction")
errBadProof = errors.New("bad proof")
errAncientBirthBlock = errors.New("ancient birth block")
errExhaustsResources = errors.New("exhausts resources")
errMandatoryDispatchError = errors.New("mandatory dispatch error")
errInvalidMandatoryDispatch = errors.New("invalid mandatory dispatch")
errLookupFailed = errors.New("lookup failed")
errValidatorNotFound = errors.New("validator not found")
)

func newUnknownError(data scale.VaryingDataTypeValue) error {
return fmt.Errorf("unknown error: %d", data)
}

// UnmarshalError occurs when unmarshalling fails
type UnmarshalError struct {
msg string
Expand Down Expand Up @@ -223,31 +241,31 @@ func determineErrType(vdt scale.VaryingDataType) error {
case Module:
return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())}
case Call:
return &TransactionValidityError{"call of the transaction is not expected"}
return &TransactionValidityError{errUnexpectedTxCall}
case Payment:
return &TransactionValidityError{"invalid payment"}
return &TransactionValidityError{errInvalidPayment}
case Future:
return &TransactionValidityError{"invalid transaction"}
return &TransactionValidityError{errInvalidTransaction}
case Stale:
return &TransactionValidityError{"outdated transaction"}
return &TransactionValidityError{errOutdatedTransaction}
case BadProof:
return &TransactionValidityError{"bad proof"}
return &TransactionValidityError{errBadProof}
case AncientBirthBlock:
return &TransactionValidityError{"ancient birth block"}
return &TransactionValidityError{errAncientBirthBlock}
case ExhaustsResources:
return &TransactionValidityError{"exhausts resources"}
return &TransactionValidityError{errExhaustsResources}
case InvalidCustom:
return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)}
return &TransactionValidityError{newUnknownError(val)}
case BadMandatory:
return &TransactionValidityError{"mandatory dispatch error"}
return &TransactionValidityError{errMandatoryDispatchError}
case MandatoryDispatch:
return &TransactionValidityError{"invalid mandatory dispatch"}
return &TransactionValidityError{errInvalidMandatoryDispatch}
case ValidityCannotLookup:
return &TransactionValidityError{"lookup failed"}
return &TransactionValidityError{errLookupFailed}
case NoUnsignedValidator:
return &TransactionValidityError{"validator not found"}
return &TransactionValidityError{errValidatorNotFound}
case UnknownCustom:
return &TransactionValidityError{fmt.Sprintf("unknown error: %d", val)}
return &TransactionValidityError{newUnknownError(val)}
}

return errInvalidResult
Expand Down
2 changes: 1 addition & 1 deletion lib/babe/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestApplyExtrinsicErrors(t *testing.T) {
_, ok := err.(*TransactionValidityError)
require.True(t, ok)
}
require.Equal(t, err.Error(), c.expected)
require.Equal(t, c.expected, err.Error())
})
}
}

0 comments on commit de7f62d

Please sign in to comment.