OSDN Git Service

Mov (#518)
[bytom/vapor.git] / protocol / asset_filter.go
diff --git a/protocol/asset_filter.go b/protocol/asset_filter.go
new file mode 100644 (file)
index 0000000..cd9fccd
--- /dev/null
@@ -0,0 +1,42 @@
+package protocol
+
+import (
+       "strings"
+
+       "github.com/bytom/vapor/consensus"
+       "github.com/bytom/vapor/protocol/bc/types"
+)
+
+// AssetFilter is struct for allow open federation asset cross chain
+type AssetFilter struct {
+       whitelist map[string]struct{}
+}
+
+// NewAssetFilter returns a assetFilter according a whitelist,
+// 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{}{}
+       }
+       return af
+}
+
+// IsDust implements the DustFilterer interface.
+// It filters a transaction as long as there is one asset neither BTM or in the whitelist
+// 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 {
+                       continue
+               }
+
+               assetID := input.AssetID()
+               if _, ok := af.whitelist[assetID.String()]; !ok {
+                       return true
+               }
+       }
+
+       return false
+}