From 16e7eef498bf50703b388fbd386107bbc9503286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Junior?= Date: Tue, 18 May 2021 22:56:48 -0400 Subject: [PATCH] fix: pending bubble hidden after block included (#1592) * 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> --- dot/rpc/subscription/listeners.go | 17 ++++++++-------- dot/types/body.go | 34 +++++++++++++++++++++++++++++++ dot/types/body_test.go | 34 +++++++++++++++++++++++++++++++ lib/babe/build_test.go | 20 ++++++++++++++++++ 4 files changed, 96 insertions(+), 9 deletions(-) diff --git a/dot/rpc/subscription/listeners.go b/dot/rpc/subscription/listeners.go index 579cfbba88..a6b5d97abc 100644 --- a/dot/rpc/subscription/listeners.go +++ b/dot/rpc/subscription/listeners.go @@ -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)) } } }() diff --git a/dot/types/body.go b/dot/types/body.go index a852a79621..52524fb29e 100644 --- a/dot/types/body.go +++ b/dot/types/body.go @@ -17,7 +17,9 @@ package types import ( + "bytes" "errors" + "fmt" "io" "math/big" @@ -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 +} diff --git a/dot/types/body_test.go b/dot/types/body_test.go index 1106b833a6..d34b22716b 100644 --- a/dot/types/body_test.go +++ b/dot/types/body_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/lib/scale" "github.com/stretchr/testify/require" ) @@ -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) + } +} diff --git a/lib/babe/build_test.go b/lib/babe/build_test.go index 156f60fde6..441f09b144 100644 --- a/lib/babe/build_test.go +++ b/lib/babe/build_test.go @@ -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) +}