Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Oct 7, 2024
1 parent 603966c commit bbf81ac
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 555 deletions.
3 changes: 2 additions & 1 deletion gno_github_agent/.env
Original file line number Diff line number Diff line change
@@ -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
GNO_REALM_PATH=gno.land/r/teritori/ghverify
GNO_TX_INDEXER=http://localhost:8546/graphql/query
63 changes: 39 additions & 24 deletions gno_github_agent/clientql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clientql

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -17,24 +18,25 @@ import (
)

type IndexerQL struct {
gqlClient graphql.Client
db *gorm.DB
logger *zap.SugaredLogger
signer *signer.Signer
gqlClient graphql.Client
db *gorm.DB
logger *zap.SugaredLogger
signer *signer.Signer
verifyRealmPath string
}

func New(graphqlEndpoint string, db *gorm.DB, logger *zap.SugaredLogger, gnoSigner *signer.Signer) *IndexerQL {
func New(graphqlEndpoint string, db *gorm.DB, logger *zap.SugaredLogger, gnoSigner *signer.Signer, verifyRealmPath string) *IndexerQL {
gqlClient := graphql.NewClient(graphqlEndpoint, nil)
return &IndexerQL{gqlClient: gqlClient, db: db, logger: logger, signer: gnoSigner}
return &IndexerQL{gqlClient: gqlClient, db: db, logger: logger, signer: gnoSigner, verifyRealmPath: verifyRealmPath}
}

func (client *IndexerQL) DealWithVerifications() error {
lastBlock, err := getLastTreatedBlock()
lastBlock, err := client.getLastTreatedBlock()
if err != nil {
return err
}

validationRequests, err := gnoindexerql.GetValidationRequests(context.Background(), client.gqlClient, lastBlock)
validationRequests, err := gnoindexerql.GetValidationRequests(context.Background(), client.gqlClient, lastBlock, client.verifyRealmPath)
if err != nil {
return err
}
Expand All @@ -45,7 +47,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
Expand All @@ -60,11 +63,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 {
Expand All @@ -75,6 +88,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 {
Expand All @@ -92,36 +106,37 @@ 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)
if err != nil {
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
}
19 changes: 15 additions & 4 deletions gno_github_agent/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Loading

0 comments on commit bbf81ac

Please sign in to comment.