Search code examples
javascriptnode.jspromisees6-promisehyperledger-sawtooth

Sawtooth transaction processor not working


I am developing a simple-wallet for deposit money webapp on hyperledger sawtooth. I want the amount to be deposited after specific time interval. For that I am using setInterval(). I have written a promise which resolves after successful setInteval(). But I am facing an issue here. My TP is not working properly. The blocks are getting added but state is not getting created.

class SimpleWalletHandler extends TransactionHandler {
    constructor(){
        super(SW_FAMILY,['1.0'],[SW_NAMESPACE]);
    }
    apply(transactionProcessRequest, context){
        let payload = transactionProcessRequest.payload;
        payload = payload.toString().split(',');
        var action, amount;
        action = payload[0];
        amount = payload[1];
        let header = transactionProcessRequest.header;
        let userPublicKey = header.signerPublicKey;
        let endMinutes = 40; //Hard-coded value. Later I am going to get this from front-end
        if(action === 'deposit'){
        //I think the issue is getting raised from now on
            let p = new Promise((resolve,reject) => {
                let timer = setInterval(()=>{
                    let startMinutes = new Date().getMinutes();
                    if(startMinutes >= endMinutes){
                        console.log('From: Alice\nTo: Bob\nAmount: $99');
                        clearInterval(timer);
                        resolve(99);
                    }
                },1000);
            })
            p.then((flag) => {
                if(flag == 99){
                    let senderAddress = SW_NAMESPACE + _hash(userPublicKey).slice(-64);
                    let strAmount = amount.toString();
                    let dataBytes = encoder.encode(strAmount);
                    let entries = {
                        [senderAddress]: dataBytes
                    }
                    return context.setState(entries)
                        .then((result) => console.log(`Success${result}`))
                        .catch((error) => console.error(`Error!${error}`));
                }
            });
            return p;
        }
    }
}

I am beginner to javascript. Please help me to solve this issue. If possible please provide complete corrected snippet. Thanks in advance :)


Solution

  • I am not a JavaScript expert, but the transaction processor must be deterministic--including time independent. That is, when a transaction is processed the same result will always occur. That's because a transaction is usually processed multiple times, when submitted, when published, and by each node.

    A better solution is to have a daemon or separate client handle the timing and submission of time-sensitive transactions (such as interest and fees, I suppose).