-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathagents.go
128 lines (105 loc) · 3.47 KB
/
agents.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package config
import (
"fmt"
"strconv"
"strings"
"github.com/forta-network/forta-core-go/protocol"
"github.com/forta-network/forta-core-go/utils"
)
const (
AgentGrpcPort = "50051"
HeartbeatBotID = "0x37ce3df7facea4bd95619655347ffd66f50909d100fa74ae042f122a8b024c29"
)
type AgentConfig struct {
ID string `yaml:"id" json:"id"`
Image string `yaml:"image" json:"image"`
Manifest string `yaml:"manifest" json:"manifest"`
IsLocal bool `yaml:"isLocal" json:"isLocal"`
IsStandalone bool `yaml:"isStandalone" json:"isStandalone"`
StartBlock *uint64 `yaml:"startBlock" json:"startBlock,omitempty"`
StopBlock *uint64 `yaml:"stopBlock" json:"stopBlock,omitempty"`
Owner string `yaml:"owner" json:"owner"`
ProtocolVersion int `yaml:"protocolVersion" json:"protocolVersion"`
ChainID int
ShardConfig *ShardConfig
}
type ShardConfig struct {
ShardID uint `yaml:"shardId" json:"shardId"`
Shards uint `yaml:"shards" json:"shards"`
Target uint `yaml:"target" json:"target"`
ChainID int64 `yaml:"chainId" json:"chainId"`
}
func (ac AgentConfig) ShardID() int32 {
if !ac.IsSharded() {
// default uint value is 0, so we cannot tell between an unset shardID and actual shard 0
// therefore, we return -1 here
return -1
}
return int32(ac.ShardConfig.ShardID)
}
func (ac AgentConfig) ShardDetails() string {
if !ac.IsSharded() {
return ""
}
return fmt.Sprintf("shard=%d", ac.ShardConfig.ShardID)
}
func (ac AgentConfig) Equal(b AgentConfig) bool {
sameID := strings.EqualFold(ac.ID, b.ID)
sameManifest := strings.EqualFold(ac.Manifest, b.Manifest)
if !sameID || !sameManifest {
return false
}
// if both don't have sharding config, then they are the same
if ac.ShardConfig == nil && b.ShardConfig == nil {
return true
}
// if one of them does not have shard config, then they are different
if ac.ShardConfig == nil && b.ShardConfig != nil {
return false
}
if ac.ShardConfig != nil && b.ShardConfig == nil {
return false
}
// if both have shard config, then configs should match
sameShardID := ac.ShardConfig.ShardID == b.ShardConfig.ShardID
sameShardCount := ac.ShardConfig.Shards == b.ShardConfig.Shards
return sameShardID && sameShardCount
}
// IsSharded tells if this is a sharded bot.
func (ac *AgentConfig) IsSharded() bool {
return ac.ShardConfig != nil && ac.ShardConfig.Shards > 1
}
// ToAgentInfo transforms the agent config to the agent info.
func (ac AgentConfig) ToAgentInfo() *protocol.AgentInfo {
return &protocol.AgentInfo{
Id: ac.ID,
Image: ac.Image,
ImageHash: ac.ImageHash(),
Manifest: ac.Manifest,
}
}
func (ac AgentConfig) ImageHash() string {
_, digest := utils.SplitImageRef(ac.Image)
return digest
}
func (ac AgentConfig) ContainerName() string {
if ac.IsStandalone {
// the container is already running - don't mess with the name
return ac.ID
}
if ac.IsLocal {
return fmt.Sprintf("%s-agent-%s", ContainerNamePrefix, utils.ShortenString(ac.ID, 8))
}
_, digest := utils.SplitImageRef(ac.Image)
parts := []string{ContainerNamePrefix, "agent", utils.ShortenString(ac.ID, 8), utils.ShortenString(digest, 4)}
if ac.ProtocolVersion >= 2 {
parts = append(parts, "c"+strconv.Itoa(ac.ChainID)) // append the chain id
}
if ac.IsSharded() {
parts = append(parts, "s"+strconv.Itoa(int(ac.ShardConfig.ShardID))) // append the shard id at the end
}
return strings.Join(parts, "-")
}
func (ac AgentConfig) GrpcPort() string {
return AgentGrpcPort
}