Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add submit bitcoin solution new methods #486

Merged
merged 22 commits into from
Mar 2, 2018

Conversation

martinmedina
Copy link
Contributor

@martinmedina martinmedina commented Feb 19, 2018

New methods to support submission of merged mining solution:

  • submitBitcoinBlockTransactions
  • submitBitcoinBlockPartialMerkle

Both receive less information than old submitBitcoinBlock method which is still available for backward compatibility reasons.

Copy link
Contributor

@colltoaction colltoaction left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comments are mostly style issues, but looks good overall.

Regarding functionality, I would appreciate if someone who knew this better could review the mnr_ methods implementations.

* Full responsibility for processing the request is delegated to MinerServer.
*
* @author Adrian Eidelman
* @author Martin Medina
*/
public class Web3RskImpl extends Web3Impl {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider extracting a Web3MnrModule like here: #483 (in a separate PR!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created to tackle this in a near future


int rskTagPosition = Collections.lastIndexOfSubList(coinbaseAsByteList, rskTagAsByteList);
byte[] blockHashForMergedMiningArray = new byte[Keccak256Helper.Size.S256.getValue()/8];
System.arraycopy(coinbaseAsByteArray, rskTagPosition+ RskMiningConstants.RSK_TAG.length, blockHashForMergedMiningArray, 0, blockHashForMergedMiningArray.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Arrays.copyOf.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elements needs to be copied into a certain position of an existing array and that can not be done with Arrays.copyOf.


co.rsk.bitcoinj.core.NetworkParameters params = co.rsk.bitcoinj.params.RegTestParams.get();

NetworkParameters params = co.rsk.bitcoinj.params.RegTestParams.get();
new co.rsk.bitcoinj.core.Context(params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this dangling new co.rsk.bitcoinj.core.Context necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, fixed!

}

public SubmittedBlockInfo mnr_submitBitcoinSolution(String blockHashHex, String blockHeaderHex, String coinbaseHex, String txnHashesHex) {
if (logger.isDebugEnabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using if (logger.isDebugEnabled()) is an antipattern when your log statement doesn't do any work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

private SubmittedBlockInfo parseResultAndReturn(SubmitBlockResult result) {
if("OK".equals(result.getStatus())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about result.isSuccessful()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a method of the SubmitBlockResult object ?

@@ -32,6 +33,11 @@

boolean isRunning();

SubmitBlockResult submitBitcoinSolution(String blockHashForMergedMining,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent correctly and remove fully-qualified name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

BtcBlock blockWithHeaderOnly,
BtcTransaction coinbase,
List<String> txHashes) {
logger.debug("Received solution with hash {} for merged mining", blockHashForMergedMining);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All your logs are debug, but maybe you should consider using info or trace


private List<String> parseTransactionHashes(String txnHashesHex) {
List<String> txnHashes = new ArrayList<>();
for (int start = 0; start < txnHashesHex.length(); start += 64) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a constant for 64

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since a last minute optimization this code no longer exists


public SubmitBlockResult submitBitcoinBlock(String blockHashForMergedMining, co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock, boolean lastTag) {
SubmitBlockResult submitBitcoinBlock(String blockHashForMergedMining, BtcBlock bitcoinMergedMiningBlock, boolean lastTag) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay to keep it package private

@martinmedina martinmedina force-pushed the feature/add-submit-bitcoin-solution-method branch from cfd5741 to 534100b Compare February 20, 2018 05:59
@martinmedina martinmedina requested a review from juli February 21, 2018 21:55
@martinmedina martinmedina changed the title Feature/add submit bitcoin solution method Feature/add submit bitcoin solution new methods Feb 21, 2018

// bits indicates which nodes are going to be used for building the partial merkle tree
// for more information please refer to {@link co.rsk.bitcoinj.core.PartialMerkleTree#buildFromLeaves } method
byte[] bits = new byte[(int) Math.ceil(bitList.size() / 8.0)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(bitList.size() + 7) / 8 with integer arithmetic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea! change done.

byte[] bitvector = new byte[(int) Math.ceil(txs.size() / 8.0)];
co.rsk.bitcoinj.core.Utils.setBitLE(bitvector, 0);
return co.rsk.bitcoinj.core.PartialMerkleTree.buildFromLeaves(bitcoinMergedMiningBlock.getParams(), bitvector, txHashes);
byte[] bitvector = new byte[(int) Math.ceil(txHashes.size() / 8.0)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

donequis
donequis previously approved these changes Feb 28, 2018
@martinmedina martinmedina force-pushed the feature/add-submit-bitcoin-solution-method branch from bbd3de0 to ab52e54 Compare February 28, 2018 20:25
@rskops
Copy link
Collaborator

rskops commented Feb 28, 2018

SonarQube analysis reported 40 issues

  • CRITICAL 2 critical
  • MAJOR 16 major
  • MINOR 10 minor
  • INFO 12 info

Top 10 issues

  1. CRITICAL MinerServerImpl.java#L314: Make the enclosing method "static" or remove this set. rule
  2. CRITICAL MinerServerImpl.java#L318: Make the enclosing method "static" or remove this set. rule
  3. MAJOR MinerServerImpl.java#L121: Constructor has 8 parameters, which is greater than 7 authorized. rule
  4. MAJOR MinerServerImpl.java#L287: Either log or rethrow this exception. rule
  5. MAJOR MinerServerImpl.java#L288: Return an empty array instead of null. rule
  6. MAJOR MinerServerImpl.java#L313: Incorrect lazy initialization of static field co.rsk.mine.MinerServerImpl.privKey0 in co.rsk.mine.MinerServerImpl.generateFallbackBlock() rule
  7. MAJOR MinerServerImpl.java#L314: java/io/File.(Ljava/lang/String;Ljava/lang/String;)V reads a file whose location might be specified by user input rule
  8. MAJOR MinerServerImpl.java#L317: Incorrect lazy initialization of static field co.rsk.mine.MinerServerImpl.privKey1 in co.rsk.mine.MinerServerImpl.generateFallbackBlock() rule
  9. MAJOR MinerServerImpl.java#L318: java/io/File.(Ljava/lang/String;Ljava/lang/String;)V reads a file whose location might be specified by user input rule
  10. MAJOR MinerUtils.java#L69: Define and throw a dedicated exception instead of using a generic one. rule

@aeidelman aeidelman merged commit 0443acb into master Mar 2, 2018
@aeidelman aeidelman deleted the feature/add-submit-bitcoin-solution-method branch March 2, 2018 11:40
@aeidelman aeidelman added this to the Bamboo v0.4.1 milestone Mar 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants