Sawtooth Transaction error: "Tried to set unauthorized address"

Sawtooth Transaction error: "Tried to set unauthorized address"

我正在尝试编写自定义事务处理程序。我正在为简单的帐户写作 class

class Account:
    def __init__(self, name, ac_number, balance):
        self.name = name 
        self.ac_number = ac_number
        self.balance = balance 

我的 TP 对于单个帐户运行良好。现在我想为多个帐户改进它。为了让每个帐号获得不同的状态,我更改了 _'_get_account_address_' 函数。我正在关注@danintel 的 Cookiejar and XO_python 项目。我正在按照 xo 代码获取地址。

AC_NAMESPACE = hashlib.sha512('account'.encode("utf-8")).hexdigest()[0:6]

def _make_account_address(name):
    return AC_NAMESPACE + \
        hashlib.sha512(name.encode('utf-8')).hexdigest()[:64]  

_get_account_address 工作正常但 _make_account_address 在 cli

中显示错误

Tried to set unauthorized address

我的州代码是

import logging
import hashlib
from sawtooth_sdk.processor.exceptions import InternalError

LOGGER = logging.getLogger(__name__)


FAMILY_NAME = "account"
# TF Prefix is first 6 characters of SHA-512("cookiejar"), a4d219

AC_NAMESPACE = hashlib.sha512('account'.encode("utf-8")).hexdigest()[0:6]


def _make_account_address(name):
    return AC_NAMESPACE + \
        hashlib.sha512(name.encode('utf-8')).hexdigest()[:64]

def _hash(data):
    '''Compute the SHA-512 hash and return the result as hex characters.'''
    return hashlib.sha512(data).hexdigest()

def _get_account_address(from_key):
    '''
    Return the address of a cookiejar object from the cookiejar TF.

    The address is the first 6 hex characters from the hash SHA-512(TF name),
    plus the result of the hash SHA-512(cookiejar public key).
    '''
    return _hash(FAMILY_NAME.encode('utf-8'))[0:6] + \
                 _hash(from_key.encode('utf-8'))[0:64]

class Account:
    def __init__(self, name, ac_number, balance):
        self.name = name 
        self.ac_number = ac_number
        self.balance = balance 


class AccountState:
    def __init__(self, context):
        self._context = context 

    def make_account(self, account_obj, from_key):
        '''Bake (add) "amount" cookies.'''
        account_address = _make_account_address(account_obj.name) # not working 
        #account_address = _get_account_address(from_key) # working fine
        LOGGER.info('Got the key %s and the account address %s.',
                    from_key, account_address)

        state_str = ",".join([str(account_obj.name), str(account_obj.ac_number), str(account_obj.balance)])
        state_data = state_str.encode('utf-8')
        addresses = self._context.set_state({account_address: state_data})

        if len(addresses) < 1:
            raise InternalError("State Error")

这可能已经得到解答,但我没有多少功劳来添加评论。

您看到的错误“试图设置未经授权的地址:”是因为客户端没有在 TransactionHeader 中包含这些地址” 输出”地址字段。

客户端可以在“outputs”地址字段中给出前缀而不是完整地址,但请谨慎使用此功能,因为它会影响并行事务调度。

在编写TransactionHeader时,请参考https://sawtooth.hyperledger.org/docs/core/nightly/master/architecture/transactions_and_batches.html#dependencies-and-input-output-addresses对不同字段的详细理解

这意味着 t运行saction 处理器试图设置(放置)一个不在输出列表中的值。当客户提交一个包含 inputs/outputs.

的不准确列表的 t运行saction 时,会发生这种情况

确保 Sawtooth 地址的长度正确——地址是 70 个十六进制字符,代表一个 35 字节的地址(包括 6 个十六进制字符或 3 个字节的 T运行saction 系列前缀)。

此外,您可以将输出列表设置为空——这将允许写入所有地址(以牺牲安全性和效率为代价)。最好将输入和输出设置为您正在更改的状态地址——这允许 t运行saction 是 运行 并行的(如果您 运行 sawtooth-validator --scheduler parallel -vv )和更安全可靠,因为 t运行saction 处理器无法写入列表之外的状态地址。