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 051b7cf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 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
48 changes: 31 additions & 17 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 Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -92,36 +105,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"
)
5 changes: 3 additions & 2 deletions gno_github_agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down

0 comments on commit 051b7cf

Please sign in to comment.