Skip to content

Commit bfff46e

Browse files
authored
Merge pull request #12 from dan13ram/dev
incorporate audit findings
2 parents 5467e3e + e979cba commit bfff46e

22 files changed

+988
-206
lines changed

Dockerfile

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.19 as base
1+
FROM golang:1.21 as base
22

33
WORKDIR /app
44

@@ -14,7 +14,7 @@ COPY main.go ./
1414
COPY defaults.yml ./
1515

1616
# build
17-
RUN CGO_ENABLED=0 GOOS=linux go build -o /validator
17+
RUN CGO_ENABLED=0 GOOS=linux go build -o /bin/validator
1818

1919
# set environment variables
2020
# mongodb
@@ -80,5 +80,17 @@ ENV HEALTH_CHECK_INTERVAL_SECS ${HEALTH_CHECK_INTERVAL_SECS}
8080
# logging
8181
ENV LOG_LEVEL ${LOG_LEVEL}
8282

83+
# create app user
84+
RUN adduser --group --system app
85+
86+
RUN chown -R app:app /bin/validator
87+
88+
RUN chmod +x /bin/validator
89+
90+
RUN chown -R app:app /app
91+
92+
# switch to app user
93+
USER app
94+
8395
# run
84-
CMD ["/validator", "--config", "/app/defaults.yml"]
96+
CMD ["/bin/validator", "--config", "/app/defaults.yml"]

app/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,22 @@ func validateConfig() {
100100
}
101101

102102
// services
103-
if Config.MintMonitor.Enabled == true && Config.MintMonitor.IntervalMillis == 0 {
103+
if Config.MintMonitor.Enabled && Config.MintMonitor.IntervalMillis == 0 {
104104
log.Fatal("[CONFIG] MintMonitor.Interval is required")
105105
}
106-
if Config.MintSigner.Enabled == true && Config.MintSigner.IntervalMillis == 0 {
106+
if Config.MintSigner.Enabled && Config.MintSigner.IntervalMillis == 0 {
107107
log.Fatal("[CONFIG] MintSigner.Interval is required")
108108
}
109-
if Config.MintExecutor.Enabled == true && Config.MintExecutor.IntervalMillis == 0 {
109+
if Config.MintExecutor.Enabled && Config.MintExecutor.IntervalMillis == 0 {
110110
log.Fatal("[CONFIG] MintExecutor.Interval is required")
111111
}
112-
if Config.BurnMonitor.Enabled == true && Config.BurnMonitor.IntervalMillis == 0 {
112+
if Config.BurnMonitor.Enabled && Config.BurnMonitor.IntervalMillis == 0 {
113113
log.Fatal("[CONFIG] BurnMonitor.Interval is required")
114114
}
115-
if Config.BurnSigner.Enabled == true && Config.BurnSigner.IntervalMillis == 0 {
115+
if Config.BurnSigner.Enabled && Config.BurnSigner.IntervalMillis == 0 {
116116
log.Fatal("[CONFIG] BurnSigner.Interval is required")
117117
}
118-
if Config.BurnExecutor.Enabled == true && Config.BurnExecutor.IntervalMillis == 0 {
118+
if Config.BurnExecutor.Enabled && Config.BurnExecutor.IntervalMillis == 0 {
119119
log.Fatal("[CONFIG] BurnExecutor.Interval is required")
120120
}
121121

app/database.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,41 @@ func (d *MongoDatabase) SetupLocker() error {
7070
defer cancel()
7171

7272
locker = lock.NewClient(d.db.Collection("locks"))
73-
locker.CreateIndexes(ctx)
73+
err := locker.CreateIndexes(ctx)
74+
if err != nil {
75+
return err
76+
}
77+
7478
d.locker = locker
7579

7680
log.Info("[DB] Locker setup")
7781
return nil
7882
}
7983

80-
func randomString(n int) string {
84+
func randomString(n int) (string, error) {
8185
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
8286
var bytes = make([]byte, n)
83-
rand.Read(bytes)
87+
_, err := rand.Read(bytes)
88+
if err != nil {
89+
return "", err
90+
}
91+
8492
for i, b := range bytes {
8593
bytes[i] = alphanum[b%byte(len(alphanum))]
8694
}
87-
return string(bytes)
95+
return string(bytes), nil
8896
}
8997

9098
// XLock locks a resource for exclusive access
9199
func (d *MongoDatabase) XLock(resourceId string) (string, error) {
92100
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
93101
defer cancel()
94102

95-
lockId := randomString(32)
96-
err := d.locker.XLock(ctx, resourceId, lockId, lock.LockDetails{
103+
lockId, err := randomString(32)
104+
if err != nil {
105+
return "", err
106+
}
107+
err = d.locker.XLock(ctx, resourceId, lockId, lock.LockDetails{
97108
TTL: 60, // locks expire in 60 seconds
98109
})
99110
return lockId, err
@@ -104,8 +115,11 @@ func (d *MongoDatabase) SLock(resourceId string) (string, error) {
104115
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
105116
defer cancel()
106117

107-
lockId := randomString(32)
108-
err := d.locker.SLock(ctx, resourceId, lockId, lock.LockDetails{
118+
lockId, err := randomString(32)
119+
if err != nil {
120+
return "", err
121+
}
122+
err = d.locker.SLock(ctx, resourceId, lockId, lock.LockDetails{
109123
TTL: 60, // locks expire in 60 seconds
110124
}, -1)
111125
return lockId, err
@@ -242,13 +256,16 @@ func InitDB() {
242256

243257
err := db.Connect()
244258
if err != nil {
245-
log.Fatal(err)
259+
log.Fatal("[DB] Failed to connect to database: ", err)
246260
}
247261
err = db.SetupIndexes()
248262
if err != nil {
249-
log.Fatal(err)
263+
log.Fatal("[DB] Failed to setup indexes: ", err)
250264
}
251265
err = db.SetupLocker()
266+
if err != nil {
267+
log.Fatal("[DB] Failed to setup locker: ", err)
268+
}
252269
log.Info("[DB] Database initialized")
253270

254271
DB = db

e2e/docker-compose.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ services:
33
image: dan13ram/wpokt-validator:latest
44
restart: always
55
volumes:
6-
- ./config/validator/config.validator1.yml:/root/config.yml
7-
command: sh -c "sleep 30 && /validator --config /root/config.yml"
6+
- ./config/validator/config.validator1.yml:/home/app/config.yml
7+
command: sh -c "sleep 30 && /bin/validator --config /home/app/config.yml"
88
environment:
99
LOG_LEVEL: debug
1010

1111
wpokt.validator2:
1212
image: dan13ram/wpokt-validator:latest
1313
restart: always
1414
volumes:
15-
- ./config/validator/config.validator2.yml:/root/config.yml
16-
command: sh -c "sleep 30 && /validator --config /root/config.yml"
15+
- ./config/validator/config.validator2.yml:/home/app/config.yml
16+
command: sh -c "sleep 30 && /bin/validator --config /home/app/config.yml"
1717
environment:
1818
LOG_LEVEL: debug
1919

2020
wpokt.validator3:
2121
image: dan13ram/wpokt-validator:latest
2222
restart: always
2323
volumes:
24-
- ./config/validator/config.validator3.yml:/root/config.yml
25-
command: sh -c "sleep 30 && /validator --config /root/config.yml"
24+
- ./config/validator/config.validator3.yml:/home/app/config.yml
25+
command: sh -c "sleep 30 && /bin/validator --config /home/app/config.yml"
2626
environment:
2727
LOG_LEVEL: debug
2828

eth/client/mint_controller.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package client
2+
3+
import (
4+
"math/big"
5+
6+
"github.com/dan13ram/wpokt-validator/eth/autogen"
7+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
8+
"github.com/ethereum/go-ethereum/common"
9+
)
10+
11+
type DomainData struct {
12+
Fields [1]byte
13+
Name string
14+
Version string
15+
ChainId *big.Int
16+
VerifyingContract common.Address
17+
Salt [32]byte
18+
Extensions []*big.Int
19+
}
20+
21+
type MintControllerContract interface {
22+
ValidatorCount(opts *bind.CallOpts) (*big.Int, error)
23+
Eip712Domain(opts *bind.CallOpts) (DomainData, error)
24+
MaxMintLimit(opts *bind.CallOpts) (*big.Int, error)
25+
}
26+
27+
type MintControllerContractImpl struct {
28+
contract *autogen.MintController
29+
}
30+
31+
func (x *MintControllerContractImpl) ValidatorCount(opts *bind.CallOpts) (*big.Int, error) {
32+
return x.contract.ValidatorCount(opts)
33+
}
34+
35+
func (x *MintControllerContractImpl) Eip712Domain(opts *bind.CallOpts) (DomainData, error) {
36+
return x.contract.Eip712Domain(opts)
37+
}
38+
39+
func (x *MintControllerContractImpl) MaxMintLimit(opts *bind.CallOpts) (*big.Int, error) {
40+
return x.contract.MaxMintLimit(opts)
41+
}
42+
43+
func NewMintControllerContract(contract *autogen.MintController) MintControllerContract {
44+
return &MintControllerContractImpl{contract: contract}
45+
}

0 commit comments

Comments
 (0)