Skip to content

Commit 80ee390

Browse files
authored
Commit data Fix (#1111)
* 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
1 parent 1695706 commit 80ee390

12 files changed

+695
-292
lines changed

cmd/commit.go

+72-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
6161
return types.CommitData{}, err
6262
}
6363
log.Debug("HandleCommitState: Number of active collections: ", numActiveCollections)
64-
log.Debugf("HandleCommitState: Calling GetAssignedCollections() with arguments number of active collections = %d, seed = %v", numActiveCollections, seed)
64+
log.Debugf("HandleCommitState: Calling GetAssignedCollections() with arguments number of active collections = %d", numActiveCollections)
6565
assignedCollections, seqAllottedCollections, err := razorUtils.GetAssignedCollections(client, numActiveCollections, seed)
6666
if err != nil {
6767
return types.CommitData{}, err
@@ -112,15 +112,18 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
112112
/*
113113
Commit finally commits the data to the smart contract. It calculates the commitment to send using the merkle tree root and the seed.
114114
*/
115-
func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, root [32]byte) (common.Hash, error) {
115+
func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error) {
116116
if state, err := razorUtils.GetBufferedState(client, config.BufferPercent); err != nil || state != 0 {
117117
log.Error("Not commit state")
118118
return core.NilHash, err
119119
}
120120

121-
commitment := solsha3.SoliditySHA3([]string{"bytes32", "bytes32"}, []interface{}{"0x" + hex.EncodeToString(root[:]), "0x" + hex.EncodeToString(seed)})
122-
commitmentToSend := [32]byte{}
123-
copy(commitmentToSend[:], commitment)
121+
commitmentToSend, err := CalculateCommitment(seed, values)
122+
if err != nil {
123+
log.Error("Error in getting commitment: ", err)
124+
return core.NilHash, err
125+
}
126+
124127
txnOpts := razorUtils.GetTxnOpts(types.TransactionOptions{
125128
Client: client,
126129
Password: account.Password,
@@ -133,8 +136,6 @@ func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations
133136
Parameters: []interface{}{epoch, commitmentToSend},
134137
})
135138

136-
log.Debugf("Committing: epoch: %d, commitment: %s, seed: %s, account: %s", epoch, "0x"+hex.EncodeToString(commitment), "0x"+hex.EncodeToString(seed), account.Address)
137-
138139
log.Info("Commitment sent...")
139140
log.Debugf("Executing Commit transaction with epoch = %d, commitmentToSend = %v", epoch, commitmentToSend)
140141
txn, err := voteManagerUtils.Commit(client, txnOpts, epoch, commitmentToSend)
@@ -145,3 +146,67 @@ func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations
145146
log.Info("Txn Hash: ", txnHash.Hex())
146147
return txnHash, nil
147148
}
149+
150+
func CalculateSeed(client *ethclient.Client, account types.Account, keystorePath string, epoch uint32) ([]byte, error) {
151+
log.Debugf("CalculateSeed: Calling CalculateSecret() with arguments epoch = %d, keystorePath = %s, chainId = %s", epoch, keystorePath, core.ChainId)
152+
_, secret, err := cmdUtils.CalculateSecret(account, epoch, keystorePath, core.ChainId)
153+
if err != nil {
154+
return nil, err
155+
}
156+
log.Debugf("CalculateSeed: Getting Salt for current epoch %d...", epoch)
157+
salt, err := cmdUtils.GetSalt(client, epoch)
158+
if err != nil {
159+
log.Error("Error in getting salt: ", err)
160+
return nil, err
161+
}
162+
seed := solsha3.SoliditySHA3([]string{"bytes32", "bytes32"}, []interface{}{"0x" + hex.EncodeToString(salt[:]), "0x" + hex.EncodeToString(secret)})
163+
return seed, nil
164+
}
165+
166+
func CalculateCommitment(seed []byte, values []*big.Int) ([32]byte, error) {
167+
log.Debug("CalculateCommitment: Calling CreateMerkle() with argument Leaves = ", values)
168+
merkleTree, err := merkleUtils.CreateMerkle(values)
169+
if err != nil {
170+
return [32]byte{}, errors.New("Error in getting merkle tree: " + err.Error())
171+
}
172+
log.Debug("CalculateCommitment: Merkle Tree: ", merkleTree)
173+
log.Debug("CalculateCommitment: Calling GetMerkleRoot() for the merkle tree...")
174+
merkleRoot, err := merkleUtils.GetMerkleRoot(merkleTree)
175+
if err != nil {
176+
return [32]byte{}, errors.New("Error in getting root: " + err.Error())
177+
}
178+
commitment := solsha3.SoliditySHA3([]string{"bytes32", "bytes32"}, []interface{}{"0x" + hex.EncodeToString(merkleRoot[:]), "0x" + hex.EncodeToString(seed)})
179+
log.Debug("CalculateCommitment: Commitment: ", hex.EncodeToString(commitment))
180+
commitmentToSend := [32]byte{}
181+
copy(commitmentToSend[:], commitment)
182+
return commitmentToSend, nil
183+
}
184+
185+
func VerifyCommitment(client *ethclient.Client, account types.Account, keystorePath string, epoch uint32, values []*big.Int) (bool, error) {
186+
commitmentStruct, err := razorUtils.GetCommitment(client, account.Address)
187+
if err != nil {
188+
log.Error("Error in getting commitments: ", err)
189+
return false, err
190+
}
191+
log.Debugf("VerifyCommitment: CommitmentStruct: %+v", commitmentStruct)
192+
193+
seed, err := CalculateSeed(client, account, keystorePath, epoch)
194+
if err != nil {
195+
log.Error("Error in calculating seed: ", err)
196+
return false, err
197+
}
198+
199+
calculatedCommitment, err := CalculateCommitment(seed, values)
200+
if err != nil {
201+
log.Error("Error in calculating commitment for given committed values: ", err)
202+
return false, err
203+
}
204+
log.Debug("VerifyCommitment: Calculated commitment: ", calculatedCommitment)
205+
206+
if calculatedCommitment == commitmentStruct.CommitmentHash {
207+
log.Debug("VerifyCommitment: Calculated commitment for given values is EQUAL to commitment of the epoch")
208+
return true, nil
209+
}
210+
log.Debug("VerifyCommitment: Calculated commitment for given values DOES NOT MATCH with commitment in the epoch")
211+
return false, nil
212+
}

cmd/commit_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"razor/core"
1616
"razor/core/types"
1717
"razor/pkg/bindings"
18+
"razor/utils"
1819
"reflect"
1920
"testing"
2021
)
@@ -31,9 +32,9 @@ func TestCommit(t *testing.T) {
3132
txnOpts, _ := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1))
3233

3334
type args struct {
35+
values []*big.Int
3436
state int64
3537
stateErr error
36-
root [32]byte
3738
txnOpts *bind.TransactOpts
3839
commitTxn *Types.Transaction
3940
commitErr error
@@ -48,6 +49,7 @@ func TestCommit(t *testing.T) {
4849
{
4950
name: "Test 1: When Commit function executes successfully",
5051
args: args{
52+
values: []*big.Int{big.NewInt(1)},
5153
state: 0,
5254
stateErr: nil,
5355
txnOpts: txnOpts,
@@ -61,6 +63,7 @@ func TestCommit(t *testing.T) {
6163
{
6264
name: "Test 2: When there is an error in getting state",
6365
args: args{
66+
values: []*big.Int{big.NewInt(1)},
6467
stateErr: errors.New("state error"),
6568
txnOpts: txnOpts,
6669
commitTxn: &Types.Transaction{},
@@ -73,6 +76,7 @@ func TestCommit(t *testing.T) {
7376
{
7477
name: "Test 3: When Commit transaction fails",
7578
args: args{
79+
values: []*big.Int{big.NewInt(1)},
7680
state: 0,
7781
stateErr: nil,
7882
txnOpts: txnOpts,
@@ -88,13 +92,16 @@ func TestCommit(t *testing.T) {
8892
t.Run(tt.name, func(t *testing.T) {
8993
SetUpMockInterfaces()
9094

95+
utils.MerkleInterface = &utils.MerkleTreeStruct{}
96+
merkleUtils = utils.MerkleInterface
97+
9198
utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr)
9299
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(tt.args.txnOpts)
93100
voteManagerMock.On("Commit", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.AnythingOfType("uint32"), mock.Anything).Return(tt.args.commitTxn, tt.args.commitErr)
94101
transactionMock.On("Hash", mock.AnythingOfType("*types.Transaction")).Return(tt.args.hash)
95102

96103
utils := &UtilsStruct{}
97-
got, err := utils.Commit(client, config, account, epoch, seed, tt.args.root)
104+
got, err := utils.Commit(client, config, account, epoch, seed, tt.args.values)
98105
if got != tt.want {
99106
t.Errorf("Txn hash for Commit function, got = %v, want = %v", got, tt.want)
100107
}

cmd/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ type UtilsCmdInterface interface {
188188
ClaimBlockReward(options types.TransactionOptions) (common.Hash, error)
189189
GetSalt(client *ethclient.Client, epoch uint32) ([32]byte, error)
190190
HandleCommitState(client *ethclient.Client, epoch uint32, seed []byte, rogueData types.Rogue) (types.CommitData, error)
191-
Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, root [32]byte) (common.Hash, error)
191+
Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error)
192192
ListAccounts() ([]accounts.Account, error)
193193
AssignAmountInWei(flagSet *pflag.FlagSet) (*big.Int, error)
194194
ExecuteTransfer(flagSet *pflag.FlagSet)

0 commit comments

Comments
 (0)