Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Updates to async.js, app.js, and display logging of output (and fix async.js) #1

Merged
merged 1 commit into from

2 participants

@domtancredi

In trying to learn more bitcoinjs-color version of the server, I went through and updated async.js and think I have it working. It works great for me locally!

@domtancredi domtancredi updates to app.js to comment out color.js; update to package.json to …
…include xmlhttprequest; update to async.js to reorder methods, debug, add logging, and display results
26fa794
@0i0
Owner

Amazing work. I'll take a better look at it soon. any chance you can make a test unit? so we can writes some tests..
like the one's described here:
https://docs.google.com/document/d/1pOaWe1fWdcUUKdyXz3rMWQneGLfTg89QaIBkS91nngE/edit#heading=h.rws30fmbhy2p

@0i0 0i0 merged commit 6e32a92 into 0i0:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 15, 2012
  1. @domtancredi

    updates to app.js to comment out color.js; update to package.json to …

    domtancredi authored
    …include xmlhttprequest; update to async.js to reorder methods, debug, add logging, and display results
This page is out of date. Refresh to see the latest.
Showing with 111 additions and 68 deletions.
  1. +3 −2 app.js
  2. +107 −66 async.js
  3. +1 −0  package.json
View
5 app.js
@@ -7,7 +7,7 @@ var fs = require('fs');
var init = require('bitcoinjs/daemon/init');
var config = init.getConfig();
-var app = module.exports = express.createServer();
+var app = module.exports = express();
var rpcClient = new RpcClient(config.jsonrpc.port, config.jsonrpc.host,
config.jsonrpc.username, config.jsonrpc.password);
@@ -23,10 +23,11 @@ rpc.on('connect', function () {
});
});
-var vm = require('vm')
+/*var vm = require('vm')
var str = fs.readFileSync(__dirname + '/color.js', 'utf8');
this.console = console
vm.runInNewContext(str, this);
+*/
// Configuration
View
173 async.js
@@ -1,107 +1,148 @@
//order based input merge by output
+var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
-function getByDefenition(prev_tx, sourceAddr){
+function mixColors(colors){
+ console.log('14: mixColors starting [colors]', colors);
+
+ outColor = 'undefined';
+ do{
+ color = colors.shift();
+ if (color != outColor && outColor != 'undefined'){
+ color = 'mixed';
+ break;
+ }
+ // outColor = prevInputColor;
+ outColor = color;
+ } while (colors.length > 0);
+
+ console.log('14: mixColors result [outColor]', outColor);
+ return outColor;
+}
+
+function getByDefinition(prev_tx, sourceAddr){
+ // console.log('12: getByDefinition [prev_tx], [sourceAddr]', prev_tx, sourceAddr);
+ // testing 81a39c41ca946d72e8b5de8ba61bbba085252a2b10f075dc1ca8d4118df4981e as color definition for d10d7127366229b341a559ddf83b53b9ac6c1b2ca792cb2e81edaf7a595d763b tx
if (
- prev_tx == "81a39c41ca946d72e8b5de8ba61bbba085252a2b10f075dc1ca8d4118df4981e"
- && sourceAddr == "1GPwJvMpB4vJPuMwHs9JDAZm9Lh66Y2qnS"
+ prev_tx == "81a39c41ca946d72e8b5de8ba61bbba085252a2b10f075dc1ca8d4118df4981e"
+ && sourceAddr == "1GPwJvMpB4vJPuMwHs9JDAZm9Lh66Y2qnS"
)
- return 'g'
+ return 'g';
else
- return 'unknown'
+ return 'unknown';
+}
+
+function getColor(input){
+ // console.log('11: getColor [input]', input);
+ if (input.type == 'coinBase')
+ return 'default';
+
+ console.log('13: getColor [color]', input);
+ color = getByDefinition(input.prev_tx,input.sourceAddr);
+ console.log('13: getColor [color]', color);
+
+ if (color != 'unknown') {
+ // console.log('coloring tx: ' + input.prev_tx);
+ console.log('13: coloring tx: ' + input.prev_tx);
+ orderedTx = colorTx(input.prev_tx);
+ console.log('13: [orderedTx]: ', orderedTx);
+ //
+ // while(orderedTx.out.length>0){
+ // debugger;
+ // out = orderedTx.out.shift();
+ // if (out.toAddr == sourceAddr){
+ // color = out.color;
+ // break;
+ // }
+ // }
+ }
+ return color;
+}
+
+function getColors(inputs){
+ var inputs_colors = [];
+
+ for (var i = inputs.length - 1; i >= 0; i--) {
+ // console.log('10: getColors [input i]:', i);
+ inputs[i].color = getColor(inputs[i]);
+ inputs_colors.push(inputs[i].color);
+ // console.log('10: inputs[i].color:', inputs[i].color);
+ }
+ return inputs_colors;
+}
+
+function setOrderedTxColors(err,orderedTx){
+ // console.log('8: setOrderedTxColors [begin]:', orderedTx);
+ for (var i = 0 , n = orderedTx.out.length; i < n; i++){
+ // console.log('9: setOrderedTxColors [orderedTx.out[i].prev_inputs]:', orderedTx.out[i].prev_inputs);
+ // do we color if prev_inputs == []?
+ var inputcolors = getColors(orderedTx.out[i].prev_inputs);
+ var color = mixColors(inputcolors);
+ orderedTx.out[i].color = color;
+ }
+ console.log('15: setOrderedTxColors [end]:', orderedTx);
+ return orderedTx;
}
function orderBasedInputMergeByOutput(tx,callback){
+ // console.log('5: orderBasedInputMergeByOutput [begin]:', tx);
+
var orderedTx = {out:[]}
- , apetite = 0
+ , appetite = 0
, tasted = 0
, eaten = 0
, err = 'none'
- for (var j = 0, n = tx.out.length; j < n ; ++ j){
- apetite = parseFloat(tx.out[j].value)
+ // console.log('6: orderBasedInputMergeByOutput [tx.out]:', tx.out);
+
+ for (var j = 0, n = tx.out.length; j < n; ++j){
+ appetite = parseFloat(tx.out[j].value)
eaten = 0;
var out =
{toAddr : tx.out[j].toAddr
- ,value : apetite
+ ,value : appetite
,prev_inputs : []
}
- while (eaten < apetite && tasted < tx.in.length){
+ while (eaten < appetite && tasted < tx.in.length){
eaten += parseFloat(tx.in[tasted].value);
out.prev_inputs.push(tx.in[tasted]);
++tasted;
}
orderedTx.out.push(out)
}
- callback(err, orderedTx);
-}
-
-function getColor(input){
- if (input.type == 'coinBase')
- return 'default'
- color = getByDefenition(input.prev_tx,input.sourceAddr);
- if (color != 'unknown')
- console.log('coloring tx: ' + input.prev_tx)
- orderedTx = colorTx(input.prev_tx);
- while(orderedTx.out.length>0){
- debugger;
- out = orderedTx.out.shift();
- if (out.toAddr == sourceAddr){
- color = out.color;
- break;
- }
- }
- return color;
-}
-
-function getColors(inputs){
- for (var i = inputs.length - 1; i >= 0; i--) {
- inputs[i].color = getColor(inputs[i]);
- }
-}
-
-function mixColors(colors){
- outColor = 'undefined';
- do{
- color = colors.shift();
- if (color != outColor && outColor != 'undefined'){
- color = 'mixed';
- break;
- }
- outColor = prevInputColor;
- } while (colors.length > 0);
- return outColor;
+ // console.log('7: orderBasedInputMergeByOutput [orderedTx.out]:', orderedTx.out);
+ callback(err, orderedTx);
}
function getTx(txId,callback){
- xhr = new XMLHttpRequest();
+ // console.log('2: getTx [begin]: ' + txId);
+
+ xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:3333/json/tx/'+txId, true);
xhr.onreadystatechange = function(e) {
- if (xhr.readyState === 4) {
- if (xhr.status === 200) {
- var data = JSON.parse(xhr.responseText);
- var err = "none"
- console.log('grabbed: ' + data.hash)
- return callback(err,data);
+ if (xhr.readyState === 4) {
+ if (xhr.status === 200) {
+ // console.log('3: getTx [json response]: ' + xhr.responseText);
+ var data = JSON.parse(xhr.responseText);
+ var err = "none";
+ // console.log('4: getTx [grabbed]: ' + data.hash);
+ return callback(err,data);
+ }
}
- }
};
xhr.send(null);
}
function colorTx(txId){
+ console.log('1: colorTx: ' + txId);
+
return getTx(txId,
function(err, tx){
- return orderBasedInputMergeByOutput(tx,function(err,orderedTx){
- for (var i = 0 , n = orderedTx.out.length; i < n; i++){
- var inputcolors = getColors(orderedTx.out[i].prev_inputs);
- var color = mixColors(inputcolors);
- orderedTx.out[i].color = color;
- }
- return orderedTx;
- })
+ return orderBasedInputMergeByOutput(tx,setOrderedTxColors)
}
)
}
-var colored = colorTx('bf2bd6aefa3263988ac27fc1dc5be0e646143363031dc0976f971a73f7a64931');
-console.log(colored);
+var txTest = 'd10d7127366229b341a559ddf83b53b9ac6c1b2ca792cb2e81edaf7a595d763b';
+var colored = colorTx(txTest);
+
+console.log('16: colored:',colored);
View
1  package.json
@@ -6,5 +6,6 @@
{ "express": ">=2.4.4"
, "jsonrpc2": ">=0.1.0"
, "jade" :">=0.0.1"
+ , "xmlhttprequest" :">=0.0.1"
}
}
Something went wrong with that request. Please try again.