public static bool openChannel(byte[] _openRequest)
        {
            byte[] tmpChannelId                     = getTmpChannelId();
            LedgerStruct.Channel       c            = LedgerStruct.getChannelMap(tmpChannelId);
            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile0 = peerProfiles[0];
            LedgerStruct.PeerProfile   peerProfile1 = peerProfiles[1];
            byte[][] peerAddrs =
            {
                peerProfile0.peerAddr,
                peerProfile1.peerAddr
            };
            BigInteger[] amounts =
            {
                peerProfile0.deposit,
                peerProfile1.deposit
            };

            PbEntity.TokenInfo token = c.token;
            LedgerStruct.setChannelMap(tmpChannelId, c);
            OpenChannelEvent(
                tmpChannelId,
                token.tokenType,
                token.address,
                peerAddrs,
                amounts
                );
            return(true);
        }
        public static bool snapshotStatesMockSet(byte[][] _channelIds, byte[][] _peerFroms, BigInteger[] _seqNums, BigInteger[] _transferOuts, BigInteger[] _pendingPayOuts)
        {
            BasicMethods.assert(_channelIds.Length == _peerFroms.Length &&
                                _peerFroms.Length == _seqNums.Length &&
                                _seqNums.Length == _transferOuts.Length &&
                                _transferOuts.Length == _pendingPayOuts.Length,
                                "Parameter length not the same");

            for (int i = 0; i < _channelIds.Length; i++)
            {
                BasicMethods.assert(BasicMethods._isByte32(_channelIds[i]), "_channelIds " + i + " illegal");
                BasicMethods.assert(BasicMethods._isLegalAddress(_peerFroms[i]), "_peerFroms " + i + " illegal");
                BasicMethods.assert(_seqNums[i] >= 0, "_seqNums " + i + " illegal");
                BasicMethods.assert(_transferOuts[i] >= 0, "_transferOuts " + i + " illegal");
                BasicMethods.assert(_pendingPayOuts[i] >= 0, "_pendingPayOuts " + i + " illegal");

                LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelIds[i]);
                byte peerFromId        = LedgerChannel._getPeerId(c, _peerFroms[i]);
                LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
                LedgerStruct.PeerProfile   peerProfile  = peerProfiles[peerFromId];
                LedgerStruct.PeerState     state        = peerProfile.state;
                state.seqNum             = _seqNums[i];
                state.transferOut        = _transferOuts[i];
                state.pendingPayOut      = _pendingPayOuts[i];
                peerProfile.state        = state;
                peerProfiles[peerFromId] = peerProfile;
                c.peerProfiles           = peerProfiles;
                LedgerStruct.setChannelMap(_channelIds[i], c);
            }
            setTmpChannelIdSet(_channelIds);
            return(true);
        }
        public static bool intendSettleMockSet(byte[] _channelId, byte[] _peerFrom, BigInteger _seqNum, BigInteger _transferOut, byte[] _nextPayIdListHash, BigInteger _lastPayResolveDeadline, BigInteger _pendingPayOut)
        {
            BasicMethods.assert(BasicMethods._isByte32(_channelId), "_channelId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_peerFrom), "_peerFrom illegal");
            BasicMethods.assert(_seqNum >= 0, "_seqNum smaller than zero");
            BasicMethods.assert(_transferOut >= 0, "_transferOut smaller than zero");
            BasicMethods.assert(BasicMethods._isByte32(_nextPayIdListHash), "_nextPayIdListHash illegal");
            BasicMethods.assert(_lastPayResolveDeadline >= 0, "_lastPayResolveDeadline smaller than zero");
            BasicMethods.assert(_pendingPayOut >= 0, "_pendingPayOut smaller than zero");

            LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelId);
            byte peerFromId        = LedgerChannel._getPeerId(c, _peerFrom);

            BasicMethods.assert(peerFromId == 0 || peerFromId == 1, "peerFromId illegal");
            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile  = peerProfiles[peerFromId];
            LedgerStruct.PeerState     state        = peerProfile.state;

            state.seqNum                 = _seqNum;
            state.transferOut            = _transferOut;
            state.nextPayIdListHash      = _nextPayIdListHash;
            state.lastPayResolveDeadline = _lastPayResolveDeadline;
            state.pendingPayOut          = _pendingPayOut;
            peerProfile.state            = state;
            peerProfiles[peerFromId]     = peerProfile;
            c.peerProfiles               = peerProfiles;

            c = _updateOverallStatesByIntendState(c);
            LedgerStruct.setChannelMap(_channelId, c);
            setTmpChannelId(_channelId);
            return(true);
        }
        private static LedgerStruct.Channel _resetDuplexState(LedgerStruct.Channel c)
        {
            c.settleFinalizedTime = 0;
            LedgerStruct.ChannelStatus channelStatus = LedgerStruct.getStandardChannelStatus();
            c = _updateChannelStatus(c, channelStatus.Operable);
            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile0 = peerProfiles[0];
            LedgerStruct.PeerProfile   peerProfile1 = peerProfiles[1];
            LedgerStruct.PeerState     state        = peerProfile0.state;
            state.seqNum                 = 0;
            state.transferOut            = 0;
            state.nextPayIdListHash      = null;
            state.lastPayResolveDeadline = 0;
            state.pendingPayOut          = 0;
            peerProfile0.state           = state;
            state                        = peerProfile1.state;
            state.seqNum                 = 0;
            state.transferOut            = 0;
            state.nextPayIdListHash      = null;
            state.lastPayResolveDeadline = 0;
            state.pendingPayOut          = 0;
            peerProfile1.state           = state;
            peerProfiles[0]              = peerProfile0;
            peerProfiles[1]              = peerProfile1;
            c.peerProfiles               = peerProfiles;

            // reset possibly remaining WithdrawIntent freezed by previous intendSettle()
            LedgerStruct.WithdrawIntent intent = c.withdrawIntent;
            intent.receiver           = null;
            intent.amount             = 0;
            intent.requestTime        = 0;
            intent.recipientChannelId = null;
            c.withdrawIntent          = intent;
            return(c);
        }
 public static BigInteger getTotalBalanceInner(LedgerStruct.Channel _c)
 {
     LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
     BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
     LedgerStruct.PeerProfile peer0 = peerProfiles[0];
     LedgerStruct.PeerProfile peer1 = peerProfiles[1];
     return(peer0.deposit + peer1.deposit - peer0.withdrawal - peer1.withdrawal);
 }
 public static bool _isPeer(LedgerStruct.Channel _c, byte[] _addr)
 {
     BasicMethods.assert(BasicMethods._isLegalAddress(_addr), "_fromLedgerAddr parameter error");
     LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
     BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
     LedgerStruct.PeerProfile peer0 = peerProfiles[0];
     LedgerStruct.PeerProfile peer1 = peerProfiles[1];
     return(_addr.Equals(peer0.peerAddr) || _addr.Equals(peer1.peerAddr));
 }
 public static BigInteger[] _getStateSeqNums(LedgerStruct.Channel _c)
 {
     LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
     BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
     LedgerStruct.PeerProfile peer0      = peerProfiles[0];
     LedgerStruct.PeerProfile peer1      = peerProfiles[1];
     LedgerStruct.PeerState   peerState0 = peer0.state;
     LedgerStruct.PeerState   peerState1 = peer1.state;
     return(new BigInteger[] { peerState0.seqNum, peerState1.seqNum });
 }
    public static LedgerStruct.Channel _addWithdrawal(LedgerStruct.Channel _c, byte[] _receiver, BigInteger _amount)
    {
        BasicMethods.assert(BasicMethods._isLegalAddress(_receiver), "_receiver is illegal");
        BasicMethods.assert(_amount >= 0, "_amount is negative");
        byte rid = _getPeerId(_c, _receiver);

        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        LedgerStruct.PeerProfile   peerProfile  = peerProfiles[rid];
        peerProfile.withdrawal = peerProfile.withdrawal + _amount;
        peerProfiles[rid]      = peerProfile;
        BasicMethods.assert(getTotalBalanceInner(_c) >= 0, "Total balance is negative");
        return(_c);
    }
 public static LedgerStruct.BalanceMap getBalanceMapInner(LedgerStruct.Channel _c)
 {
     LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
     BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
     LedgerStruct.PeerProfile peer0      = peerProfiles[0];
     LedgerStruct.PeerProfile peer1      = peerProfiles[1];
     LedgerStruct.BalanceMap  balanceMap = new LedgerStruct.BalanceMap();
     balanceMap.peerAddrs      = new byte[2][];
     balanceMap.deposits       = new BigInteger[2];
     balanceMap.withdrawals    = new BigInteger[2];
     balanceMap.peerAddrs[0]   = peer0.peerAddr;
     balanceMap.peerAddrs[1]   = peer1.peerAddr;
     balanceMap.deposits[0]    = peer0.deposit;
     balanceMap.deposits[1]    = peer1.deposit;
     balanceMap.withdrawals[0] = peer0.withdrawal;
     balanceMap.withdrawals[1] = peer1.withdrawal;
     return(balanceMap);
 }
    public static LedgerStruct.StateSeqNumMap getStateSeqNumMapInner(LedgerStruct.Channel _c)
    {
        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
        LedgerStruct.PeerProfile peer0      = peerProfiles[0];
        LedgerStruct.PeerProfile peer1      = peerProfiles[1];
        LedgerStruct.PeerState   peerState0 = peer0.state;
        LedgerStruct.PeerState   peerState1 = peer1.state;

        LedgerStruct.StateSeqNumMap stateSeqNumMap = new LedgerStruct.StateSeqNumMap();
        stateSeqNumMap.peerAddr = new byte[2][];
        stateSeqNumMap.seqNum   = new BigInteger[2];
        byte[][] peerAddr = stateSeqNumMap.peerAddr;
        peerAddr[0] = peer0.peerAddr;
        peerAddr[1] = peer1.peerAddr;
        BigInteger[] seqNum = stateSeqNumMap.seqNum;
        seqNum[0] = peerState0.seqNum;
        seqNum[1] = peerState1.seqNum;
        return(stateSeqNumMap);
    }
    public static LedgerStruct.LastPayResolveDeadlineMap getLastPayResolveDeadlineMapInner(LedgerStruct.Channel _c)
    {
        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
        LedgerStruct.PeerProfile peer0      = peerProfiles[0];
        LedgerStruct.PeerProfile peer1      = peerProfiles[1];
        LedgerStruct.PeerState   peerState0 = peer0.state;
        LedgerStruct.PeerState   peerState1 = peer1.state;

        LedgerStruct.LastPayResolveDeadlineMap lastPayResolveDeadlineMap = new LedgerStruct.LastPayResolveDeadlineMap();
        lastPayResolveDeadlineMap.peerAddr = new byte[2][];
        lastPayResolveDeadlineMap.lastPayResolveDeadline = new BigInteger[2];
        byte[][]     peerAddr = lastPayResolveDeadlineMap.peerAddr;
        BigInteger[] lastPayResolveDeadline = lastPayResolveDeadlineMap.lastPayResolveDeadline;
        peerAddr[0] = peer0.peerAddr;
        peerAddr[1] = peer1.peerAddr;
        lastPayResolveDeadline[0] = peerState0.lastPayResolveDeadline;
        lastPayResolveDeadline[1] = peerState1.lastPayResolveDeadline;
        return(lastPayResolveDeadlineMap);
    }
    public static LedgerStruct.PendingPayOutMap getPendingPayOutMapInner(LedgerStruct.Channel _c)
    {
        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
        LedgerStruct.PeerProfile peer0      = peerProfiles[0];
        LedgerStruct.PeerProfile peer1      = peerProfiles[1];
        LedgerStruct.PeerState   peerState0 = peer0.state;
        LedgerStruct.PeerState   peerState1 = peer1.state;

        LedgerStruct.PendingPayOutMap pendingPayOutMap = new LedgerStruct.PendingPayOutMap();
        pendingPayOutMap.peerAddr      = new byte[2][];
        pendingPayOutMap.pendingPayOut = new BigInteger[2];
        byte[][] peerAddr = pendingPayOutMap.peerAddr;
        peerAddr[0] = peer0.peerAddr;
        peerAddr[1] = peer1.peerAddr;
        BigInteger[] pendingPayOut = pendingPayOutMap.pendingPayOut;
        pendingPayOut[0] = peerState0.pendingPayOut;
        pendingPayOut[1] = peerState1.pendingPayOut;
        return(pendingPayOutMap);
    }
    public static LedgerStruct.NextPayIdListHashMap getNextPayIdListHashMapInner(LedgerStruct.Channel _c)
    {
        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
        LedgerStruct.PeerProfile peer0      = _c.peerProfiles[0];
        LedgerStruct.PeerProfile peer1      = _c.peerProfiles[1];
        LedgerStruct.PeerState   peerState0 = peer0.state;
        LedgerStruct.PeerState   peerState1 = peer1.state;

        LedgerStruct.NextPayIdListHashMap nextPayIdListHashMap = new LedgerStruct.NextPayIdListHashMap();
        nextPayIdListHashMap.peerAddr          = new byte[2][];
        nextPayIdListHashMap.nextPayIdListHash = new byte[2][];
        byte[][] peerAddr = nextPayIdListHashMap.peerAddr;
        peerAddr[0] = peer0.peerAddr;
        peerAddr[1] = peer1.peerAddr;
        byte[][] nextPayIdListHash = nextPayIdListHashMap.nextPayIdListHash;
        nextPayIdListHash[0] = peerState0.nextPayIdListHash;
        nextPayIdListHash[1] = peerState1.nextPayIdListHash;
        return(nextPayIdListHashMap);
    }
 public static byte _getPeerId(LedgerStruct.Channel _c, byte[] _peer)
 {
     BasicMethods.assert(BasicMethods._isLegalAddress(_peer), "_fromLedgerAddr parameter error");
     LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
     BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
     LedgerStruct.PeerProfile peer0 = peerProfiles[0];
     LedgerStruct.PeerProfile peer1 = peerProfiles[1];
     if (_peer.Equals(peer0.peerAddr))
     {
         return(0);
     }
     else if (_peer.Equals(peer1.peerAddr))
     {
         return(1);
     }
     else
     {
         BasicMethods.assert(false, "Nonexist peer");
     }
     return(2);
 }
    public static LedgerStruct.Channel _importPeersMigrationInfo(LedgerStruct.Channel _c, byte[] _fromLedgerAddr, byte[] _channelId)
    {
        BasicMethods.assert(BasicMethods._isLegalAddress(_fromLedgerAddr), "invalid contract address");
        BasicMethods.assert(BasicMethods._isByte32(_channelId), "invalid _channelId");
        object[]            input   = new object[] { _channelId };
        DynamicCallContract dyncall = (DynamicCallContract)_fromLedgerAddr.ToDelegate();

        LedgerStruct.PeersMigrationInfo args = (LedgerStruct.PeersMigrationInfo)dyncall("getPeersMigrationInfo", input);
        byte[][]     peerAddr      = args.peerAddr;
        BigInteger[] deposit       = args.deposit;
        BigInteger[] withdrawal    = args.withdrawal;
        BigInteger[] seqNum        = args.seqNum;
        BigInteger[] transferOut   = args.transferOut;
        BigInteger[] pendingPayout = args.pendingPayout;
        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        for (int i = 0; i < 2; i++)
        {
            LedgerStruct.PeerProfile originalPeerProfile = peerProfiles[i];
            LedgerStruct.PeerState   originaPeerState    = originalPeerProfile.state;
            LedgerStruct.PeerState   peerState           = new LedgerStruct.PeerState
            {
                seqNum                 = seqNum[i],
                transferOut            = transferOut[i],
                nextPayIdListHash      = originaPeerState.nextPayIdListHash,
                lastPayResolveDeadline = originaPeerState.lastPayResolveDeadline,
                pendingPayOut          = pendingPayout[i]
            };
            LedgerStruct.PeerProfile peerProfile = new LedgerStruct.PeerProfile()
            {
                peerAddr   = peerAddr[i],
                deposit    = deposit[i],
                withdrawal = withdrawal[i],
                state      = peerState
            };
            peerProfiles[i] = peerProfile;
        }
        return(_c);
    }
        public static bool openChannelMockSet(byte[] _channelId, BigInteger _disputeTimeout, byte[] _tokenAddress, byte _tokenType, byte[][] _peerAddrs, BigInteger[] _deposits)
        {
            BasicMethods.assert(BasicMethods._isByte32(_channelId), "_channelId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_tokenAddress), "_channelId illegal");
            BasicMethods.assert(_tokenType >= 0 && _tokenType <= 3, "_tokenType illegal");
            BasicMethods.assert(_peerAddrs.Length == 2, "_peerAddrs length illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_peerAddrs[0]), "_peerAddrs 0 illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_peerAddrs[1]), "_peerAddrs 1 illegal");
            BasicMethods.assert(_deposits.Length == 2, "_deposits length illegal");
            BasicMethods.assert(_deposits[0] >= 0, "_deposits 0 illegal");
            BasicMethods.assert(_deposits[1] >= 0, "_deposits 1 illegal");

            setTmpChannelId(_channelId);

            LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelId);
            c.disputeTimeout = _disputeTimeout;
            LedgerStruct.ChannelStatus channelStatus = LedgerStruct.getStandardChannelStatus();
            c = LedgerOperation._updateChannelStatus(c, channelStatus.Operable);
            PbEntity.TokenInfo token = new PbEntity.TokenInfo()
            {
                address   = _tokenAddress,
                tokenType = _tokenType
            };
            c.token = token;

            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile0 = peerProfiles[0];
            LedgerStruct.PeerProfile   peerProfile1 = peerProfiles[1];
            peerProfile0.peerAddr = _peerAddrs[0];
            peerProfile0.deposit  = _deposits[0];
            peerProfile1.peerAddr = _peerAddrs[1];
            peerProfile1.deposit  = _deposits[1];
            peerProfiles[0]       = peerProfile0;
            peerProfiles[1]       = peerProfile1;
            c.peerProfiles        = peerProfiles;
            LedgerStruct.setChannelMap(_channelId, c);
            return(true);
        }
    public static LedgerStruct.PeersMigrationInfo getPeersMigrationInfoInner(LedgerStruct.Channel _c)
    {
        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
        LedgerStruct.PeerProfile peer0      = peerProfiles[0];
        LedgerStruct.PeerProfile peer1      = peerProfiles[1];
        LedgerStruct.PeerState   peerState0 = peer0.state;
        LedgerStruct.PeerState   peerState1 = peer1.state;

        LedgerStruct.PeersMigrationInfo peersMigrationInfo = new LedgerStruct.PeersMigrationInfo();
        peersMigrationInfo.peerAddr      = new byte[2][];
        peersMigrationInfo.deposit       = new BigInteger[2];
        peersMigrationInfo.withdrawal    = new BigInteger[2];
        peersMigrationInfo.seqNum        = new BigInteger[2];
        peersMigrationInfo.transferOut   = new BigInteger[2];
        peersMigrationInfo.pendingPayout = new BigInteger[2];

        byte[][] peerAddr = peersMigrationInfo.peerAddr;
        peerAddr[0] = peer0.peerAddr;
        peerAddr[1] = peer1.peerAddr;
        BigInteger[] deposit = peersMigrationInfo.deposit;
        deposit[0] = peer0.deposit;
        deposit[1] = peer1.deposit;
        BigInteger[] withdrawal = peersMigrationInfo.withdrawal;
        withdrawal[0] = peer0.withdrawal;
        withdrawal[1] = peer1.withdrawal;
        BigInteger[] seqNum = peersMigrationInfo.seqNum;
        seqNum[0] = peerState0.seqNum;
        seqNum[1] = peerState1.seqNum;
        BigInteger[] transferOut = peersMigrationInfo.transferOut;
        transferOut[0] = peerState0.transferOut;
        transferOut[1] = peerState1.transferOut;
        BigInteger[] pendingPayout = peersMigrationInfo.pendingPayout;
        pendingPayout[0] = peerState0.pendingPayOut;
        pendingPayout[1] = peerState1.pendingPayOut;

        return(peersMigrationInfo);
    }
    public static LedgerStruct.SettleBalance _validateSettleBalance(LedgerStruct.Channel _c)
    {
        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
        LedgerStruct.PeerProfile   peer0         = peerProfiles[0];
        LedgerStruct.PeerProfile   peer1         = peerProfiles[1];
        LedgerStruct.PeerState     peerState0    = peer0.state;
        LedgerStruct.PeerState     peerState1    = peer1.state;
        LedgerStruct.SettleBalance settleBalance = new LedgerStruct.SettleBalance();
        settleBalance.balance = new BigInteger[2];
        BigInteger[] balance = settleBalance.balance;
        balance[0] = peer0.deposit + peerState1.transferOut;
        balance[1] = peer1.deposit + peerState0.transferOut;

        BigInteger subAmt = peerState0.transferOut + peer0.withdrawal;

        if (balance[0] < subAmt)
        {
            settleBalance.isSettled = 0;
            balance[0] = 0;
            balance[1] = 0;
            return(settleBalance);
        }
        balance[0] = balance[0] - subAmt;

        subAmt = peerState1.transferOut + peer1.withdrawal;
        if (balance[1] < subAmt)
        {
            settleBalance.isSettled = 0;
            balance[0] = 0;
            balance[1] = 0;
            return(settleBalance);
        }
        balance[1] = balance[1] - subAmt;

        settleBalance.isSettled = 1;
        return(settleBalance);
    }
        public static bool deposit(byte[] _channelId, byte[] _receiver, BigInteger _transferFromAmount)
        {
            BasicMethods.assert(BasicMethods._isByte32(_channelId), "_channelId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_receiver), "_receiver illegal");
            BasicMethods.assert(_transferFromAmount >= 0, "_transferFromAmount illegal");

            LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelId);
            byte rid = LedgerChannel._getPeerId(c, _receiver);

            BasicMethods.assert(rid == 0 || rid == 1, "rid illegal");
            PbEntity.TokenInfo token  = c.token;
            BigInteger         value  = LedgerStruct.getTransactionValue(token.tokenType, getCelerWallet());
            BigInteger         amount = _transferFromAmount + value;

            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile  = peerProfiles[rid];
            peerProfile.deposit = peerProfile.deposit + amount;
            peerProfiles[rid]   = peerProfile;
            c.peerProfiles      = peerProfiles;
            LedgerStruct.BalanceMap balanceMap = LedgerChannel.getBalanceMapInner(c);
            LedgerStruct.setChannelMap(_channelId, c);
            DepositEvent(_channelId, balanceMap.peerAddrs, balanceMap.deposits, balanceMap.withdrawals);
            return(true);
        }