@@ -61,7 +61,7 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
61
61
return types.CommitData {}, err
62
62
}
63
63
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 )
65
65
assignedCollections , seqAllottedCollections , err := razorUtils .GetAssignedCollections (client , numActiveCollections , seed )
66
66
if err != nil {
67
67
return types.CommitData {}, err
@@ -112,15 +112,18 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
112
112
/*
113
113
Commit finally commits the data to the smart contract. It calculates the commitment to send using the merkle tree root and the seed.
114
114
*/
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 ) {
116
116
if state , err := razorUtils .GetBufferedState (client , config .BufferPercent ); err != nil || state != 0 {
117
117
log .Error ("Not commit state" )
118
118
return core .NilHash , err
119
119
}
120
120
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
+
124
127
txnOpts := razorUtils .GetTxnOpts (types.TransactionOptions {
125
128
Client : client ,
126
129
Password : account .Password ,
@@ -133,8 +136,6 @@ func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations
133
136
Parameters : []interface {}{epoch , commitmentToSend },
134
137
})
135
138
136
- log .Debugf ("Committing: epoch: %d, commitment: %s, seed: %s, account: %s" , epoch , "0x" + hex .EncodeToString (commitment ), "0x" + hex .EncodeToString (seed ), account .Address )
137
-
138
139
log .Info ("Commitment sent..." )
139
140
log .Debugf ("Executing Commit transaction with epoch = %d, commitmentToSend = %v" , epoch , commitmentToSend )
140
141
txn , err := voteManagerUtils .Commit (client , txnOpts , epoch , commitmentToSend )
@@ -145,3 +146,67 @@ func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations
145
146
log .Info ("Txn Hash: " , txnHash .Hex ())
146
147
return txnHash , nil
147
148
}
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
+ }
0 commit comments