From 051b7cf4589d14863e0c1d17bc6ba938d6816135 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Mon, 7 Oct 2024 22:46:29 +0200 Subject: [PATCH] clean code --- gno_github_agent/.env | 3 +- gno_github_agent/clientql/client.go | 48 +++++++++++++++++++---------- gno_github_agent/db/db.go | 19 +++++++++--- gno_github_agent/main.go | 5 +-- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/gno_github_agent/.env b/gno_github_agent/.env index a9db2a93fb..4d48a6b1ce 100644 --- a/gno_github_agent/.env +++ b/gno_github_agent/.env @@ -1,4 +1,5 @@ GNO_MNEMONIC=scissors razor beauty delay derive chronic toss burger gravity shallow couch slogan change tray connect frame token slight zone usage sad monkey pyramid change GNO_CHAIN_ID=dev GNO_RPC_ADDR=http://127.0.0.1:26657 -GNO_REALM_PATH=gno.land/r/teritori/ghverify \ No newline at end of file +GNO_REALM_PATH=gno.land/r/teritori/ghverify +GNO_TX_INDEXER=http://localhost:8546/graphql/query \ No newline at end of file diff --git a/gno_github_agent/clientql/client.go b/gno_github_agent/clientql/client.go index 404d9779a3..15d59507da 100644 --- a/gno_github_agent/clientql/client.go +++ b/gno_github_agent/clientql/client.go @@ -2,6 +2,7 @@ package clientql import ( "context" + "errors" "fmt" "io" "net/http" @@ -29,7 +30,7 @@ func New(graphqlEndpoint string, db *gorm.DB, logger *zap.SugaredLogger, gnoSign } func (client *IndexerQL) DealWithVerifications() error { - lastBlock, err := getLastTreatedBlock() + lastBlock, err := client.getLastTreatedBlock() if err != nil { return err } @@ -45,7 +46,8 @@ func (client *IndexerQL) DealWithVerifications() error { switch event := responseEvent.(type) { case *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent: client.logger.Infof("args %v\n", event.Attrs) - err := client.dealWithVerification(event) + + err := client.dealWithVerification(event, validationRequest.Block_height) if err != nil { client.logger.Errorf("failed to deal with verification: %s", err.Error()) continue @@ -60,11 +62,21 @@ func (client *IndexerQL) DealWithVerifications() error { return nil } -func getLastTreatedBlock() (int, error) { - return 0, nil +func (client *IndexerQL) getLastTreatedBlock() (int, error) { + var verification db.Verification + err := client.db.Model(&db.Verification{}).Where("status = ?", string(db.VerificationStatusVerified)).Order("id desc").First(&verification).Error + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return 0, nil + } + + return 0, nil + } + return verification.BlockHeight, err } -func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent) error { +func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent, blockHeight int) error { var handle string var callerAddress string for _, attr := range event.Attrs { @@ -75,6 +87,7 @@ func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationR callerAddress = attr.Value } } + var verification db.Verification err := client.db.Model(&db.Verification{}).Where("handle = ? AND address = ?", handle, callerAddress).Find(&verification).Error if err != nil { @@ -92,17 +105,17 @@ func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationR return err } - client.logger.Infof("Get\n") defer res.Body.Close() if res.StatusCode != 200 { - return client.updateVerification(handle, callerAddress, "config_not_found") + return client.updateVerification(handle, callerAddress, db.VerificationStatusConfigNotFound, blockHeight) } + data, err := io.ReadAll(res.Body) if err != nil { - client.updateVerification(handle, callerAddress, "invalid_data") + client.updateVerification(handle, callerAddress, db.VerificationStatusInvalidData, blockHeight) return err } - client.logger.Infof("config.yml: %s\n", string(data)) + githubConfiguredAddress := strings.TrimSpace(string(data)) if githubConfiguredAddress == callerAddress { err = client.signer.CallVerify(githubConfiguredAddress) @@ -110,18 +123,19 @@ func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationR return err } - return client.updateVerification(handle, callerAddress, "verified") + return client.updateVerification(handle, callerAddress, db.VerificationStatusVerified, blockHeight) } - return client.updateVerification(handle, callerAddress, "caller_address_mismatch") + return client.updateVerification(handle, callerAddress, db.VerificationStatusCallerAddressMismatch, blockHeight) } -func (client *IndexerQL) updateVerification(handle, address, status string) error { +func (client *IndexerQL) updateVerification(handle, address string, status db.VerificationStatus, blockHeight int) error { verification := db.Verification{ - Handle: handle, - Address: address, - Status: status, - CreatedAt: time.Now().Format("2006-01-02 15:04:05"), + Handle: handle, + Address: address, + Status: string(status), + CreatedAt: time.Now().Format("2006-01-02 15:04:05"), + BlockHeight: blockHeight, } - return client.db.Model(&verification).Where("handle = ? AND address = ?", handle, address).Assign(db.Verification{Status: status}).FirstOrCreate(&verification).Error + return client.db.Model(&verification).Where("handle = ? AND address = ?", handle, address).Assign(db.Verification{Status: string(status)}).FirstOrCreate(&verification).Error } diff --git a/gno_github_agent/db/db.go b/gno_github_agent/db/db.go index 1a766e1ce8..6e2a9a51fd 100644 --- a/gno_github_agent/db/db.go +++ b/gno_github_agent/db/db.go @@ -29,8 +29,19 @@ type Verification struct { gorm.Model Id uint `json:"id" gorm:"unique;primaryKey;autoIncrement"` - Handle string - Address string - Status string - CreatedAt string + Handle string + Address string + Status string + CreatedAt string + BlockHeight int } + +type VerificationStatus string + +const ( + VerificationStatusUnverified VerificationStatus = "unverified" + VerificationStatusVerified VerificationStatus = "verified" + VerificationStatusConfigNotFound VerificationStatus = "config_not_found" + VerificationStatusInvalidData VerificationStatus = "invalid_data" + VerificationStatusCallerAddressMismatch VerificationStatus = "caller_address_mismatch" +) diff --git a/gno_github_agent/main.go b/gno_github_agent/main.go index d84e02fbd5..0b1a0691b0 100644 --- a/gno_github_agent/main.go +++ b/gno_github_agent/main.go @@ -34,16 +34,17 @@ func main() { chainID := os.Getenv("GNO_CHAIN_ID") rpcAddr := os.Getenv("GNO_RPC_ADDR") realmPath := os.Getenv("GNO_REALM_PATH") + txIndexerHost := os.Getenv("GNO_TX_INDEXER") gnoSigner = signer.New(db, logger.Sugar(), mnemonic, chainID, rpcAddr, realmPath) - clientql := clientql.New("http://localhost:8546/graphql/query", db, logger.Sugar(), gnoSigner) + clientql := clientql.New(txIndexerHost, db, logger.Sugar(), gnoSigner) schedule := gocron.NewScheduler(time.UTC) schedule.Every(30).Seconds().Do(func() { err = clientql.DealWithVerifications() if err != nil { - logger.Error("failed to get names list", zap.Error(err)) + logger.Error("failed to deal with verifications", zap.Error(err)) panic(err) } })