Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Sep 23, 2024
1 parent 23935f5 commit 32a06db
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ faucet-docker:
docker build --build-arg "GIT_DESCRIBE=$(GIT_DESCRIBE)" --build-arg "GIT_COMMIT=$(GIT_COMMIT)" -t "$(IMAGE)/faucet" -f cmd/accumulated-faucet/Dockerfile .

faucet-docker-push: faucet-docker
docker push "$(IMAGE)/faucet"
docker push "$(IMAGE)/faucet"

sim:
go build -trimpath $(FLAGS) ./tools/cmd/simulator

sim-docker:
docker build --build-arg "GIT_DESCRIBE=$(GIT_DESCRIBE)" --build-arg "GIT_COMMIT=$(GIT_COMMIT)" -t "$(IMAGE)/simulator" -f tools/cmd/simulator/Dockerfile .

sim-docker-push: sim-docker
docker push "$(IMAGE)/simulator"
25 changes: 25 additions & 0 deletions tools/cmd/simulator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:1.23 as build

ARG GIT_DESCRIBE
ARG GIT_COMMIT

# Build
WORKDIR /root
COPY . .
ENV CGO_ENABLED 0
RUN make sim GIT_DESCRIBE=$GIT_DESCRIBE GIT_COMMIT=$GIT_COMMIT
RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN mkdir /data

FROM alpine:3

# Install tools
RUN apk add --no-cache bash jq curl nano

# Copy binaries
COPY --from=build /root/simulator /go/bin/dlv /bin/

EXPOSE 26660

ENTRYPOINT ["simulator", "-b=1", "-v=1", "-s=100ms", "--database=/data/accumulate.db", "--globals={\"executorVersion\": \"v2vandenberg\"}"]
CMD ["-a=0.0.0.0"]
60 changes: 31 additions & 29 deletions tools/cmd/simulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var flag = struct {
LogFormat string
Step string
Globals string
Localhost bool
BaseAddr string
BvnCount int
ValCount int
BasePort int
Expand All @@ -65,7 +65,7 @@ func init() {
cmd.Flags().IntVarP(&flag.BvnCount, "bvns", "b", 3, "Number of BVNs to create; applicable only when --network=simple")
cmd.Flags().IntVarP(&flag.ValCount, "validators", "v", 3, "Number of validators to create per BVN; applicable only when --network=simple")
cmd.Flags().IntVarP(&flag.BasePort, "port", "p", 26656, "Base port to listen on")
cmd.Flags().BoolVarP(&flag.Localhost, "localhost", "l", false, "Base loopback URL is 'localhost'")
cmd.Flags().StringVarP(&flag.BaseAddr, "address", "a", "127.0.1.1", "Base address to listen on")
cmd.Flags().StringSliceVarP(&flag.Cors, "cors", "c", []string{"*"}, "Specify url's for CORS requests, (default=*)")

cmd.MarkFlagsMutuallyExclusive("snapshot", "globals")
Expand Down Expand Up @@ -113,13 +113,15 @@ func nextIP(ip net.IP, addToIP int) net.IP {
}

func run(*cobra.Command, []string) {
localhost := net.ParseIP("127.0.1.1")
if flag.Localhost {
var baseAddr net.IP
if flag.BaseAddr == "localhost" {
ips := findLoopback()
if len(ips) == 0 {
log.Fatal("No IP addresses found")
}
localhost = ips[0]
baseAddr = ips[0]
} else {
baseAddr = net.ParseIP(flag.BaseAddr)
}

jsonrpc2.DebugMethodFunc = true
Expand All @@ -134,7 +136,7 @@ func run(*cobra.Command, []string) {
net = simulator.NewSimpleNetwork("Simulator", flag.BvnCount, flag.ValCount)
for i, bvn := range net.Bvns {
for j, node := range bvn.Nodes {
node.AdvertizeAddress = nextIP(localhost, i*flag.ValCount+j).String()
node.AdvertizeAddress = nextIP(baseAddr, i*flag.ValCount+j).String()
node.BasePort = uint64(flag.BasePort)
}
}
Expand Down Expand Up @@ -178,18 +180,8 @@ func run(*cobra.Command, []string) {
ListenHTTPv3: true,
ServeError: check,
HookHTTP: func(h http.Handler, w http.ResponseWriter, r *http.Request) {

if flag.Cors != nil {
cors := strings.Join(flag.Cors, ",")
w.Header().Set("Access-Control-Allow-Origin", cors) // set "*" for testing, but use specific origin in production
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

// Handle preflight OPTIONS request
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
if handleCORS(w, r) {
return
}
onWaitHook(sim, h, w, r)
},
Expand All @@ -213,18 +205,10 @@ func run(*cobra.Command, []string) {
ListenHTTPv3: true,
ServeError: check,
HookHTTP: func(h http.Handler, w http.ResponseWriter, r *http.Request) {
if flag.Cors != nil {
cors := strings.Join(flag.Cors, ",")
w.Header().Set("Access-Control-Allow-Origin", cors) // set "*" for testing, but use specific origin in production
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

// Handle preflight OPTIONS request
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
if handleCORS(w, r) {
return
}
h.ServeHTTP(w, r)
},
}))

Expand Down Expand Up @@ -327,3 +311,21 @@ func waitForTxID(sim *simulator.Simulator, txid *url.TxID, ignorePending bool) {
}
}
}

func handleCORS(w http.ResponseWriter, r *http.Request) bool {
if flag.Cors == nil {
return false
}

cors := strings.Join(flag.Cors, ",")
w.Header().Set("Access-Control-Allow-Origin", cors) // set "*" for testing, but use specific origin in production
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

// Handle preflight OPTIONS request
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return true
}
return false
}

0 comments on commit 32a06db

Please sign in to comment.