@@ -2,6 +2,7 @@ package checker
2
2
3
3
import (
4
4
"database/sql"
5
+ "math/big"
5
6
"strings"
6
7
7
8
"github.com/ethereum/go-ethereum/accounts/abi"
@@ -18,20 +19,16 @@ func (ch *checker) processEvents(event contracts.ProposalEvent) error {
18
19
processedEvent .LogIndex = event .LogIndex ()
19
20
processedEvent .TransactionHash = event .TxHash ().Bytes ()
20
21
if err := ch .insertProcessedEventLog (processedEvent ); err != nil {
21
- ch .log .WithField ("Error" , err ).Debug ("failed insert processed event" )
22
- return err
22
+ return errors .Wrap (err , "failed insert processed event" )
23
23
}
24
24
votingInfo , err := ch .checkVoteAndGetBalance (event .ProposalID ())
25
25
if err != nil {
26
- ch .log .WithField ("Error" , err ).Errorf ("failed insert processed event" )
27
-
28
- return err
26
+ return errors .Wrap (err , "failed get voting info" )
29
27
}
30
- votingInfo .Balance += event .FundAmountU64 ( )
28
+ votingInfo .Balance = new (big. Int ). Add ( votingInfo . Balance , event .FundAmount () )
31
29
32
30
if err := ch .checkerQ .UpdateVotingInfo (votingInfo ); err != nil {
33
- ch .log .Errorf ("Failed Update voting balance: %v" , err )
34
- return err
31
+ return errors .Wrap (err , "failed Update voting balance" )
35
32
}
36
33
37
34
return nil
@@ -47,8 +44,7 @@ func (ch *checker) processLog(vLog types.Log, eventName string) error {
47
44
48
45
err := ch .insertProcessedEventLog (processedEvent )
49
46
if err != nil {
50
- ch .log .WithFields (logan.F {"Error" : err , "eventName" : eventName }).Error ("failed insert processed event" )
51
- return err
47
+ return errors .Wrap (err , "failed insert processed event" )
52
48
}
53
49
54
50
votingId , value , err := ch .getTransferEvent (eventName , vLog )
@@ -58,52 +54,48 @@ func (ch *checker) processLog(vLog types.Log, eventName string) error {
58
54
59
55
votingInfo , err := ch .checkVoteAndGetBalance (votingId )
60
56
if err != nil {
61
- ch .log .WithFields (logan.F {"Error" : err , "eventName" : eventName , "Voting ID" : votingId }).Error ("failed get voting info" )
62
- return err
57
+ return errors .Wrap (err , "failed get voting info" , logan.F {"Voting ID" : votingId })
63
58
}
64
- votingInfo .Balance = votingInfo .Balance + value
59
+ votingInfo .Balance = new (big. Int ). Add ( votingInfo .Balance , value )
65
60
66
61
err = ch .checkerQ .UpdateVotingInfo (votingInfo )
67
62
if err != nil {
68
- ch .log .WithFields (logan.F {"Error" : err , "eventName" : eventName , "Voting ID" : votingId }).Error ("Failed Update voting balance" )
69
- return err
63
+ return errors .Wrap (err , "failed update voting balance" , logan.F {"Voting ID" : votingId })
70
64
}
71
65
return nil
72
66
}
73
67
74
- func (ch * checker ) getTransferEvent (eventName string , vLog types.Log ) (votingId int64 , value uint64 , err error ) {
68
+ func (ch * checker ) getTransferEvent (eventName string , vLog types.Log ) (votingId int64 , value * big. Int , err error ) {
75
69
parsedABI , err := abi .JSON (strings .NewReader (contracts .ProposalsStateABI ))
76
70
if err != nil {
77
71
ch .log .Errorf ("Failed to parse contract ABI: %v" , err )
78
- return 0 , 0 , err
72
+ return 0 , nil , err
79
73
}
80
74
81
75
if eventName == "ProposalCreated" {
82
76
var transferEvent contracts.ProposalsStateProposalCreated
83
77
err = parsedABI .UnpackIntoInterface (& transferEvent , eventName , vLog .Data )
84
78
if err != nil {
85
79
ch .log .Errorf ("Failed to unpack log data: %v" , err )
86
- return 0 , 0 , err
80
+ return 0 , nil , err
87
81
}
88
- return transferEvent .ProposalId .Int64 (), transferEvent .FundAmount . Uint64 () , nil
82
+ return transferEvent .ProposalId .Int64 (), transferEvent .FundAmount , nil
89
83
}
90
84
91
85
var transferEvent contracts.ProposalsStateProposalFunded
92
86
err = parsedABI .UnpackIntoInterface (& transferEvent , eventName , vLog .Data )
93
87
if err != nil {
94
- ch .log .Errorf ("Failed to unpack log data: %v" , err )
95
- return 0 , 0 , err
88
+ return 0 , nil , errors .Wrap (err , "failed to unpack log data" )
96
89
}
97
- return transferEvent .ProposalId .Int64 (), transferEvent .FundAmount . Uint64 () , nil
90
+ return transferEvent .ProposalId .Int64 (), transferEvent .FundAmount , nil
98
91
}
99
92
100
93
func (ch * checker ) insertProcessedEventLog (processedEvent data.ProcessedEvent ) error {
101
94
isExist , err := ch .checkerQ .CheckProcessedEventExist (processedEvent )
102
95
if isExist {
103
- ch .log .WithField ("event_index_log" , processedEvent .LogIndex ).Debug ("Duplicate event in db" )
104
96
return errors .New ("Duplicate event in db" )
105
97
}
106
- if err != nil && err != sql . ErrNoRows {
98
+ if err != nil {
107
99
return err
108
100
}
109
101
err = ch .checkerQ .InsertProcessedEvent (processedEvent )
@@ -113,36 +105,36 @@ func (ch *checker) insertProcessedEventLog(processedEvent data.ProcessedEvent) e
113
105
return nil
114
106
}
115
107
116
- func (ch * checker ) checkVoteAndGetBalance (votingId int64 ) (data.VotingInfo , error ) {
108
+ func (ch * checker ) checkVoteAndGetBalance (votingId int64 ) (* data.VotingInfo , error ) {
117
109
votingInfo , err := ch .checkerQ .GetVotingInfo (votingId )
118
- if err == sql . ErrNoRows {
119
- newVote := & data. VotingInfo {
120
- VotingId : votingId ,
121
- Balance : 0 ,
122
- GasLimit : ch . VotingV2Config . GasLimit ,
123
- }
110
+ if err != nil {
111
+ return nil , err
112
+ }
113
+ if votingInfo != nil {
114
+ return votingInfo , nil
115
+ }
124
116
125
- err := ch . checkerQ . InsertVotingInfo ( * newVote )
126
- if err != nil {
127
- ch . log . Errorf ( "Failed insert new voting info: %v" , err )
128
- return * newVote , err
129
- }
130
- return * newVote , nil
131
- } else if err != nil {
132
- ch . log . Errorf ( "Error get voting: %v" , err )
133
- return data. VotingInfo {}, err
117
+ votingInfo = & data. VotingInfo {
118
+ VotingId : votingId ,
119
+ Balance : big . NewInt ( 0 ),
120
+ GasLimit : ch . VotingV2Config . GasLimit ,
121
+ }
122
+
123
+ err = ch . checkerQ . InsertVotingInfo ( votingInfo )
124
+ if err != nil {
125
+ return votingInfo , errors . Wrap ( err , "failed insert new voting info" )
134
126
}
135
- return votingInfo , err
127
+
128
+ return votingInfo , nil
136
129
}
137
130
138
131
func (ch * checker ) getStartBlockNumber () (uint64 , error ) {
139
132
block , err := ch .checkerQ .GetLastBlock ()
140
- if err == sql .ErrNoRows {
133
+ if err != nil {
134
+ if err != sql .ErrNoRows {
135
+ return 0 , errors .Wrap (err , "failed get block from db" )
136
+ }
141
137
block = ch .VotingV2Config .Block
142
- } else if err != nil {
143
- ch .log .Errorf ("Failed get block from db: %v" , err )
144
- return ch .VotingV2Config .Block , err
145
138
}
146
-
147
139
return block , nil
148
140
}
0 commit comments