OSDN Git Service

Mov (#518)
[bytom/vapor.git] / protocol / asset_filter.go
1 package protocol
2
3 import (
4         "strings"
5
6         "github.com/bytom/vapor/consensus"
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         af.whitelist[consensus.BTMAssetID.String()] = struct{}{}
20         for _, assetID := range strings.Split(whitelist, ",") {
21                 af.whitelist[strings.ToLower(assetID)] = struct{}{}
22         }
23         return af
24 }
25
26 // IsDust implements the DustFilterer interface.
27 // It filters a transaction as long as there is one asset neither BTM or in the whitelist
28 // No need to check the output assets types becauese they must have been cover in input assets types
29 func (af *AssetFilter) IsDust(tx *types.Tx) bool {
30         for _, input := range tx.Inputs {
31                 if _, ok := input.TypedInput.(*types.CrossChainInput); !ok {
32                         continue
33                 }
34
35                 assetID := input.AssetID()
36                 if _, ok := af.whitelist[assetID.String()]; !ok {
37                         return true
38                 }
39         }
40
41         return false
42 }