-
Notifications
You must be signed in to change notification settings - Fork 5
/
filters.go
113 lines (98 loc) · 2.81 KB
/
filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package contracts
import (
"fmt"
"github.com/celer-network/goutils/log"
comtypes "github.com/celer-network/im-executor/sgn-v2/common/types"
"github.com/celer-network/im-executor/sgn-v2/eth"
)
const (
DEFAULT_CONTRACT_GROUP = "default"
)
type ReceiverContract struct {
ChainId uint64 `mapstructure:"chain_id"`
Address string `mapstructure:"address"`
// the payable value to add when calling executeMessage or executeMessageWithTransfer
PayableValue string `mapstructure:"add_payable_value_for_execution"`
// sender verification
AllowSenderGroups []string `mapstructure:"allow_sender_groups"`
}
func (c *ReceiverContract) ContractAddress() *ContractAddress {
return &ContractAddress{
Address: c.Address,
ChainId: c.ChainId,
}
}
func (c *ReceiverContract) GetAddress() eth.Addr {
return eth.Hex2Addr(c.Address)
}
func (c *ReceiverContract) ContractInfo() *comtypes.ContractInfo {
return &comtypes.ContractInfo{
ChainId: c.ChainId,
Address: c.Address,
}
}
type ReceiverContracts []*ReceiverContract
func (c ReceiverContracts) ContractInfoList() []*comtypes.ContractInfo {
infos := []*comtypes.ContractInfo{}
for _, filter := range c {
infos = append(infos, filter.ContractInfo())
}
return infos
}
func (c ReceiverContracts) Get(chid uint64, addr string) (ReceiverContract, bool) {
for _, filter := range c {
if chid == filter.ChainId && eth.Hex2Addr(filter.Address) == eth.Hex2Addr(addr) {
return *filter, true
}
}
return ReceiverContract{}, false
}
func (c ReceiverContracts) GetByChain(chid uint64) ReceiverContracts {
f := ReceiverContracts{}
for _, filter := range c {
if filter.ChainId == chid {
f = append(f, filter)
}
}
return f
}
func (c ReceiverContracts) Addresses(chid uint64) []eth.Addr {
addrs := []eth.Addr{}
for _, filter := range c {
if filter.ChainId == chid {
addrs = append(addrs, filter.GetAddress())
}
}
return addrs
}
func (c ReceiverContracts) AddressHashes(chid uint64) []eth.Hash {
addrs := []eth.Hash{}
for _, filter := range c {
if filter.ChainId == chid {
addrs = append(addrs, filter.GetAddress().Hash())
}
}
return addrs
}
func (c ReceiverContracts) String() string {
str := ""
for _, f := range c {
s := fmt.Sprintf("- chain_id %d address %s payable_value %s allow_sender_groups %q\n",
f.ChainId, f.Address, f.PayableValue, f.AllowSenderGroups)
str += s
}
return str
}
func (c ReceiverContracts) Validate() error {
if len(c) == 0 {
return fmt.Errorf("empty executor contract filter")
}
log.Infoln("executor will submit execution for these contracts:")
for _, f := range c {
if len(f.AllowSenderGroups) == 0 {
log.Warnf("allow_sender_groups not configured for chain %d contract %s, default to submitting txs originated from all senders to this receiver contract", f.ChainId, f.Address)
}
}
log.Infof("contract filters\n%s", c)
return nil
}