Skip to content

Commit

Permalink
fix: pending bubble hidden after block included (ChainSafe#1592)
Browse files Browse the repository at this point in the history
* fix: pending bubble hidden after block included

* chore: fix typo

* chore: change FindExtrinsic to HasExtrinsic

Co-authored-by: noot <36753753+noot@users.noreply.github.com>
  • Loading branch information
EclesioMeloJunior and noot committed May 19, 2021
1 parent 7574f10 commit 5826322
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 9 deletions.
17 changes: 8 additions & 9 deletions dot/rpc/subscription/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,17 @@ func (l *ExtrinsicSubmitListener) Listen() {
if block == nil {
continue
}
exts, err := block.Body.AsExtrinsics()
bodyHasExtrinsic, err := block.Body.HasExtrinsic(l.extrinsic)
if err != nil {
fmt.Printf("error %v\n", err)
}
for _, v := range exts {
if reflect.DeepEqual(v, l.extrinsic) {
resM := make(map[string]interface{})
resM["inBlock"] = block.Header.Hash().String()

l.importedHash = block.Header.Hash()
l.wsconn.safeSend(newSubscriptionResponse(AuthorExtrinsicUpdates, l.subID, resM))
}

if bodyHasExtrinsic {
resM := make(map[string]interface{})
resM["inBlock"] = block.Header.Hash().String()

l.importedHash = block.Header.Hash()
l.wsconn.safeSend(newSubscriptionResponse(AuthorExtrinsicUpdates, l.subID, resM))
}
}
}()
Expand Down
34 changes: 34 additions & 0 deletions dot/types/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package types

import (
"bytes"
"errors"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -173,3 +175,35 @@ func decodeOptionalBody(r io.Reader) (*optional.Body, error) {

return optional.NewBody(false, nil), nil
}

// HasExtrinsic returns true if body contains target Extrisic
// returns error when fails to encode decoded extrinsic on body
func (b *Body) HasExtrinsic(target Extrinsic) (bool, error) {
exts, err := b.AsExtrinsics()
if err != nil {
return false, err
}

// goes through the decreasing order due to the fact that extrinsicsToBody func (lib/babe/build.go)
// appends the valid transaction extrinsic on the end of the body
for i := len(exts) - 1; i >= 0; i-- {
currext := exts[i]

// if current extrinsic is equal the target then returns true
if bytes.Equal(target, currext) {
return true, nil
}

//otherwise try to encode and compare
encext, err := scale.Encode(currext)
if err != nil {
return false, fmt.Errorf("fail while scale encode: %w", err)
}

if len(encext) >= len(target) && bytes.Equal(target, encext[:len(target)]) {
return true, nil
}
}

return false, nil
}
34 changes: 34 additions & 0 deletions dot/types/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -77,3 +78,36 @@ func TestBody_EncodedExtrinsics(t *testing.T) {
require.NoError(t, err)
require.Equal(t, BytesArrayToExtrinsics(exts), res)
}

func TestBody_FindEncodedExtrinsic(t *testing.T) {
target := Extrinsic([]byte{0x1, 0x2, 0x3, 0x4, 0x5})

body1, err := NewBodyFromExtrinsics([]Extrinsic{})
require.Nil(t, err)

decodedTarget, err := scale.Decode(target, []byte{})
require.Nil(t, err)

body2, err := NewBodyFromExtrinsics([]Extrinsic{decodedTarget.([]byte)})
require.Nil(t, err)

tests := []struct {
body *Body
expect bool
}{
{
body: body1,
expect: false,
},
{
body: body2,
expect: true,
},
}

for _, test := range tests {
res, err := test.body.HasExtrinsic(target)
require.Nil(t, err)
require.Equal(t, test.expect, res)
}
}
20 changes: 20 additions & 0 deletions lib/babe/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,23 @@ func TestBuildBlock_failing(t *testing.T) {
t.Fatal("did not readd valid transaction to queue")
}
}

func TestDecodeExtrinsicBody(t *testing.T) {
ext := types.NewExtrinsic([]byte{0x1, 0x2, 0x3})
inh := [][]byte{{0x4, 0x5}, {0x6, 0x7}}

vtx := transaction.NewValidTransaction(ext, &transaction.Validity{})

body, err := extrinsicsToBody(inh, []*transaction.ValidTransaction{vtx})
require.Nil(t, err)
require.NotNil(t, body)

bodyext, err := body.AsExtrinsics()
require.Nil(t, err)
require.NotNil(t, bodyext)
require.Len(t, bodyext, 3)

contains, err := body.HasExtrinsic(ext)
require.Nil(t, err)
require.True(t, contains)
}

0 comments on commit 5826322

Please sign in to comment.