Пример #1
0
    def _do_trans(self, vfrom, value, vto, state):
        msg = 'transfer "{n}"->"{t}" by {v}'.format(n=vfrom, t=vto, v=value)
        LOGGER.debug(msg)

        if vfrom not in state or vto not in state:
            raise InvalidTransaction(
                'Verb is "trans" but vallet "{}" or vallet "{}" not in state'.
                format(vfrom, vto))

        curr = state[vfrom]
        token = BgtTokenInfo()
        token.ParseFromString(curr)
        to = state[vto]
        token1 = BgtTokenInfo()
        token1.ParseFromString(to)
        LOGGER.debug('_do_tans token[%s]=%s', token.group_code, value)
        decd = token.decimals - value
        if decd < MIN_VALUE:
            raise InvalidTransaction(
                'Verb is "trans", but result would be less than {}'.format(
                    MIN_VALUE))
        incd = token1.decimals + value
        if incd > MAX_VALUE:
            raise InvalidTransaction(
                'Verb is "inc", but result would be greater than {}'.format(
                    MAX_VALUE))

        updated = {k: v for k, v in state.items()}
        token.decimals = decd
        updated[vfrom] = token.SerializeToString()
        token1.decimals = incd
        updated[vto] = token1.SerializeToString()

        return updated
Пример #2
0
    def _do_dec(self, name, value, to, state):
        msg = 'Decrementing "{n}" by {v}'.format(n=name, v=value)
        LOGGER.debug(msg)

        if name not in state:
            raise InvalidTransaction(
                'Verb is "dec" but name "{}" not in state'.format(name))

        curr = state[name]
        token = BgtTokenInfo()
        token.ParseFromString(curr)
        LOGGER.debug('_do_dec token[%s]=%s', token.group_code, token.decimals,
                     value)
        decd = token.decimals - value

        if decd < MIN_VALUE:
            raise InvalidTransaction(
                'Verb is "dec", but result would be less than {}'.format(
                    MIN_VALUE))

        updated = {k: v for k, v in state.items()}
        token.decimals = decd
        updated[name] = token.SerializeToString()

        return updated