Skip to content

Commit

Permalink
v0.1.6 support implemented (#182)
Browse files Browse the repository at this point in the history
* Start working on v0.1.6
* Change contracts version on package.json
* Fixed all compilation errors.
* Ran gofmt.
* Fix linting and test issues.
* Fix performAggregation and add test cases.
* Add new state parameter.
* Commit and Reveal working.
* Vote working.
* Run gofmt.
* Change version
* Change the log messages.
* Fix requested changes.
* Remove unnecessary test.
* Add stakerId as a flag in unstake, withdraw and reset lock.

Signed-off-by: Ashish Mishra <[email protected]>
  • Loading branch information
ashish10677 authored Sep 9, 2021
1 parent 47eda61 commit 065909a
Show file tree
Hide file tree
Showing 32 changed files with 924 additions and 6,104 deletions.
27 changes: 8 additions & 19 deletions cmd/addJobToCollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
log "github.com/sirupsen/logrus"
"math/big"
"razor/core"
"razor/core/types"
"razor/utils"
Expand Down Expand Up @@ -30,21 +29,11 @@ Note:
password := utils.PasswordPrompt()

address, _ := cmd.Flags().GetString("address")
jobId, _ := cmd.Flags().GetString("jobId")
collectionId, _ := cmd.Flags().GetString("collectionId")
jobId, _ := cmd.Flags().GetUint8("jobId")
collectionId, _ := cmd.Flags().GetUint8("collectionId")

client := utils.ConnectToClient(config.Provider)

jobIdInBigInt, ok := new(big.Int).SetString(jobId, 10)
if !ok {
log.Fatal("SetString: error")
}

collectionIdInBigInt, ok := new(big.Int).SetString(collectionId, 10)
if !ok {
log.Fatal("SetString: error")
}

txnOpts := utils.GetTxnOpts(types.TransactionOptions{
Client: client,
Password: password,
Expand All @@ -54,8 +43,8 @@ Note:
})

assetManager := utils.GetAssetManager(client)
log.Infof("Adding Job %s to collection %s", jobIdInBigInt, collectionIdInBigInt)
txn, err := assetManager.AddJobToCollection(txnOpts, collectionIdInBigInt, jobIdInBigInt)
log.Infof("Adding Job %d to collection %d", jobId, collectionId)
txn, err := assetManager.AddJobToCollection(txnOpts, collectionId, jobId)
if err != nil {
log.Fatal(err)
}
Expand All @@ -68,13 +57,13 @@ func init() {

var (
Account string
JobId string
CollectionId string
JobId uint8
CollectionId uint8
)

addJobToCollectionCmd.Flags().StringVarP(&Account, "address", "a", "", "address of the job creator")
addJobToCollectionCmd.Flags().StringVarP(&JobId, "jobId", "", "", "job id to add to the collection")
addJobToCollectionCmd.Flags().StringVarP(&CollectionId, "collectionId", "", "", "collection id")
addJobToCollectionCmd.Flags().Uint8VarP(&JobId, "jobId", "", 0, "job id to add to the collection")
addJobToCollectionCmd.Flags().Uint8VarP(&CollectionId, "collectionId", "", 0, "collection id")

addrErr := addJobToCollectionCmd.MarkFlagRequired("address")
utils.CheckError("Address error: ", addrErr)
Expand Down
50 changes: 34 additions & 16 deletions cmd/cmd-utils.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
package cmd

import (
"math/big"
"razor/utils"
"time"

"github.com/ethereum/go-ethereum/ethclient"
log "github.com/sirupsen/logrus"
)

func WaitForCommitState(client *ethclient.Client, accountAddress string, action string) (*big.Int, error) {
func GetEpochAndState(client *ethclient.Client, accountAddress string) (uint32, int64, error) {
epoch, err := utils.GetEpoch(client, accountAddress)
if err != nil {
return 0, 0, err
}
bufferPercent, err := getBufferPercent()
if err != nil {
return 0, 0, err
}
state, err := utils.GetDelayedState(client, bufferPercent)
if err != nil {
return 0, 0, err
}
log.Info("Epoch ", epoch)
log.Info("State ", state)
return epoch, state, nil
}

func WaitForCommitState(client *ethclient.Client, accountAddress string, action string) (uint32, error) {
for {
epoch, err := utils.GetEpoch(client, accountAddress)
if err != nil {
log.Fatal("Error in fetching epoch: ", err)
}
bufferPercent, err := getBufferPercent()
if err != nil {
log.Fatal(err)
}
state, err := utils.GetDelayedState(client, bufferPercent)
if err != nil {
log.Fatal("Error in fetching state: ", err)
}
log.Info("Epoch ", epoch)
log.Info("State ", state)
epoch, state, err := GetEpochAndState(client, accountAddress)
utils.CheckError("Error in fetching epoch and state: ", err)
if state != 0 {
log.Infof("Can only %s during state 0 (commit). Retrying in 5 second...", action)
time.Sleep(5 * time.Second)
Expand All @@ -33,3 +38,16 @@ func WaitForCommitState(client *ethclient.Client, accountAddress string, action
}
}
}

func WaitForDisputeOrConfirmState(client *ethclient.Client, accountAddress string, action string) (uint32, error) {
for {
epoch, state, err := GetEpochAndState(client, accountAddress)
utils.CheckError("Error in fetching epoch and state: ", err)
if state != 3 && state != 4 {
log.Infof("Can only %s during dispute or confirm state. Retrying in 5 seconds...", action)
time.Sleep(5 * time.Second)
} else {
return epoch, nil
}
}
}
20 changes: 2 additions & 18 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"encoding/hex"
"errors"
"fmt"
"math/big"
"razor/core"
Expand Down Expand Up @@ -30,27 +29,12 @@ func Commit(client *ethclient.Client, data []*big.Int, secret []byte, account ty
return err
}

root, err := utils.GetMerkleTreeRoot(data)
if err != nil {
return err
}

epoch, err := utils.GetEpoch(client, account.Address)
if err != nil {
return err
}

// Required if 2 or more instances of same staker is running and one of them has already committed in the current epoch
commitments, err := utils.GetCommitments(client, account.Address, epoch)
if err != nil {
return err
}
if !utils.AllZero(commitments) {
return errors.New("already committed")
}

commitment := solsha3.SoliditySHA3([]string{"uint256", "bytes32", "bytes32"}, []interface{}{epoch.String(), "0x" + hex.EncodeToString(root), "0x" + hex.EncodeToString(secret)})

commitment := solsha3.SoliditySHA3([]string{"uint32", "uint256[]", "bytes32"}, []interface{}{epoch, data, "0x" + hex.EncodeToString(secret)})
voteManager := utils.GetVoteManager(client)
txnOpts := utils.GetTxnOpts(types.TransactionOptions{
Client: client,
Expand All @@ -62,7 +46,7 @@ func Commit(client *ethclient.Client, data []*big.Int, secret []byte, account ty
commitmentToSend := [32]byte{}
copy(commitmentToSend[:], commitment)

log.Infof("Committing: epoch: %s, root: %s, commitment: %s, secret: %s, account: %s", epoch, "0x"+hex.EncodeToString(root), "0x"+hex.EncodeToString(commitment), "0x"+hex.EncodeToString(secret), account.Address)
log.Infof("Committing: epoch: %d, commitment: %s, secret: %s, account: %s", epoch, "0x"+hex.EncodeToString(commitment), "0x"+hex.EncodeToString(secret), account.Address)

txn, err := voteManager.Commit(txnOpts, epoch, commitmentToSend)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions cmd/confirm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
log "github.com/sirupsen/logrus"
"razor/core/types"
"razor/utils"
)

func ClaimBlockReward(options types.TransactionOptions) {
blockManager := utils.GetBlockManager(options.Client)
txn, err := blockManager.ClaimBlockReward(utils.GetTxnOpts(options))
if err != nil {
log.Error("Error in claiming block reward: ", err)
return
}
log.Info("Claiming block reward...")
log.Info("Txn Hash: ", txn.Hash().Hex())
utils.WaitForBlockCompletion(options.Client, txn.Hash().Hex())
}
25 changes: 14 additions & 11 deletions cmd/createCollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,25 @@ Note:
password := utils.AssignPassword(cmd.Flags())
name, _ := cmd.Flags().GetString("name")
address, _ := cmd.Flags().GetString("address")
jobIds, _ := cmd.Flags().GetStringSlice("jobIds")
jobIdInUint, _ := cmd.Flags().GetUintSlice("jobIds")
aggregation, _ := cmd.Flags().GetUint32("aggregation")
power, _ := cmd.Flags().GetInt8("power")

client := utils.ConnectToClient(config.Provider)

jobIdsInBigInt := utils.ConvertToBigIntArray(jobIds)

txnOpts := utils.GetTxnOpts(types.TransactionOptions{
Client: client,
Password: password,
AccountAddress: address,
ChainId: core.ChainId,
Config: config,
})

jobIds := utils.ConvertUintArrayToUint8Array(jobIdInUint)
assetManager := utils.GetAssetManager(client)
txn, err := assetManager.CreateCollection(txnOpts, name, jobIdsInBigInt, aggregation)
if err != nil {
log.Fatal(err)
}
_, err = WaitForDisputeOrConfirmState(client, address, "create collection")
utils.CheckError("Error in fetching state: ", err)
txn, err := assetManager.CreateCollection(txnOpts, jobIds, aggregation, power, name)
utils.CheckError("Error in creating collection: ", err)
log.Info("Creating collection...")
utils.WaitForBlockCompletion(client, txn.Hash().String())
},
Expand All @@ -59,21 +58,25 @@ func init() {
var (
Name string
Account string
JobIds []string
JobIds []uint
AggregationMethod uint32
Password string
Power int8
)

createCollectionCmd.Flags().StringVarP(&Name, "name", "n", "", "name of the collection")
createCollectionCmd.Flags().StringVarP(&Account, "address", "a", "", "address of the job creator")
createCollectionCmd.Flags().StringSliceVarP(&JobIds, "jobIds", "", []string{}, "job ids for the collection")
createCollectionCmd.Flags().UintSliceVarP(&JobIds, "jobIds", "", []uint{}, "job ids for the collection")
createCollectionCmd.Flags().Uint32VarP(&AggregationMethod, "aggregation", "", 1, "aggregation method to be used")
createCollectionCmd.Flags().StringVarP(&Password, "password", "", "", "password path of job creater to protect the keystore")
createCollectionCmd.Flags().Int8VarP(&Power, "power", "", 0, "multiplier for the collection")
createCollectionCmd.Flags().StringVarP(&Password, "password", "", "", "password path of job creator to protect the keystore")

nameErr := createCollectionCmd.MarkFlagRequired("name")
utils.CheckError("Name error: ", nameErr)
addrErr := createCollectionCmd.MarkFlagRequired("address")
utils.CheckError("Address Error: ", addrErr)
jobIdErr := createCollectionCmd.MarkFlagRequired("jobIds")
utils.CheckError("Job Id Error: ", jobIdErr)
powerErr := createCollectionCmd.MarkFlagRequired("power")
utils.CheckError("Power Error: ", powerErr)
}
10 changes: 6 additions & 4 deletions cmd/createJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Note:
password := utils.AssignPassword(cmd.Flags())
address, _ := cmd.Flags().GetString("address")
name, _ := cmd.Flags().GetString("name")
repeat, _ := cmd.Flags().GetBool("repeat")
url, _ := cmd.Flags().GetString("url")
selector, _ := cmd.Flags().GetString("selector")
power, _ := cmd.Flags().GetInt8("power")

client := utils.ConnectToClient(config.Provider)
txnOpts := utils.GetTxnOpts(types.TransactionOptions{
Expand All @@ -45,7 +45,7 @@ Note:

assetManager := utils.GetAssetManager(client)
log.Info("Creating Job...")
txn, err := assetManager.CreateJob(txnOpts, url, selector, name, repeat)
txn, err := assetManager.CreateJob(txnOpts, power, name, selector, url)
if err != nil {
log.Fatal(err)
}
Expand All @@ -62,15 +62,15 @@ func init() {
URL string
Selector string
Name string
Repeat bool
Power int8
Account string
Password string
)

createJobCmd.Flags().StringVarP(&URL, "url", "u", "", "url of job")
createJobCmd.Flags().StringVarP(&Selector, "selector", "s", "", "selector (jsonPath selector)")
createJobCmd.Flags().StringVarP(&Name, "name", "n", "", "name of job")
createJobCmd.Flags().BoolVarP(&Repeat, "repeat", "r", true, "repeat")
createJobCmd.Flags().Int8VarP(&Power, "power", "", 0, "power")
createJobCmd.Flags().StringVarP(&Account, "address", "a", "", "address of the job creator")
createJobCmd.Flags().StringVarP(&Password, "password", "", "", "password path of job creator to protect the keystore")

Expand All @@ -82,4 +82,6 @@ func init() {
utils.CheckError("Name error: ", nameErr)
addrErr := createJobCmd.MarkFlagRequired("address")
utils.CheckError("Address error: ", addrErr)
powErr := createJobCmd.MarkFlagRequired("power")
utils.CheckError("Power error: ", powErr)
}
16 changes: 5 additions & 11 deletions cmd/delegate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"math/big"
"razor/core"
"razor/core/types"
"razor/utils"
Expand All @@ -25,7 +24,7 @@ Example:

password := utils.AssignPassword(cmd.Flags())
address, _ := cmd.Flags().GetString("address")
stakerId, _ := cmd.Flags().GetString("stakerId")
stakerId, _ := cmd.Flags().GetUint32("stakerId")

client := utils.ConnectToClient(config.Provider)

Expand All @@ -37,11 +36,6 @@ Example:

utils.CheckEthBalanceIsZero(client, address)

_stakerId, ok := new(big.Int).SetString(stakerId, 10)
if !ok {
log.Fatal("SetString error while converting stakerId")
}

stakeManager := utils.GetStakeManager(client)
txnOpts := types.TransactionOptions{
Client: client,
Expand All @@ -54,11 +48,11 @@ Example:

approve(txnOpts)

log.Infof("Delegating %g razors to Staker %s", utils.GetAmountInDecimal(valueInWei), _stakerId)
log.Infof("Delegating %g razors to Staker %d", utils.GetAmountInDecimal(valueInWei), stakerId)
delegationTxnOpts := utils.GetTxnOpts(txnOpts)
epoch, err := WaitForCommitState(client, address, "delegate")
utils.CheckError("Error in fetching epoch: ", err)
txn, err := stakeManager.Delegate(delegationTxnOpts, epoch, valueInWei, _stakerId)
txn, err := stakeManager.Delegate(delegationTxnOpts, epoch, stakerId, valueInWei)
utils.CheckError("Error in delegating: ", err)
log.Infof("Sending Delegate transaction...")
log.Infof("Transaction hash: %s", txn.Hash())
Expand All @@ -71,14 +65,14 @@ func init() {
var (
Amount string
Address string
StakerId string
StakerId uint32
Password string
Power string
)

delegateCmd.Flags().StringVarP(&Amount, "value", "v", "0", "amount to stake (in Wei)")
delegateCmd.Flags().StringVarP(&Address, "address", "a", "", "your account address")
delegateCmd.Flags().StringVarP(&StakerId, "stakerId", "", "", "staker id")
delegateCmd.Flags().Uint32VarP(&StakerId, "stakerId", "", 0, "staker id")
delegateCmd.Flags().StringVarP(&Password, "password", "", "", "password path to protect the keystore")
delegateCmd.Flags().StringVarP(&Power, "pow", "", "", "power of 10")

Expand Down
Loading

0 comments on commit 065909a

Please sign in to comment.