Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pending bubble hidden after block included #1592

Merged
merged 8 commits into from
May 19, 2021
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)
}