Skip to content

Commit

Permalink
Commit data Fix (#1111)
Browse files Browse the repository at this point in the history
* Saved commit data before checking for transaction status

* Fixed InitiateCommit() and InitiateReveal() tests

* Fixed GetCommitment() contract call

* Verify commitment for commit file data

* Fixed tests
  • Loading branch information
Yashk767 authored Aug 28, 2023
1 parent 1695706 commit 80ee390
Show file tree
Hide file tree
Showing 12 changed files with 695 additions and 292 deletions.
79 changes: 72 additions & 7 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
return types.CommitData{}, err
}
log.Debug("HandleCommitState: Number of active collections: ", numActiveCollections)
log.Debugf("HandleCommitState: Calling GetAssignedCollections() with arguments number of active collections = %d, seed = %v", numActiveCollections, seed)
log.Debugf("HandleCommitState: Calling GetAssignedCollections() with arguments number of active collections = %d", numActiveCollections)
assignedCollections, seqAllottedCollections, err := razorUtils.GetAssignedCollections(client, numActiveCollections, seed)
if err != nil {
return types.CommitData{}, err
Expand Down Expand Up @@ -112,15 +112,18 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
/*
Commit finally commits the data to the smart contract. It calculates the commitment to send using the merkle tree root and the seed.
*/
func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, root [32]byte) (common.Hash, error) {
func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error) {
if state, err := razorUtils.GetBufferedState(client, config.BufferPercent); err != nil || state != 0 {
log.Error("Not commit state")
return core.NilHash, err
}

commitment := solsha3.SoliditySHA3([]string{"bytes32", "bytes32"}, []interface{}{"0x" + hex.EncodeToString(root[:]), "0x" + hex.EncodeToString(seed)})
commitmentToSend := [32]byte{}
copy(commitmentToSend[:], commitment)
commitmentToSend, err := CalculateCommitment(seed, values)
if err != nil {
log.Error("Error in getting commitment: ", err)
return core.NilHash, err
}

txnOpts := razorUtils.GetTxnOpts(types.TransactionOptions{
Client: client,
Password: account.Password,
Expand All @@ -133,8 +136,6 @@ func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations
Parameters: []interface{}{epoch, commitmentToSend},
})

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

log.Info("Commitment sent...")
log.Debugf("Executing Commit transaction with epoch = %d, commitmentToSend = %v", epoch, commitmentToSend)
txn, err := voteManagerUtils.Commit(client, txnOpts, epoch, commitmentToSend)
Expand All @@ -145,3 +146,67 @@ func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations
log.Info("Txn Hash: ", txnHash.Hex())
return txnHash, nil
}

func CalculateSeed(client *ethclient.Client, account types.Account, keystorePath string, epoch uint32) ([]byte, error) {
log.Debugf("CalculateSeed: Calling CalculateSecret() with arguments epoch = %d, keystorePath = %s, chainId = %s", epoch, keystorePath, core.ChainId)
_, secret, err := cmdUtils.CalculateSecret(account, epoch, keystorePath, core.ChainId)
if err != nil {
return nil, err
}
log.Debugf("CalculateSeed: Getting Salt for current epoch %d...", epoch)
salt, err := cmdUtils.GetSalt(client, epoch)
if err != nil {
log.Error("Error in getting salt: ", err)
return nil, err
}
seed := solsha3.SoliditySHA3([]string{"bytes32", "bytes32"}, []interface{}{"0x" + hex.EncodeToString(salt[:]), "0x" + hex.EncodeToString(secret)})
return seed, nil
}

func CalculateCommitment(seed []byte, values []*big.Int) ([32]byte, error) {
log.Debug("CalculateCommitment: Calling CreateMerkle() with argument Leaves = ", values)
merkleTree, err := merkleUtils.CreateMerkle(values)
if err != nil {
return [32]byte{}, errors.New("Error in getting merkle tree: " + err.Error())
}
log.Debug("CalculateCommitment: Merkle Tree: ", merkleTree)
log.Debug("CalculateCommitment: Calling GetMerkleRoot() for the merkle tree...")
merkleRoot, err := merkleUtils.GetMerkleRoot(merkleTree)
if err != nil {
return [32]byte{}, errors.New("Error in getting root: " + err.Error())
}
commitment := solsha3.SoliditySHA3([]string{"bytes32", "bytes32"}, []interface{}{"0x" + hex.EncodeToString(merkleRoot[:]), "0x" + hex.EncodeToString(seed)})
log.Debug("CalculateCommitment: Commitment: ", hex.EncodeToString(commitment))
commitmentToSend := [32]byte{}
copy(commitmentToSend[:], commitment)
return commitmentToSend, nil
}

func VerifyCommitment(client *ethclient.Client, account types.Account, keystorePath string, epoch uint32, values []*big.Int) (bool, error) {
commitmentStruct, err := razorUtils.GetCommitment(client, account.Address)
if err != nil {
log.Error("Error in getting commitments: ", err)
return false, err
}
log.Debugf("VerifyCommitment: CommitmentStruct: %+v", commitmentStruct)

seed, err := CalculateSeed(client, account, keystorePath, epoch)
if err != nil {
log.Error("Error in calculating seed: ", err)
return false, err
}

calculatedCommitment, err := CalculateCommitment(seed, values)
if err != nil {
log.Error("Error in calculating commitment for given committed values: ", err)
return false, err
}
log.Debug("VerifyCommitment: Calculated commitment: ", calculatedCommitment)

if calculatedCommitment == commitmentStruct.CommitmentHash {
log.Debug("VerifyCommitment: Calculated commitment for given values is EQUAL to commitment of the epoch")
return true, nil
}
log.Debug("VerifyCommitment: Calculated commitment for given values DOES NOT MATCH with commitment in the epoch")
return false, nil
}
11 changes: 9 additions & 2 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
"razor/utils"
"reflect"
"testing"
)
Expand All @@ -31,9 +32,9 @@ func TestCommit(t *testing.T) {
txnOpts, _ := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1))

type args struct {
values []*big.Int
state int64
stateErr error
root [32]byte
txnOpts *bind.TransactOpts
commitTxn *Types.Transaction
commitErr error
Expand All @@ -48,6 +49,7 @@ func TestCommit(t *testing.T) {
{
name: "Test 1: When Commit function executes successfully",
args: args{
values: []*big.Int{big.NewInt(1)},
state: 0,
stateErr: nil,
txnOpts: txnOpts,
Expand All @@ -61,6 +63,7 @@ func TestCommit(t *testing.T) {
{
name: "Test 2: When there is an error in getting state",
args: args{
values: []*big.Int{big.NewInt(1)},
stateErr: errors.New("state error"),
txnOpts: txnOpts,
commitTxn: &Types.Transaction{},
Expand All @@ -73,6 +76,7 @@ func TestCommit(t *testing.T) {
{
name: "Test 3: When Commit transaction fails",
args: args{
values: []*big.Int{big.NewInt(1)},
state: 0,
stateErr: nil,
txnOpts: txnOpts,
Expand All @@ -88,13 +92,16 @@ func TestCommit(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utils.MerkleInterface = &utils.MerkleTreeStruct{}
merkleUtils = utils.MerkleInterface

utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr)
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(tt.args.txnOpts)
voteManagerMock.On("Commit", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.AnythingOfType("uint32"), mock.Anything).Return(tt.args.commitTxn, tt.args.commitErr)
transactionMock.On("Hash", mock.AnythingOfType("*types.Transaction")).Return(tt.args.hash)

utils := &UtilsStruct{}
got, err := utils.Commit(client, config, account, epoch, seed, tt.args.root)
got, err := utils.Commit(client, config, account, epoch, seed, tt.args.values)
if got != tt.want {
t.Errorf("Txn hash for Commit function, got = %v, want = %v", got, tt.want)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ type UtilsCmdInterface interface {
ClaimBlockReward(options types.TransactionOptions) (common.Hash, error)
GetSalt(client *ethclient.Client, epoch uint32) ([32]byte, error)
HandleCommitState(client *ethclient.Client, epoch uint32, seed []byte, rogueData types.Rogue) (types.CommitData, error)
Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, root [32]byte) (common.Hash, error)
Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error)
ListAccounts() ([]accounts.Account, error)
AssignAmountInWei(flagSet *pflag.FlagSet) (*big.Int, error)
ExecuteTransfer(flagSet *pflag.FlagSet)
Expand Down
Loading

0 comments on commit 80ee390

Please sign in to comment.