@@ -3,6 +3,7 @@ package contracts
3
3
import (
4
4
"context"
5
5
"math/big"
6
+ "sync"
6
7
7
8
"github.com/omni-network/omni/e2e/app/eoa"
8
9
"github.com/omni-network/omni/lib/create3"
@@ -94,17 +95,29 @@ type Salts struct {
94
95
Token string
95
96
}
96
97
98
+ type cache [T any ] struct {
99
+ mu sync.Mutex
100
+ cache T
101
+ }
102
+
97
103
var (
98
104
// cached addresses by network.
99
- addrsCache = map [netconf.ID ]Addresses {}
105
+ addrsCache = cache [map [netconf.ID ]Addresses ]{
106
+ cache : map [netconf.ID ]Addresses {},
107
+ }
100
108
101
109
// cached salts by network.
102
- saltsCache = map [netconf.ID ]Salts {}
110
+ saltsCache = cache [map [netconf.ID ]Salts ]{
111
+ cache : map [netconf.ID ]Salts {},
112
+ }
103
113
)
104
114
105
115
// GetAddresses returns the contract addresses for the given network.
106
116
func GetAddresses (ctx context.Context , network netconf.ID ) (Addresses , error ) {
107
- addrs , ok := addrsCache [network ]
117
+ addrsCache .mu .Lock ()
118
+ defer addrsCache .mu .Unlock ()
119
+
120
+ addrs , ok := addrsCache .cache [network ]
108
121
if ok {
109
122
return addrs , nil
110
123
}
@@ -124,14 +137,17 @@ func GetAddresses(ctx context.Context, network netconf.ID) (Addresses, error) {
124
137
GasStation : gasStation (network , ver ),
125
138
}
126
139
127
- addrsCache [network ] = addrs
140
+ addrsCache . cache [network ] = addrs
128
141
129
142
return addrs , nil
130
143
}
131
144
132
145
// GetSalts returns the contract salts for the given network.
133
146
func GetSalts (ctx context.Context , network netconf.ID ) (Salts , error ) {
134
- salts , ok := saltsCache [network ]
147
+ saltsCache .mu .Lock ()
148
+ defer saltsCache .mu .Unlock ()
149
+
150
+ salts , ok := saltsCache .cache [network ]
135
151
if ok {
136
152
return salts , nil
137
153
}
@@ -150,7 +166,7 @@ func GetSalts(ctx context.Context, network netconf.ID) (Salts, error) {
150
166
GasStation : gasStationSalt (network , ver ),
151
167
}
152
168
153
- saltsCache [network ] = salts
169
+ saltsCache . cache [network ] = salts
154
170
155
171
return salts , nil
156
172
}
0 commit comments