Add sendTransaction method to the daemon.

This commit is contained in:
Braydon Fuller 2015-07-20 14:43:21 -04:00
parent 6cc6d7f4de
commit 796d7682f8
3 changed files with 64 additions and 2 deletions

View File

@ -167,7 +167,6 @@ describe('Basic Functionality', function() {
var expected = tx.toBuffer().toString('hex');
txBuffer.toString('hex').should.equal(expected);
});
});
it('get outputs by address', function() {

View File

@ -1037,6 +1037,66 @@ NAN_METHOD(GetInfo) {
NanReturnValue(obj);
}
/**
* Send Transaction
* bitcoindjs.sendTransaction()
* Will add a transaction to the mempool and broadcast to connected peers.
* @param {string} - The serialized hex string of the transaction.
* @param {boolean} - Skip absurdly high fee checks
*/
NAN_METHOD(SendTransaction) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
LOCK(cs_main);
// Decode the transaction
v8::String::Utf8Value param1(args[0]->ToString());
std::string *input = new std::string(*param1);
CTransaction tx;
if (!DecodeHexTx(tx, *input)) {
return NanThrowError("TX decode failed");
}
uint256 hashTx = tx.GetHash();
// Skip absurdly high fee check
bool allowAbsurdFees = false;
if (args.Length() > 1) {
allowAbsurdFees = args[1]->BooleanValue();
}
CCoinsViewCache &view = *pcoinsTip;
const CCoins* existingCoins = view.AccessCoins(hashTx);
bool fHaveMempool = mempool.exists(hashTx);
bool fHaveChain = existingCoins && existingCoins->nHeight < 1000000000;
if (!fHaveMempool && !fHaveChain) {
CValidationState state;
bool fMissingInputs;
char *errorMessage;
// Attempt to add the transaction to the mempool
if (!AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !allowAbsurdFees)) {
if (state.IsInvalid()) {
sprintf(errorMessage, "%i: %s", state.GetRejectCode(), state.GetRejectReason().c_str());
return NanThrowError(errorMessage);
} else {
if (fMissingInputs) {
return NanThrowError("Missing inputs");
}
sprintf(errorMessage, "%s", state.GetRejectReason());
return NanThrowError(errorMessage);
}
}
} else if (fHaveChain) {
return NanThrowError("transaction already in block chain");
}
// Relay the transaction connect peers
RelayTransaction(tx);
return NanReturnValue(NanNew<String>(hashTx.GetHex()));
}
/**
* GetMempoolOutputs
* bitcoindjs.getMempoolOutputs()
@ -1174,7 +1234,7 @@ init(Handle<Object> target) {
NODE_SET_METHOD(target, "getMempoolOutputs", GetMempoolOutputs);
NODE_SET_METHOD(target, "addMempoolUncheckedTransaction", AddMempoolUncheckedTransaction);
NODE_SET_METHOD(target, "verifyScript", VerifyScript);
NODE_SET_METHOD(target, "sendTransaction", SendTransaction);
}
NODE_MODULE(bitcoindjs, init)

View File

@ -12,6 +12,7 @@
#include "scheduler.h"
#include "core_io.h"
#include "script/bitcoinconsensus.h"
#include "consensus/validation.h"
NAN_METHOD(StartBitcoind);
NAN_METHOD(OnBlocksReady);
@ -26,3 +27,5 @@ NAN_METHOD(GetChainWork);
NAN_METHOD(GetMempoolOutputs);
NAN_METHOD(AddMempoolUncheckedTransaction);
NAN_METHOD(VerifyScript);
NAN_METHOD(SendTransaction);