@@ -2,6 +2,7 @@ package clientql
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "io"
78 "net/http"
@@ -29,7 +30,7 @@ func New(graphqlEndpoint string, db *gorm.DB, logger *zap.SugaredLogger, gnoSign
2930}
3031
3132func (client * IndexerQL ) DealWithVerifications () error {
32- lastBlock , err := getLastTreatedBlock ()
33+ lastBlock , err := client . getLastTreatedBlock ()
3334 if err != nil {
3435 return err
3536 }
@@ -45,7 +46,8 @@ func (client *IndexerQL) DealWithVerifications() error {
4546 switch event := responseEvent .(type ) {
4647 case * gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent :
4748 client .logger .Infof ("args %v\n " , event .Attrs )
48- err := client .dealWithVerification (event )
49+
50+ err := client .dealWithVerification (event , validationRequest .Block_height )
4951 if err != nil {
5052 client .logger .Errorf ("failed to deal with verification: %s" , err .Error ())
5153 continue
@@ -60,11 +62,21 @@ func (client *IndexerQL) DealWithVerifications() error {
6062 return nil
6163}
6264
63- func getLastTreatedBlock () (int , error ) {
64- return 0 , nil
65+ func (client * IndexerQL ) getLastTreatedBlock () (int , error ) {
66+ var verification db.Verification
67+ err := client .db .Model (& db.Verification {}).Where ("status = ?" , string (db .VerificationStatusVerified )).Order ("id desc" ).First (& verification ).Error
68+
69+ if err != nil {
70+ if errors .Is (err , gorm .ErrRecordNotFound ) {
71+ return 0 , nil
72+ }
73+
74+ return 0 , nil
75+ }
76+ return verification .BlockHeight , err
6577}
6678
67- func (client * IndexerQL ) dealWithVerification (event * gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent ) error {
79+ func (client * IndexerQL ) dealWithVerification (event * gnoindexerql.GetValidationRequestsTransactionsTransactionResponseEventsGnoEvent , blockHeight int ) error {
6880 var handle string
6981 var callerAddress string
7082 for _ , attr := range event .Attrs {
@@ -75,6 +87,7 @@ func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationR
7587 callerAddress = attr .Value
7688 }
7789 }
90+
7891 var verification db.Verification
7992 err := client .db .Model (& db.Verification {}).Where ("handle = ? AND address = ?" , handle , callerAddress ).Find (& verification ).Error
8093 if err != nil {
@@ -92,36 +105,37 @@ func (client *IndexerQL) dealWithVerification(event *gnoindexerql.GetValidationR
92105 return err
93106 }
94107
95- client .logger .Infof ("Get\n " )
96108 defer res .Body .Close ()
97109 if res .StatusCode != 200 {
98- return client .updateVerification (handle , callerAddress , "config_not_found" )
110+ return client .updateVerification (handle , callerAddress , db . VerificationStatusConfigNotFound , blockHeight )
99111 }
112+
100113 data , err := io .ReadAll (res .Body )
101114 if err != nil {
102- client .updateVerification (handle , callerAddress , "invalid_data" )
115+ client .updateVerification (handle , callerAddress , db . VerificationStatusInvalidData , blockHeight )
103116 return err
104117 }
105- client . logger . Infof ( "config.yml: %s \n " , string ( data ))
118+
106119 githubConfiguredAddress := strings .TrimSpace (string (data ))
107120 if githubConfiguredAddress == callerAddress {
108121 err = client .signer .CallVerify (githubConfiguredAddress )
109122 if err != nil {
110123 return err
111124 }
112125
113- return client .updateVerification (handle , callerAddress , "verified" )
126+ return client .updateVerification (handle , callerAddress , db . VerificationStatusVerified , blockHeight )
114127 }
115- return client .updateVerification (handle , callerAddress , "caller_address_mismatch" )
128+ return client .updateVerification (handle , callerAddress , db . VerificationStatusCallerAddressMismatch , blockHeight )
116129}
117130
118- func (client * IndexerQL ) updateVerification (handle , address , status string ) error {
131+ func (client * IndexerQL ) updateVerification (handle , address string , status db. VerificationStatus , blockHeight int ) error {
119132 verification := db.Verification {
120- Handle : handle ,
121- Address : address ,
122- Status : status ,
123- CreatedAt : time .Now ().Format ("2006-01-02 15:04:05" ),
133+ Handle : handle ,
134+ Address : address ,
135+ Status : string (status ),
136+ CreatedAt : time .Now ().Format ("2006-01-02 15:04:05" ),
137+ BlockHeight : blockHeight ,
124138 }
125139
126- return client .db .Model (& verification ).Where ("handle = ? AND address = ?" , handle , address ).Assign (db.Verification {Status : status }).FirstOrCreate (& verification ).Error
140+ return client .db .Model (& verification ).Where ("handle = ? AND address = ?" , handle , address ).Assign (db.Verification {Status : string ( status ) }).FirstOrCreate (& verification ).Error
127141}
0 commit comments