generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRD-637 Tradelogs v2: Backfill Flow (#78)
* TRD-637 tradelogs v2: backfill flow * TRD-637 update unit test * TRD-637 update unit test * update release flow * TRD-637 refactor backfill flow * TRD-637 remove price filler * TRD-637 update backfill table
- Loading branch information
1 parent
7b0a323
commit 7627ccf
Showing
21 changed files
with
671 additions
and
58 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
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
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,27 @@ | ||
## BUILDER | ||
FROM golang:1.22-bullseye as builder | ||
|
||
WORKDIR /src | ||
|
||
COPY . . | ||
|
||
RUN go build -o parse_log ./v2/cmd/parse_log | ||
RUN go build -o backfill ./v2/cmd/backfill | ||
|
||
|
||
## DEPLOY | ||
FROM debian:bullseye | ||
|
||
RUN apt-get update && \ | ||
apt install -y ca-certificates && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
|
||
WORKDIR /v2 | ||
|
||
COPY --from=builder /src/parse_log /v2/parse_log | ||
COPY --from=builder /src/backfill /v2/backfill | ||
|
||
COPY v2/cmd/migrations /v2/migrations | ||
|
||
CMD /v2/parse_log |
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,150 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/KyberNetwork/tradelogs/v2/internal/server" | ||
"github.com/KyberNetwork/tradelogs/v2/internal/worker" | ||
libapp "github.com/KyberNetwork/tradelogs/v2/pkg/app" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/handler" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/kafka" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/parser" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/parser/zxotc" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/rpcnode" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/storage/backfill" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/storage/state" | ||
"github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs" | ||
storageTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/types" | ||
zxotcStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/zxotc" | ||
"github.com/KyberNetwork/tradinglib/pkg/dbutil" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/urfave/cli" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
app := libapp.NewApp() | ||
app.Name = "trade log backfill service" | ||
app.Action = run | ||
|
||
app.Flags = append(app.Flags, libapp.RPCNodeFlags()...) | ||
app.Flags = append(app.Flags, libapp.PostgresSQLFlags("tradelogs_v2")...) | ||
app.Flags = append(app.Flags, libapp.KafkaFlag()...) | ||
app.Flags = append(app.Flags, libapp.HTTPServerFlags()...) | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
|
||
func run(c *cli.Context) error { | ||
logger, _, flush, err := libapp.NewLogger(c) | ||
if err != nil { | ||
return fmt.Errorf("new logger: %w", err) | ||
} | ||
|
||
defer flush() | ||
|
||
zap.ReplaceGlobals(logger) | ||
l := logger.Sugar() | ||
l.Infow("Starting backfill service") | ||
|
||
db, err := initDB(c) | ||
if err != nil { | ||
return fmt.Errorf("cannot init DB: %w", err) | ||
} | ||
|
||
// trade log manager | ||
storages := []storageTypes.Storage{ | ||
zxotcStorage.New(l, db), | ||
} | ||
manager := tradelogs.NewManager(l, storages) | ||
|
||
// backfill storage | ||
backfillStorage := backfill.New(l, db) | ||
|
||
// state storage | ||
stateStorage := state.New(l, db) | ||
|
||
// rpc node to query trace call | ||
rpcURL := c.StringSlice(libapp.RPCUrlFlagName) | ||
if len(rpcURL) == 0 { | ||
return fmt.Errorf("rpc url is empty") | ||
} | ||
|
||
ethClients := make([]*ethclient.Client, len(rpcURL)) | ||
for i, url := range rpcURL { | ||
client, err := ethclient.Dial(url) | ||
if err != nil { | ||
return fmt.Errorf("cannot dial eth client: %w", err) | ||
} | ||
ethClients[i] = client | ||
} | ||
rpcNode := rpcnode.NewClient(l, ethClients...) | ||
|
||
parsers := []parser.Parser{ | ||
//kyberswap.MustNewParser(), | ||
zxotc.MustNewParser(), | ||
//paraswap.MustNewParser(), | ||
//kyberswaprfq.MustNewParser(), | ||
//hashflowv3.MustNewParser(), | ||
//oneinchv6.MustNewParser(traceCalls), | ||
//uniswapxv1.MustNewParser(traceCalls), | ||
//uniswapx.MustNewParser(traceCalls), | ||
//bebop.MustNewParser(traceCalls), | ||
//zxrfqv3.MustNewParserWithDeployer(traceCalls, ethClient, common.HexToAddress(parser.Deployer0xV3)), | ||
} | ||
|
||
// kafka broadcast topic | ||
broadcastTopic := c.String(libapp.KafkaBroadcastTopic.Name) | ||
err = kafka.ValidateTopicName(broadcastTopic) | ||
if err != nil { | ||
return fmt.Errorf("invalid kafka topic: %w", err) | ||
} | ||
|
||
// kafka publisher for broadcasting trade logs | ||
kafkaPublisher, err := kafka.NewPublisher(libapp.KafkaConfigFromFlags(c)) | ||
if err != nil { | ||
return fmt.Errorf("cannot create kafka publisher: %w", err) | ||
} | ||
|
||
// trade log handler | ||
tradeLogHandler := handler.NewTradeLogHandler(l, rpcNode, manager, parsers, broadcastTopic, kafkaPublisher) | ||
|
||
// parse log worker | ||
w := worker.NewBackFiller(tradeLogHandler, backfillStorage, stateStorage, l, rpcNode, parsers) | ||
|
||
go func() { | ||
if err = w.Run(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
s := server.NewBackfill(l, c.String(libapp.HTTPBackfillServerFlag.Name), w) | ||
|
||
return s.Run() | ||
} | ||
|
||
func initDB(c *cli.Context) (*sqlx.DB, error) { | ||
db, err := libapp.NewDB(map[string]interface{}{ | ||
"host": c.String(libapp.PostgresHost.Name), | ||
"port": c.Int(libapp.PostgresPort.Name), | ||
"user": c.String(libapp.PostgresUser.Name), | ||
"password": c.String(libapp.PostgresPassword.Name), | ||
"dbname": c.String(libapp.PostgresDatabase.Name), | ||
"sslmode": "disable", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = dbutil.RunMigrationUp(db.DB, c.String(libapp.PostgresMigrationPath.Name), | ||
c.String(libapp.PostgresDatabase.Name)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} |
File renamed without changes.
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,8 @@ | ||
create table backfill ( | ||
exchange text not null primary key, | ||
deploy_block bigint not null, | ||
backfilled_block bigint not null default 0 | ||
); | ||
|
||
insert into backfill (exchange, deploy_block) | ||
values ('zerox', 20926953) |
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
Oops, something went wrong.