@@ -2,7 +2,10 @@ package cmd
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
6
+ "github.com/ethereum/go-ethereum"
5
7
"github.com/ethereum/go-ethereum/accounts/abi/bind"
8
+ "github.com/ethereum/go-ethereum/common"
6
9
types2 "github.com/ethereum/go-ethereum/core/types"
7
10
"github.com/ethereum/go-ethereum/ethclient"
8
11
"math/big"
@@ -11,13 +14,18 @@ import (
11
14
"razor/core/types"
12
15
"razor/pkg/bindings"
13
16
"razor/utils"
17
+ "strings"
14
18
)
15
19
16
- var giveSortedLeafIds []int
20
+ var (
21
+ giveSortedLeafIds []int
22
+ disputedFlag bool
23
+ )
17
24
18
25
//blockId is id of the block
19
26
20
27
func (* UtilsStruct ) HandleDispute (client * ethclient.Client , config types.Configurations , account types.Account , epoch uint32 , blockNumber * big.Int , rogueData types.Rogue ) error {
28
+ disputedFlag = false
21
29
22
30
sortedProposedBlockIds , err := razorUtils .GetSortedProposedBlockIds (client , epoch )
23
31
if err != nil {
@@ -77,6 +85,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
77
85
log .Info ("Txn Hash: " , transactionUtils .Hash (disputeBiggestStakeProposedTxn ))
78
86
status := razorUtils .WaitForBlockCompletion (client , transactionUtils .Hash (disputeBiggestStakeProposedTxn ).String ())
79
87
if status == 1 {
88
+ disputedFlag = true
80
89
continue
81
90
}
82
91
}
@@ -93,6 +102,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
93
102
log .Debugf ("Txn Hash: %s" , transactionUtils .Hash (idDisputeTxn ).String ())
94
103
status := razorUtils .WaitForBlockCompletion (client , transactionUtils .Hash (idDisputeTxn ).String ())
95
104
if status == 1 {
105
+ disputedFlag = true
96
106
continue
97
107
}
98
108
}
@@ -108,7 +118,12 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
108
118
// ids [1, 2, 3, 4]
109
119
// Sorted revealed values would be the vote values for the wrong median, here 230
110
120
collectionIdOfWrongMedian := proposedBlock .Ids [mismatchIndex ]
111
- sortedValues := revealedDataMaps .SortedRevealedValues [collectionIdOfWrongMedian ]
121
+
122
+ //collectionId starts from 1 and in SortedRevealedValues, the keys start from 0 which are collectionId-1 mapping to respective revealed data for that collectionId.
123
+ //e.g. collectionId = [1,2,3,4] & Sorted Reveal Votes: map[0:[100] 1:[200 202] 2:[300]]
124
+ //Here 0th key in map represents collectionId 1.
125
+
126
+ sortedValues := revealedDataMaps .SortedRevealedValues [collectionIdOfWrongMedian - 1 ]
112
127
leafId , err := utils .UtilsInterface .GetLeafIdOfACollection (client , collectionIdOfWrongMedian )
113
128
if err != nil {
114
129
log .Error ("Error in leaf id: " , err )
@@ -133,26 +148,27 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
133
148
}
134
149
135
150
func (* UtilsStruct ) GetLocalMediansData (client * ethclient.Client , account types.Account , epoch uint32 , blockNumber * big.Int , rogueData types.Rogue ) ([]uint32 , []uint16 , * types.RevealedDataMaps , error ) {
136
- //TODO: Implement proper file reading and writing
137
151
138
- // if _mediansData == nil && !rogueData.IsRogue {
139
- // fileName, err := cmdUtils.GetMedianDataFileName(account.Address)
140
- // if err != nil {
141
- // log.Error("Error in getting file name to read median data: ", err)
142
- // goto CalculateMedian
143
- // }
144
- // epochInFile, medianDataFromFile, err := razorUtils.ReadDataFromFile(fileName)
145
- // if err != nil {
146
- // log.Errorf("Error in getting median data from file %s: %t", fileName, err)
147
- // goto CalculateMedian
148
- // }
149
- // if epochInFile != epoch {
150
- // log.Errorf("File %s doesn't contain latest median data: %t", fileName, err)
151
- // goto CalculateMedian
152
- // }
153
- // _mediansData = medianDataFromFile
154
- // }
155
- //CalculateMedian:
152
+ if _mediansData == nil && ! rogueData .IsRogue {
153
+ fileName , err := cmdUtils .GetProposeDataFileName (account .Address )
154
+ if err != nil {
155
+ log .Error ("Error in getting file name to read median data: " , err )
156
+ goto CalculateMedian
157
+ }
158
+ proposedata , err := razorUtils .ReadFromProposeJsonFile (fileName )
159
+ if err != nil {
160
+ log .Errorf ("Error in getting propose data from file %s: %t" , fileName , err )
161
+ goto CalculateMedian
162
+ }
163
+ if proposedata .Epoch != epoch {
164
+ log .Errorf ("File %s doesn't contain latest median data: %t" , fileName , err )
165
+ goto CalculateMedian
166
+ }
167
+ _mediansData = proposedata .MediansData
168
+ _revealedDataMaps = proposedata .RevealedDataMaps
169
+ _revealedCollectionIds = proposedata .RevealedCollectionIds
170
+ }
171
+ CalculateMedian:
156
172
if _mediansData == nil || _revealedCollectionIds == nil || _revealedDataMaps == nil {
157
173
medians , revealedCollectionIds , revealedDataMaps , err := cmdUtils .MakeBlock (client , blockNumber , epoch , types.Rogue {IsRogue : false })
158
174
if err != nil {
@@ -239,7 +255,10 @@ func (*UtilsStruct) Dispute(client *ethclient.Client, config types.Configuration
239
255
return err
240
256
}
241
257
log .Info ("Txn Hash: " , transactionUtils .Hash (finalizeTxn ))
242
- razorUtils .WaitForBlockCompletion (client , transactionUtils .Hash (finalizeTxn ).String ())
258
+ status := razorUtils .WaitForBlockCompletion (client , transactionUtils .Hash (finalizeTxn ).String ())
259
+ if status == 1 {
260
+ disputedFlag = true
261
+ }
243
262
return nil
244
263
}
245
264
@@ -278,3 +297,39 @@ func (*UtilsStruct) GetCollectionIdPositionInBlock(client *ethclient.Client, lea
278
297
}
279
298
return nil
280
299
}
300
+
301
+ func (* UtilsStruct ) GetBountyIdFromEvents (client * ethclient.Client , blockNumber * big.Int , bountyHunter string ) (uint32 , error ) {
302
+ fromBlock , err := utils .CalculateBlockNumberAtEpochBeginning (client , core .EpochLength , blockNumber )
303
+ if err != nil {
304
+ log .Error (err )
305
+ return 0 , err
306
+ }
307
+ query := ethereum.FilterQuery {
308
+ FromBlock : fromBlock ,
309
+ ToBlock : blockNumber ,
310
+ Addresses : []common.Address {
311
+ common .HexToAddress (core .StakeManagerAddress ),
312
+ },
313
+ }
314
+ logs , err := utils .UtilsInterface .FilterLogsWithRetry (client , query )
315
+ if err != nil {
316
+ return 0 , err
317
+ }
318
+ contractAbi , err := utils .ABIInterface .Parse (strings .NewReader (bindings .StakeManagerABI ))
319
+ if err != nil {
320
+ return 0 , err
321
+ }
322
+ bountyId := uint32 (0 )
323
+ for _ , vLog := range logs {
324
+ data , unpackErr := abiUtils .Unpack (contractAbi , "Slashed" , vLog .Data )
325
+ if unpackErr != nil {
326
+ log .Error (unpackErr )
327
+ continue
328
+ }
329
+ addressFromLogs := fmt .Sprint (data [1 ])
330
+ if bountyHunter == addressFromLogs {
331
+ bountyId = data [0 ].(uint32 )
332
+ }
333
+ }
334
+ return bountyId , nil
335
+ }
0 commit comments