Skip to content

Commit 3968cfc

Browse files
committed
add judgement for cb and expect option
1 parent a5cb4ed commit 3968cfc

8 files changed

Lines changed: 240 additions & 166 deletions

File tree

src/eventManager.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,28 @@ function onMessage(that,dataRes){
163163
}
164164
}
165165
}
166-
else if(data.type === "contract_event" && that.cache[data.ContractAddress] !== undefined){
167-
if(data.hasOwnProperty("ContractEventTopics")){
168-
data.ContractEventTopics.map(function(topic,index){
166+
else if (data.type === "contract_event" && that.cache[data.ContractAddress] !== undefined) {
167+
if (data.hasOwnProperty("ContractEventTopics")) {
168+
data.ContractEventTopics.map(function (topic, index) {
169169
data.ContractEventTopics[index] = "0x" + data.ContractEventTopics[index].toLowerCase();
170170
});
171171
}
172-
if(data.hasOwnProperty("ContractEventInfo") && data.ContractEventInfo !== ""){
172+
if (data.hasOwnProperty("ContractEventInfo") && data.ContractEventInfo !== "") {
173173
data.ContractEventInfo = "0x" + data.ContractEventInfo;
174174
}
175175
let key = data.ContractEventTopics[0];
176-
if(that.cache[key]){
176+
if (that.cache[key]) {
177177
let contractObj = that.cache[data.ContractAddress];
178178
let currentEvent = contractObj.options.jsonInterface.find(function (json) {
179-
return (json.type === 'event' && json.signature === '0x'+ key.replace('0x',''));
179+
return (json.type === 'event' && json.signature === '0x' + key.replace('0x', ''));
180180
});
181181
let output = contractObj._decodeEventABI(currentEvent, data);
182182
that.cache[key](null, output);
183183
// delete that.cache[key];
184184
// let keyIndex = contractObj.registeredEvent.indexOf(key);
185185
// contractObj.registeredEvent.splice(keyIndex,1);
186186
}
187-
}
187+
}
188188
}
189189

190190
function _isChainsqlType(data){

src/index.js

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ ChainsqlAPI.prototype.escrowCancel = function (sOwnerAddr, nCreateEscrowSeq) {
224224
return ripple.escrowCancel(sOwnerAddr, nCreateEscrowSeq);
225225
}
226226

227-
ChainsqlAPI.prototype.createTable = function (name, raw, opt) {
227+
ChainsqlAPI.prototype.createTable = function (name, raw, inputOpt) {
228228
validate.create(name,raw);
229-
var opt = opt ? opt : {};
229+
var opt = inputOpt ? inputOpt : {};
230230
let that = this;
231231
var confidential = false;
232232
if (opt.confidential) {
@@ -264,8 +264,9 @@ ChainsqlAPI.prototype.createTable = function (name, raw, opt) {
264264
payment.raw = crypto.aesEncrypt(secret, payment.raw).toUpperCase();
265265
payment.token = token.toUpperCase();
266266
} else {
267-
payment.raw = convertStringToHex(payment.raw)
268-
};
267+
payment.raw = convertStringToHex(payment.raw);
268+
}
269+
269270
if (payment.operationRule) {
270271
payment.operationRule = convertStringToHex(payment.operationRule);
271272
}
@@ -613,8 +614,17 @@ ChainsqlAPI.prototype.getLedgerVersion = function (cb) {
613614

614615
function handleSignedTx(ChainSQL, signed, object, resolve, reject) {
615616
var isFunction = false;
616-
if ((typeof object) == 'function')
617-
isFunction = true;
617+
let expectOpt = {expect:"send_success"};
618+
let cbCheckRet = util.checkCbOpt(object);
619+
if(cbCheckRet.status === "success") {
620+
if(cbCheckRet.type === "function") {
621+
isFunction = cbCheckRet.isFunction;
622+
} else {
623+
expectOpt.expect = cbCheckRet.expect;
624+
}
625+
} else {
626+
return reject(cbCheckRet.errMsg);
627+
}
618628

619629
var errFunc = function (error) {
620630
if (isFunction) {
@@ -636,52 +646,56 @@ function handleSignedTx(ChainSQL, signed, object, resolve, reject) {
636646
} else {
637647
resolve(data);
638648
}
639-
}
649+
};
640650
// subscribe event
641-
ChainSQL.event.subscribeTx(signed.id, isFunction ? object : function (err, data) {
642-
if (err) {
643-
errFunc(err);
644-
} else {
645-
// success
646-
if (object != undefined
647-
&& object.expect == data.status
648-
&& data.type === 'singleTransaction') {
649-
sucFunc({
650-
status: object.expect,
651-
tx_hash: signed.id
652-
});
653-
}
654-
655-
// failure
656-
if (util.checkSubError(data)) {
657-
var error = {
658-
status: data.status,
659-
tx_hash: signed.id
651+
if(expectOpt.expect !== "send_success") {
652+
ChainSQL.event.subscribeTx(signed.id, isFunction ? object : function (err, data) {
653+
if (err) {
654+
errFunc(err);
655+
} else {
656+
// success
657+
if (expectOpt.expect === data.status
658+
&& data.type === 'singleTransaction') {
659+
sucFunc({
660+
status: expectOpt.expect,
661+
tx_hash: signed.id
662+
});
660663
}
661-
if (data.hasOwnProperty("error_message")) {
662-
error.error_message = data.error_message;
664+
665+
// failure
666+
if (util.checkSubError(data)) {
667+
var error = {
668+
status: data.status,
669+
tx_hash: signed.id
670+
};
671+
if (data.hasOwnProperty("error_message")) {
672+
error.error_message = data.error_message;
673+
}
674+
errFunc(error);
663675
}
664-
errFunc(error);
665676
}
666-
}
667-
}).then(function (data) {
668-
// subscribeTx success
669-
}).catch(function (error) {
670-
// subscribeTx failure
671-
errFunc('subscribeTx exception.' + error);
672-
});
677+
}).then(function (data) {
678+
// subscribeTx success
679+
}).catch(function (error) {
680+
// subscribeTx failure
681+
errFunc('subscribeTx exception.' + error);
682+
});
683+
}
684+
673685

674686
// submit transaction
675687
ChainSQL.api.submit(signed.signedTransaction).then(function (result) {
676688
//console.log('submit ', JSON.stringify(result));
677689
if (result.resultCode != 'tesSUCCESS') {
678-
ChainSQL.event.unsubscribeTx(signed.id);
690+
if(expectOpt.expect !== "send_success") {
691+
ChainSQL.event.unsubscribeTx(signed.id);
692+
}
679693
//return error message
680694
errFunc(result);
681695
} else {
682696
// submit successfully
683-
if ((isFunction == false && object != undefined && object.expect == 'send_success') || object == undefined) {
684-
ChainSQL.event.unsubscribeTx(signed.id);
697+
if (expectOpt.expect === "send_success") {
698+
// ChainSQL.event.unsubscribeTx(signed.id);
685699
sucFunc({
686700
status: 'send_success',
687701
tx_hash: signed.id
@@ -873,7 +887,7 @@ ChainsqlAPI.prototype.getBySqlUser = function(sql){
873887
ChainsqlAPI.prototype.submit = function (cb) {
874888
var that = this;
875889
if (that.transaction) {
876-
throw new Error('you are now in transaction,can not be submit')
890+
throw new Error('you are now in transaction,can not be submit');
877891
} else {
878892

879893
//if (cb === undefined || cb === null) {

src/smartContract.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const chainsqlUtils = require('./util');
77
var abi = require('web3-eth-abi');
88
var utils = require('web3-utils');
99
var formatters = require('web3-core-helpers').formatters;
10-
const util = require('./util');
10+
1111
const preDefOptions = ["ContractData", "arguments", "ContractValue", "Gas", "expect"];
1212
/**
1313
* Contract constructor for creating new contract instance
@@ -674,7 +674,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
674674
else {
675675
sendTxPayment.ContractOpType = 2;
676676
if(args.options.hasOwnProperty("expect")) {
677-
if(args.options.expect === "send_success" || args.options.expect === "validate_success" || args.options.expect === "db_success") {
677+
if(chainsqlUtils.checkExpect(args.options)) {
678678
txCallbackProperty.callbackExpect = args.options.expect;
679679
}
680680
else {
@@ -842,7 +842,7 @@ function submitContractTx(contractObj, signedVal, callbackProperty, resolve, rej
842842
}
843843
}
844844
// failure
845-
if (util.checkSubError(data)) {
845+
if (chainsqlUtils.checkSubError(data)) {
846846
if (data.hasOwnProperty("error_message")) {
847847
resultObj.error_message = data.error_message;
848848
}
@@ -861,7 +861,9 @@ function submitContractTx(contractObj, signedVal, callbackProperty, resolve, rej
861861
chainSQL.api.submit(signedVal.signedTransaction).then(function(result) {
862862
//console.log('submit ', JSON.stringify(result));
863863
if (result.resultCode !== 'tesSUCCESS') {
864-
unsubscribeTx(callbackProperty.callbackExpect, chainSQL, signedVal, errFunc);
864+
if(callbackProperty.callbackExpect !== "send_success"){
865+
unsubscribeTx(callbackProperty.callbackExpect, chainSQL, signedVal, errFunc);
866+
}
865867
//return error message
866868
errFunc(result);
867869
} else {

src/submit.js

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,16 @@ Submit.prototype.setMaxLedgerVersionOffset = function (maxLedgerVersionOffset) {
4040

4141
Submit.prototype.handleSignedTx = function (ChainSQL, signed, object, resolve, reject) {
4242
var isFunction = false;
43-
var type = typeof( object );
44-
if (type == 'function') {
45-
isFunction = true;
46-
}
47-
else if (type === "object") {
48-
let errMsg;
49-
if(Object.getOwnPropertyNames(object).length === 1 && object.hasOwnProperty("expect")) {
50-
if(object.expect !== "send_success" && object.expect !== "validate_success" && object.expect !== "db_success") {
51-
errMsg = "Unknown 'expect' value, please check!";
52-
return reject(errMsg);
53-
}
54-
}
55-
else {
56-
errMsg = "submit option is wrong, please check!";
57-
return reject(errMsg);
43+
let expectOpt = {expect:"send_success"};
44+
let cbCheckRet = util.checkCbOpt(object);
45+
if(cbCheckRet.status === "success") {
46+
if(cbCheckRet.type === "function") {
47+
isFunction = cbCheckRet.isFunction;
48+
} else {
49+
expectOpt.expect = cbCheckRet.expect;
5850
}
51+
} else {
52+
return reject(cbCheckRet.errMsg);
5953
}
6054

6155
var errFunc = function (error) {
@@ -78,52 +72,56 @@ Submit.prototype.handleSignedTx = function (ChainSQL, signed, object, resolve, r
7872
} else {
7973
resolve(data);
8074
}
81-
}
82-
// subscribe event
83-
ChainSQL.event.subscribeTx(signed.id, isFunction ? object : function (err, data) {
84-
if (err) {
85-
errFunc(err);
86-
} else {
87-
// success
88-
if (object != undefined
89-
&& object.expect == data.status
90-
&& data.type === 'singleTransaction') {
91-
sucFunc({
92-
status: object.expect,
93-
tx_hash: signed.id
94-
});
95-
}
75+
};
9676

97-
// failure
98-
if (util.checkSubError(data)){
99-
var error = {
100-
status: data.status,
101-
tx_hash: signed.id
77+
if (expectOpt.expect !== "send_success") {
78+
// subscribe event
79+
ChainSQL.event.subscribeTx(signed.id, isFunction ? object : function (err, data) {
80+
if (err) {
81+
errFunc(err);
82+
} else {
83+
// success
84+
if (object != undefined
85+
&& object.expect == data.status
86+
&& data.type === 'singleTransaction') {
87+
sucFunc({
88+
status: object.expect,
89+
tx_hash: signed.id
90+
});
10291
}
103-
if (data.hasOwnProperty("error_message")) {
104-
error.error_message = data.error_message;
92+
93+
// failure
94+
if (util.checkSubError(data)) {
95+
var error = {
96+
status: data.status,
97+
tx_hash: signed.id
98+
};
99+
if (data.hasOwnProperty("error_message")) {
100+
error.error_message = data.error_message;
101+
}
102+
errFunc(error);
105103
}
106-
errFunc(error);
107104
}
108-
}
109-
}).then(function (data) {
110-
// subscribeTx success
111-
}).catch(function (error) {
112-
// subscribeTx failure
113-
errFunc('subscribeTx exception.' + error);
114-
});
105+
}).then(function (data) {
106+
// subscribeTx success
107+
}).catch(function (error) {
108+
// subscribeTx failure
109+
errFunc('subscribeTx exception.' + error);
110+
});
111+
}
115112

116113
// submit transaction
117114
ChainSQL.api.submit(signed.signedTransaction).then(function (result) {
118115
//console.log('submit ', JSON.stringify(result));
119-
if (result.resultCode != 'tesSUCCESS') {
120-
ChainSQL.event.unsubscribeTx(signed.id);
116+
if (result.resultCode !== 'tesSUCCESS') {
117+
if(expectOpt.expect !== "send_success") {
118+
ChainSQL.event.unsubscribeTx(signed.id);
119+
}
121120
//return error message
122121
errFunc(result);
123122
} else {
124123
// submit successfully
125-
if ((isFunction == false && object != undefined && object.expect == 'send_success') || object == undefined){
126-
ChainSQL.event.unsubscribeTx(signed.id);
124+
if (object.expect === 'send_success') {
127125
sucFunc({
128126
status: 'send_success',
129127
tx_hash: signed.id

0 commit comments

Comments
 (0)