OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / protocol / asset_filter.go
1 package protocol
2
3 import (
4         "strings"
5
6         "github.com/bytom/vapor/common"
7         "github.com/bytom/vapor/protocol/bc/types"
8 )
9
10 // AssetFilter is struct for allow open federation asset cross chain
11 type AssetFilter struct {
12         whitelist map[string]struct{}
13 }
14
15 // NewAssetFilter returns a assetFilter according a whitelist,
16 // which is a strings list cancated via comma
17 func NewAssetFilter(whitelist string) *AssetFilter {
18         af := &AssetFilter{whitelist: make(map[string]struct{})}
19         for _, assetID := range strings.Split(whitelist, ",") {
20                 af.whitelist[strings.ToLower(assetID)] = struct{}{}
21         }
22         return af
23 }
24
25 // IsDust implements the DustFilterer interface.
26 // It filters a transaction as long as there is one asset neither BTM or in the whitelist
27 // No need to check the output assets types becauese they must have been cover in input assets types
28 func (af *AssetFilter) IsDust(tx *types.Tx) bool {
29         for _, input := range tx.Inputs {
30                 if crossChainInput, ok := input.TypedInput.(*types.CrossChainInput); !ok || !common.IsOpenFederationIssueAsset(crossChainInput.AssetDefinition) {
31                         continue
32                 }
33
34                 assetID := input.AssetID()
35                 if _, ok := af.whitelist[assetID.String()]; !ok {
36                         return true
37                 }
38         }
39
40         return false
41 }