-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
228 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# SPDX-License-Identifier: ice License 1.0 | ||
|
||
FROM golang:latest AS build | ||
ARG SERVICE_NAME | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
|
||
WORKDIR /app/ | ||
COPY . /app/ | ||
|
||
ENV CGO_ENABLED=0 | ||
ENV GOOS=$TARGETOS | ||
ENV GOARCH=$TARGETARCH | ||
|
||
RUN env SERVICE_NAME=$SERVICE_NAME make dockerfile | ||
RUN cp cmd/$SERVICE_NAME/bin bin | ||
|
||
FROM gcr.io/distroless/base-debian11:latest | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
ARG PORT=443 | ||
LABEL os=$TARGETOS | ||
LABEL arch=$TARGETARCH | ||
COPY --from=build /app/bin app | ||
#You might need to expose more ports. Just add more separated by space | ||
#I.E. EXPOSE 8080 8081 8082 8083 | ||
EXPOSE $PORT | ||
ENTRYPOINT ["/app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: ice License 1.0 | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
|
||
coindistribution "github.com/ice-blockchain/freezer/coin-distribution" | ||
appCfg "github.com/ice-blockchain/wintr/config" | ||
"github.com/ice-blockchain/wintr/log" | ||
"github.com/ice-blockchain/wintr/server" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
const pkgName = "cmd/freezer-coin-distributer" | ||
|
||
var cfg struct{ Version string } | ||
appCfg.MustLoadFromKey(pkgName, &cfg) | ||
|
||
log.Info(fmt.Sprintf("starting version `%v`...", cfg.Version)) | ||
|
||
server.New(new(service), pkgName, "").ListenAndServe(ctx, cancel) | ||
} | ||
|
||
type ( | ||
// | service implements server.State and is responsible for managing the state and lifecycle of the package. | ||
service struct{ coinDistributer coindistribution.Client } | ||
) | ||
|
||
func (s *service) RegisterRoutes(_ *server.Router) {} | ||
|
||
func (s *service) Init(ctx context.Context, cancel context.CancelFunc) { | ||
s.coinDistributer = coindistribution.MustStartCoinDistribution(ctx, cancel) | ||
} | ||
|
||
func (s *service) Close(_ context.Context) error { | ||
return errors.Wrap(s.coinDistributer.Close(), "could not close service") | ||
} | ||
|
||
func (s *service) CheckHealth(ctx context.Context) error { | ||
log.Debug("checking health...", "package", "coin-distribution") | ||
|
||
return errors.Wrap(s.coinDistributer.CheckHealth(ctx), "failed to check coin distributer's health") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# SPDX-License-Identifier: ice License 1.0 | ||
|
||
development: true | ||
logger: | ||
encoder: console | ||
level: debug | ||
coin-distribution: | ||
development: true | ||
workers: 10 | ||
batchSize: 100 | ||
wintr/connectors/storage/v2: | ||
runDDL: true | ||
primaryURL: postgresql://root:pass@localhost:5433/freezer | ||
credentials: | ||
user: root | ||
password: pass | ||
replicaURLs: | ||
- postgresql://root:pass@localhost:5433/freezer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- SPDX-License-Identifier: ice License 1.0 | ||
CREATE TABLE IF NOT EXISTS pending_coin_distributions ( | ||
created_at timestamp NOT NULL, | ||
internal_id bigint NOT NULL, | ||
iceflakes bigint NOT NULL, | ||
user_id text NOT NULL PRIMARY KEY, | ||
eth_address text NOT NULL); | ||
|
||
CREATE INDEX IF NOT EXISTS pending_coin_distributions_worker_number_ix ON pending_coin_distributions ((internal_id % 10), created_at ASC); | ||
|
||
--- Flow: | ||
--infinite loop: -- with 30 sec sleep between iterations if 0 rows returned | ||
--do in transaction: | ||
--1. SELECT * | ||
-- FROM pending_coin_distributions | ||
-- WHERE internal_id % 10 = $1 | ||
-- ORDER BY created_at ASC | ||
-- LIMIT $2 | ||
-- FOR UPDATE | ||
--2. call ERC-20 smart contract method to airdrop coins | ||
--3. delete from pending_coin_distributions WHERE user_id = ANY($1) | ||
-- if transaction fails you retry infinitely | ||
-- log.info every successful transaction, log every error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: ice License 1.0 | ||
|
||
package coindistribution | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/pkg/errors" | ||
|
||
appCfg "github.com/ice-blockchain/wintr/config" | ||
"github.com/ice-blockchain/wintr/connectors/storage/v2" | ||
) | ||
|
||
func init() { | ||
appCfg.MustLoadFromKey(applicationYamlKey, &cfg) | ||
} | ||
|
||
func MustStartCoinDistribution(ctx context.Context, cancel context.CancelFunc) Client { | ||
cd := &coinDistributer{ | ||
db: storage.MustConnect(context.Background(), ddl, applicationYamlKey), | ||
wg: new(sync.WaitGroup), | ||
} | ||
cd.wg.Add(int(cfg.Workers)) | ||
cd.cancel = cancel | ||
|
||
for workerNumber := int64(0); workerNumber < cfg.Workers; workerNumber++ { | ||
go func(wn int64) { | ||
defer cd.wg.Done() | ||
cd.distributeCoins(ctx, wn) | ||
}(workerNumber) | ||
} | ||
|
||
return cd | ||
} | ||
|
||
func (cd *coinDistributer) Close() error { | ||
cd.cancel() | ||
cd.wg.Wait() | ||
|
||
return multierror.Append( | ||
errors.Wrap(cd.db.Close(), "failed to close db"), | ||
).ErrorOrNil() | ||
} | ||
|
||
func (cd *coinDistributer) CheckHealth(ctx context.Context) error { | ||
return errors.Wrap(cd.db.Ping(ctx), "[health-check] failed to ping DB") | ||
} | ||
|
||
func (cd *coinDistributer) distributeCoins(ctx context.Context, workerNumber int64) { | ||
for ctx.Err() == nil { | ||
println(workerNumber) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: ice License 1.0 | ||
|
||
package coindistribution | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"io" | ||
"sync" | ||
|
||
"github.com/ice-blockchain/wintr/connectors/storage/v2" | ||
) | ||
|
||
// Public API. | ||
|
||
type ( | ||
Client interface { | ||
io.Closer | ||
CheckHealth(context.Context) error | ||
} | ||
) | ||
|
||
// Private API. | ||
|
||
const ( | ||
applicationYamlKey = "coin-distribution" | ||
) | ||
|
||
// . | ||
var ( | ||
//nolint:gochecknoglobals // Singleton & global config mounted only during bootstrap. | ||
cfg config | ||
//go:embed DDL.sql | ||
ddl string | ||
) | ||
|
||
type ( | ||
coinDistributer struct { | ||
db *storage.DB | ||
cancel context.CancelFunc | ||
wg *sync.WaitGroup | ||
} | ||
config struct { | ||
Workers int64 `yaml:"workers"` | ||
BatchSize int64 `yaml:"batchSize"` | ||
Development bool `yaml:"development"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters