今天在调试sawtooth交易族和客户端时,提交交易时返回了如下的内容:
worker.go:85: [WARN] (0cd18b24-aed9-4f71-a928-f932ebf5f4c3) Authorization exception: Tried to set unauthorized address: [8aafadc680580c71c56c36a85363679786ea1447620ef829c1cea6de3be27debd2e705]
怀疑过是密钥没有配置对或者是地址计算有问题,最终在该网站上找到了原因:https://www.cnpython.com/qa/562220
其实原因很简单,就是客户端发送交易的时候在Input也写入了一个address,交易族可能使用这个对其计算的地址进行了验证,结果发现两个值不一样,就报了上述错误,这里我的客户端代码如下:
func (broker *BrokerClient) sendTransaction(
function string, key string, value string, wait uint) (string, error) {
...
payload, err := json.Marshal(payloadData)
if err != nil {
return "", err
}
// construct the address
address := broker.getAddress(key, "regular")
fmt.Printf("send %v\n", string(payload))
fmt.Printf("get address hash %v\n", address)
// Construct TransactionHeader
rawTransactionHeader := transaction_pb2.TransactionHeader{
SignerPublicKey: broker.signer.GetPublicKey().AsHex(),
FamilyName: FAMILY_NAME,
FamilyVersion: FAMILY_VERSION,
Dependencies: []string{}, // empty dependency list
Nonce: strconv.Itoa(rand.Int()),
BatcherPublicKey: broker.signer.GetPublicKey().AsHex(),
Inputs: []string{address}, // 提交了address
Outputs: []string{address},
PayloadSha512: Sha512HashValue(string(payload)),
}
...
return broker.sendRequest(
BATCH_SUBMIT_API, batchList, CONTENT_TYPE_OCTET_STREAM, key)
}
可以看到这里input字段提交了address,问题就出在address的计算方式上面
客户端计算address的方式如下:
func (broker *BrokerClient) getAddress(name string) string {
prefix := broker.getPrefix()
nameAddress := Sha512HashValue(name)[:FAMILY_VERB_ADDRESS_LENGTH]
return prefix + nameAddress
}
而链码族中则给key加了一个前缀再计算哈希:
func (broker *BrokerClient) getAddress(name string, typeValue string) string {
prefix := broker.getPrefix()
nameAddress := Sha512HashValue(typeValue + name)[:FAMILY_VERB_ADDRESS_LENGTH]
return prefix + nameAddress
}
所以只要将客户端也改成加前缀计算的方式就行了。。。解决倒是不难,但是第一次见确实困绕了我一段时间,所以记录一下吧。