From 7f1e7ec6501eca92a430ce92f3a8f4a7d1fc4d89 Mon Sep 17 00:00:00 2001 From: Paladz Date: Wed, 21 Oct 2020 13:45:56 +0800 Subject: [PATCH] edit the filter (#550) * edit the filter * edit small changes Co-authored-by: paladz --- protocol/asset_filter.go | 5 ++-- protocol/asset_filter_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++ protocol/txpool_test.go | 5 ++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 protocol/asset_filter_test.go diff --git a/protocol/asset_filter.go b/protocol/asset_filter.go index cd9fccd6..fe7ec25d 100644 --- a/protocol/asset_filter.go +++ b/protocol/asset_filter.go @@ -3,7 +3,7 @@ package protocol import ( "strings" - "github.com/bytom/vapor/consensus" + "github.com/bytom/vapor/common" "github.com/bytom/vapor/protocol/bc/types" ) @@ -16,7 +16,6 @@ type AssetFilter struct { // which is a strings list cancated via comma func NewAssetFilter(whitelist string) *AssetFilter { af := &AssetFilter{whitelist: make(map[string]struct{})} - af.whitelist[consensus.BTMAssetID.String()] = struct{}{} for _, assetID := range strings.Split(whitelist, ",") { af.whitelist[strings.ToLower(assetID)] = struct{}{} } @@ -28,7 +27,7 @@ func NewAssetFilter(whitelist string) *AssetFilter { // No need to check the output assets types becauese they must have been cover in input assets types func (af *AssetFilter) IsDust(tx *types.Tx) bool { for _, input := range tx.Inputs { - if _, ok := input.TypedInput.(*types.CrossChainInput); !ok { + if crossChainInput, ok := input.TypedInput.(*types.CrossChainInput); !ok || !common.IsOpenFederationIssueAsset(crossChainInput.AssetDefinition) { continue } diff --git a/protocol/asset_filter_test.go b/protocol/asset_filter_test.go new file mode 100644 index 00000000..78979429 --- /dev/null +++ b/protocol/asset_filter_test.go @@ -0,0 +1,64 @@ +package protocol + +import ( + "testing" + + "github.com/bytom/vapor/protocol/bc" + "github.com/bytom/vapor/protocol/bc/types" + "github.com/bytom/vapor/testutil" +) + +func TestIsDust(t *testing.T) { + assetFilter := NewAssetFilter("184e1cc4ee4845023888810a79eed7a42c02c544cf2c61ceac05e176d575bd46") + cases := []struct { + tx *types.Tx + wantIsDust bool + }{ + { + tx: types.NewTx(types.TxData{ + Inputs: []*types.TxInput{ + types.NewCrossChainInput(nil, bc.Hash{}, testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 1e8, 1, 1, []byte("{\n \"decimals\": 8,\n \"description\": \"Bytom Official Issue\",\n \"name\": \"BTM\",\n \"symbol\": \"BTM\"\n}"), []byte("assetbtm"))}, + Outputs: []*types.TxOutput{ + types.NewIntraChainOutput(testutil.MustDecodeAsset("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 1e8, []byte{0x51}), + }, + }), + wantIsDust: false, + }, + { + tx: types.NewTx(types.TxData{ + Inputs: []*types.TxInput{ + types.NewCrossChainInput(nil, bc.Hash{}, testutil.MustDecodeAsset("184e1cc4ee4845023888810a79eed7a42c02c544cf2c61ceac05e176d575bd46"), 1e8, 1, 1, []byte("{\n \"decimals\": 6,\n \"description\": {\n \"issue_asset_action\": \"open_federation_cross_chain\"\n },\n \"name\": \"USDT\",\n \"quorum\": \"3\",\n \"reissue\": \"true\",\n \"symbol\": \"USDT\"\n}"), []byte("assetusdt"))}, + Outputs: []*types.TxOutput{ + types.NewIntraChainOutput(testutil.MustDecodeAsset("184e1cc4ee4845023888810a79eed7a42c02c544cf2c61ceac05e176d575bd46"), 1e8, []byte{0x51}), + }, + }), + wantIsDust: false, + }, + { + tx: types.NewTx(types.TxData{ + Inputs: []*types.TxInput{ + types.NewCrossChainInput(nil, bc.Hash{}, testutil.MustDecodeAsset("47fcd4d7c22d1d38931a6cd7767156babbd5f05bbbb3f7d3900635b56eb1b67e"), 1e8, 1, 1, []byte("{\n \"decimals\": 8,\n \"description\": {},\n \"name\": \"SUP\",\n \"quorum\": 1,\n \"reissue\": \"false\",\n \"symbol\": \"SUP\"\n}"), []byte("assetsup"))}, + Outputs: []*types.TxOutput{ + types.NewIntraChainOutput(testutil.MustDecodeAsset("47fcd4d7c22d1d38931a6cd7767156babbd5f05bbbb3f7d3900635b56eb1b67e"), 1e8, []byte{0x51}), + }, + }), + wantIsDust: false, + }, + { + tx: types.NewTx(types.TxData{ + Inputs: []*types.TxInput{ + types.NewCrossChainInput(nil, bc.Hash{}, testutil.MustDecodeAsset("c4644dd6643475d57ed624f63129ab815f282b61f4bb07646d73423a6e1a1563"), 1e8, 1, 1, []byte("{\n\"decimals\":6,\n\"description\":{\n\"issue_asset_action\":\"open_federation_cross_chain\"\n},\n\"name\":\"USDC\",\n\"quorum\":\"3\",\n\"reissue\":\"true\",\n\"symbol\":\"USDC\"\n}"), []byte("assetusdc"))}, + Outputs: []*types.TxOutput{ + types.NewIntraChainOutput(testutil.MustDecodeAsset("c4644dd6643475d57ed624f63129ab815f282b61f4bb07646d73423a6e1a1563"), 1e8, []byte{0x51}), + }, + }), + wantIsDust: true, + }, + } + + for i, c := range cases { + if gotIsDust := assetFilter.IsDust(c.tx); gotIsDust != c.wantIsDust { + t.Errorf("case %d: fail on AssetFilter TestIsDust", i) + } + } +} diff --git a/protocol/txpool_test.go b/protocol/txpool_test.go index 50e448e5..68e2a809 100644 --- a/protocol/txpool_test.go +++ b/protocol/txpool_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/davecgh/go-spew/spew" + log "github.com/sirupsen/logrus" "github.com/bytom/vapor/consensus" "github.com/bytom/vapor/database/storage" @@ -757,3 +758,7 @@ func TestProcessTransaction(t *testing.T) { } } } + +func init() { + log.SetLevel(log.ErrorLevel) +} -- 2.11.0