OSDN Git Service

edit the filter (#550) v4.8.11
authorPaladz <yzhu101@uottawa.ca>
Wed, 21 Oct 2020 05:45:56 +0000 (13:45 +0800)
committerGitHub <noreply@github.com>
Wed, 21 Oct 2020 05:45:56 +0000 (13:45 +0800)
* edit the filter

* edit small changes

Co-authored-by: paladz <colt@ColtdeMacBook-Pro.local>
protocol/asset_filter.go
protocol/asset_filter_test.go [new file with mode: 0644]
protocol/txpool_test.go

index cd9fccd..fe7ec25 100644 (file)
@@ -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 (file)
index 0000000..7897942
--- /dev/null
@@ -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)
+               }
+       }
+}
index 50e448e..68e2a80 100644 (file)
@@ -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)
+}