Skip to content

Commit cc6e22c

Browse files
committed
Merge branch 'main' into rogue-stakerId
Signed-off-by: Ashish Kumar Mishra <[email protected]>
2 parents 0c65ba7 + f065e79 commit cc6e22c

18 files changed

+1040
-270
lines changed

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ docker exec -it razor-go razor ...
7272
3. If you already have the `pkg/bindings` you can run `npm run build` instead of `npm run build-all` to directly build the binary.
7373
4. If you want to build the binary without wanting to set the configurations use `npm run build-noargs`
7474
5. While building the binary, supply the provider RPC url and the gas multiplier.
75-
6. To bypass the intractive mode of providing password, create file in `.razor` directory with providing password in it.
75+
6. To bypass the interactive mode of providing password, create file in `.razor` directory with providing password in it.
7676
7. The binary will be generated at `build/bin`.
7777

7878
## Commands
@@ -335,6 +335,7 @@ Example:
335335
```
336336
$ ./razor vote --address 0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c
337337
```
338+
If you want to claim your bounty automatically after disputing staker, you can just pass `--autoClaimBounty` flag in your vote command.
338339

339340
If you want to report incorrect values, there is a `rogue` mode available. Just pass an extra flag `--rogue` to start voting in rogue mode and the client will report wrong medians.
340341
The rogueMode key can be used to specify in which particular voting state (commit, reveal, propose) you want to report incorrect values.
@@ -575,16 +576,16 @@ Example:
575576
$ ./razor createCollection --name btcCollectionMean --address 0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c --jobIds 1,2 --aggregation 2 --power 2 --tolerance 200
576577
```
577578

578-
### Modify Asset Status
579+
### Modify Collection Status
579580

580-
Modify the active status of an asset using the `modifyAssetStatus` command.
581+
Modify the active status of an collection using the `modifyCollectionStatus` command.
581582

582583
_Note: This command is restricted to "Admin Role"_
583584

584585
razor cli
585586

586587
```
587-
$ ./razor modifyAssetStatus --assetId <assetId> --address <address> --status <true_or_false>
588+
$ ./razor modifyCollectionStatus --collectionId <collectionId> --address <address> --status <true_or_false>
588589
```
589590

590591
docker
@@ -593,13 +594,13 @@ docker
593594
docker run -it \
594595
-v "$(echo $HOME)"/.razor:/root/.razor \
595596
razornetwork/razor-go:latest \
596-
modifyAssetStatus --assetId <assetId> --address <address> --status <true_or_false>
597+
modifyCollectionStatus --collectionId <collectionId> --address <address> --status <true_or_false>
597598
```
598599

599600
Example:
600601

601602
```
602-
$ ./razor modifyAssetStatus --assetId 1 --address 0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c --status false
603+
$ ./razor modifyCollectionStatus --collectionId 1 --address 0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c --status false
603604
```
604605

605606
### Update Collection
@@ -791,7 +792,7 @@ In the above example for the collection `ethCollectionMean`, new custom job havi
791792

792793
### Logs
793794

794-
User can pass a seperate flag --logFile followed with any name for log file along with command. The logs will be stored in ```.razor``` directory.
795+
User can pass a separate flag --logFile followed with any name for log file along with command. The logs will be stored in ```.razor``` directory.
795796

796797
```
797798
$ ./razor addStake --address <address> --value <value> --logFile stakingLogs
@@ -850,7 +851,7 @@ We would really appreciate your contribution. To see our [contribution guideline
850851
#Provide password through CLI
851852
docker-compose run razor-go /usr/local/bin/razor addStake --address <address> --value 50000
852853

853-
#Provide password throudh File
854+
#Provide password through File
854855

855856
#Create file and put password string
856857
vi ~/.razor/pass

cmd/claimBounty.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,17 @@ func (*UtilsStruct) ClaimBounty(config types.Configurations, client *ethclient.C
9393
}
9494

9595
log.Info("Claiming bounty transaction...")
96-
waitFor := bountyLock.RedeemAfter - epoch
96+
waitFor := int32(bountyLock.RedeemAfter) - int32(epoch)
9797
if waitFor > 0 {
9898
log.Debug("Waiting for lock period to get over....")
9999

100-
//waiting till epoch reaches redeemAfter
101-
timeUtils.Sleep(time.Duration(int64(waitFor)*core.EpochLength*razorUtils.CalculateBlockTime(client)) * time.Second)
100+
timeRemaining := int64(waitFor) * core.EpochLength
101+
if waitFor == 1 {
102+
log.Infof("Cannot claim bounty now. Please wait for %d epoch! (approximately %s)", waitFor, razorUtils.SecondsToReadableTime(int(timeRemaining)))
103+
} else {
104+
log.Infof("Cannot claim bounty now. Please wait for %d epochs! (approximately %s)", waitFor, razorUtils.SecondsToReadableTime(int(timeRemaining)))
105+
}
106+
return core.NilHash, nil
102107
}
103108

104109
txnOpts := razorUtils.GetTxnOpts(txnArgs)

cmd/dispute.go

+77-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package cmd
22

33
import (
44
"errors"
5+
"fmt"
6+
"github.com/ethereum/go-ethereum"
57
"github.com/ethereum/go-ethereum/accounts/abi/bind"
8+
"github.com/ethereum/go-ethereum/common"
69
types2 "github.com/ethereum/go-ethereum/core/types"
710
"github.com/ethereum/go-ethereum/ethclient"
811
"math/big"
@@ -11,13 +14,18 @@ import (
1114
"razor/core/types"
1215
"razor/pkg/bindings"
1316
"razor/utils"
17+
"strings"
1418
)
1519

16-
var giveSortedLeafIds []int
20+
var (
21+
giveSortedLeafIds []int
22+
disputedFlag bool
23+
)
1724

1825
//blockId is id of the block
1926

2027
func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) error {
28+
disputedFlag = false
2129

2230
sortedProposedBlockIds, err := razorUtils.GetSortedProposedBlockIds(client, epoch)
2331
if err != nil {
@@ -77,6 +85,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
7785
log.Info("Txn Hash: ", transactionUtils.Hash(disputeBiggestStakeProposedTxn))
7886
status := razorUtils.WaitForBlockCompletion(client, transactionUtils.Hash(disputeBiggestStakeProposedTxn).String())
7987
if status == 1 {
88+
disputedFlag = true
8089
continue
8190
}
8291
}
@@ -93,6 +102,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
93102
log.Debugf("Txn Hash: %s", transactionUtils.Hash(idDisputeTxn).String())
94103
status := razorUtils.WaitForBlockCompletion(client, transactionUtils.Hash(idDisputeTxn).String())
95104
if status == 1 {
105+
disputedFlag = true
96106
continue
97107
}
98108
}
@@ -108,7 +118,12 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
108118
// ids [1, 2, 3, 4]
109119
// Sorted revealed values would be the vote values for the wrong median, here 230
110120
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]
112127
leafId, err := utils.UtilsInterface.GetLeafIdOfACollection(client, collectionIdOfWrongMedian)
113128
if err != nil {
114129
log.Error("Error in leaf id: ", err)
@@ -133,26 +148,27 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
133148
}
134149

135150
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
137151

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:
156172
if _mediansData == nil || _revealedCollectionIds == nil || _revealedDataMaps == nil {
157173
medians, revealedCollectionIds, revealedDataMaps, err := cmdUtils.MakeBlock(client, blockNumber, epoch, types.Rogue{IsRogue: false})
158174
if err != nil {
@@ -239,7 +255,10 @@ func (*UtilsStruct) Dispute(client *ethclient.Client, config types.Configuration
239255
return err
240256
}
241257
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+
}
243262
return nil
244263
}
245264

@@ -278,3 +297,39 @@ func (*UtilsStruct) GetCollectionIdPositionInBlock(client *ethclient.Client, lea
278297
}
279298
return nil
280299
}
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+
}

cmd/interface.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ type UtilsInterface interface {
118118
AddJobToJSON(string, *types.StructsJob) error
119119
GetStakerSRZRBalance(*ethclient.Client, bindings.StructsStaker) (*big.Int, error)
120120
SecondsToReadableTime(int) string
121+
SaveDataToCommitJsonFile(string, uint32, types.CommitData) error
122+
ReadFromCommitJsonFile(string) (types.CommitFileData, error)
123+
SaveDataToProposeJsonFile(string, uint32, types.ProposeData) error
124+
ReadFromProposeJsonFile(string) (types.ProposeFileData, error)
125+
SaveDataToDisputeJsonFile(string, []uint32) error
126+
ReadFromDisputeJsonFile(string) (types.DisputeFileData, error)
121127
AssignLogFile(*pflag.FlagSet)
122128
}
123129

@@ -275,7 +281,7 @@ type UtilsCmdInterface interface {
275281
MakeBlock(client *ethclient.Client, blockNumber *big.Int, epoch uint32, rogueData types.Rogue) ([]uint32, []uint16, *types.RevealedDataMaps, error)
276282
IsElectedProposer(types.ElectedProposer, *big.Int) bool
277283
GetSortedRevealedValues(client *ethclient.Client, blockNumber *big.Int, epoch uint32) (*types.RevealedDataMaps, error)
278-
GetIteration(*ethclient.Client, types.ElectedProposer) int
284+
GetIteration(*ethclient.Client, types.ElectedProposer, int32) int
279285
Propose(client *ethclient.Client, config types.Configurations, account types.Account, staker bindings.StructsStaker, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) (common.Hash, error)
280286
GiveSorted(*ethclient.Client, *bindings.BlockManager, *bind.TransactOpts, uint32, uint16, []uint32)
281287
GetLocalMediansData(client *ethclient.Client, account types.Account, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) ([]uint32, []uint16, *types.RevealedDataMaps, error)
@@ -286,8 +292,8 @@ type UtilsCmdInterface interface {
286292
ExecuteExtendLock(*pflag.FlagSet)
287293
ResetUnstakeLock(*ethclient.Client, types.Configurations, types.ExtendLockInput) (common.Hash, error)
288294
CheckCurrentStatus(*ethclient.Client, uint16) (bool, error)
289-
ExecuteModifyAssetStatus(*pflag.FlagSet)
290-
ModifyAssetStatus(*ethclient.Client, types.Configurations, types.ModifyAssetInput) (common.Hash, error)
295+
ExecuteModifyCollectionStatus(*pflag.FlagSet)
296+
ModifyCollectionStatus(*ethclient.Client, types.Configurations, types.ModifyCollectionInput) (common.Hash, error)
291297
Approve(types.TransactionOptions) (common.Hash, error)
292298
ExecuteDelegate(*pflag.FlagSet)
293299
Delegate(types.TransactionOptions, uint32) (common.Hash, error)
@@ -301,7 +307,7 @@ type UtilsCmdInterface interface {
301307
StakeCoins(types.TransactionOptions) (common.Hash, error)
302308
AutoUnstakeAndWithdraw(*ethclient.Client, types.Account, *big.Int, types.Configurations)
303309
GetCommitDataFileName(string) (string, error)
304-
GetMedianDataFileName(string) (string, error)
310+
GetProposeDataFileName(string) (string, error)
305311
CalculateSecret(types.Account, uint32) ([]byte, error)
306312
GetLastProposedEpoch(*ethclient.Client, *big.Int, uint32) (uint32, error)
307313
HandleBlock(*ethclient.Client, types.Account, *big.Int, types.Configurations, types.Rogue)
@@ -313,6 +319,8 @@ type UtilsCmdInterface interface {
313319
ExecuteStake(*pflag.FlagSet)
314320
InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, rogueData types.Rogue) error
315321
InitiateReveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, rogueData types.Rogue) error
322+
GetBountyIdFromEvents(client *ethclient.Client, blockNumber *big.Int, bountyHunter string) (uint32, error)
323+
AutoClaimBounty(client *ethclient.Client, config types.Configurations, account types.Account) error
316324
}
317325

318326
type TransactionInterface interface {

0 commit comments

Comments
 (0)