2

为什么我的事务处理器没有收到我通过其余 API 发布的请求?

我在 Golang 中构建了一个客户端和事务处理器 (TP),这与 XO 示例没有太大区别。我已成功让 TP 在本地运行到 Sawtooth 组件,并从单独的 cli 工具发送批处理列表。目前 TP 中的 apply 方法没有被命中,也没有收到我的任何交易。

编辑:为了尽可能简化和澄清我的问题,我放弃了我的原始源代码,并构建了一个更简单的客户端,为 XO sdk 示例发送事务。*

当我运行我构建的工具时,rest api 成功接收请求,处理并返回 202 响应,但似乎从批处理状态 url 中省略了批处理的 id。检查日志看起来好像验证器从未收到来自 rest api 的请求,如下面的日志所示。

sawtooth-rest-api-default | [2018-05-16 09:16:38.861 DEBUG    route_handlers] Sending CLIENT_BATCH_SUBMIT_REQUEST request to validator
sawtooth-rest-api-default | [2018-05-16 09:16:38.863 DEBUG    route_handlers] Received CLIENT_BATCH_SUBMIT_RESPONSE response from validator with status OK
sawtooth-rest-api-default | [2018-05-16 09:16:38.863 INFO     helpers] POST /batches HTTP/1.1: 202 status, 213 size, in 0.002275 s

下面是我将事务发送到本地实例的整个命令行工具。

package main

import (
    "bytes"
    "crypto/sha512"
    "encoding/base64"
    "encoding/hex"
    "flag"
    "fmt"
    "io/ioutil"
    "log"
    "math/rand"
    "net/http"
    "strings"
    "time"

    "github.com/hyperledger/sawtooth-sdk-go/protobuf/batch_pb2"
    "github.com/hyperledger/sawtooth-sdk-go/protobuf/transaction_pb2"
    "github.com/hyperledger/sawtooth-sdk-go/signing"
)

var restAPI string

func main() {
    var hostname, port string

    flag.StringVar(&hostname, "hostname", "localhost", "The hostname to host the application on (default: localhost).")
    flag.StringVar(&port, "port", "8080", "The port to listen on for connection (default: 8080)")
    flag.StringVar(&restAPI, "restAPI", "http://localhost:8008", "The address of the sawtooth REST API")

    flag.Parse()

    s := time.Now()
    ctx := signing.CreateContext("secp256k1")
    key := ctx.NewRandomPrivateKey()
    snr := signing.NewCryptoFactory(ctx).NewSigner(key)

    payload := "testing_new,create,"
    encoded := base64.StdEncoding.EncodeToString([]byte(payload))

    trn := BuildTransaction(
        "testing_new",
        encoded,
        "xo",
        "1.0",
        snr)

    trn.Payload = []byte(encoded)

    batchList := &batch_pb2.BatchList{
        Batches: []*batch_pb2.Batch{
            BuildBatch(
                []*transaction_pb2.Transaction{trn},
                snr),
        },
    }

    serialised := batchList.String()

    fmt.Println(serialised)

    resp, err := http.Post(
        restAPI+"/batches",
        "application/octet-stream",
        bytes.NewReader([]byte(serialised)),
    )

    if err != nil {
        fmt.Println("Error")
        fmt.Println(err.Error())
        return
    }

    defer resp.Body.Close()
    fmt.Println(resp.Status)
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
    elapsed := time.Since(s)
    log.Printf("Creation took %s", elapsed)

    resp.Close = true
}

// BuildTransaction will build a transaction based on the information provided
func BuildTransaction(ID, payload, familyName, familyVersion string, snr *signing.Signer) *transaction_pb2.Transaction {
    publicKeyHex := snr.GetPublicKey().AsHex()
    payloadHash := Hexdigest(string(payload))

    addr := Hexdigest(familyName)[:6] + Hexdigest(ID)[:64]

    transactionHeader := &transaction_pb2.TransactionHeader{
        FamilyName:       familyName,
        FamilyVersion:    familyVersion,
        SignerPublicKey:  publicKeyHex,
        BatcherPublicKey: publicKeyHex,
        Inputs:           []string{addr},
        Outputs:          []string{addr},
        Dependencies:     []string{},
        PayloadSha512:    payloadHash,
        Nonce:            GenerateNonce(),
    }

    header := transactionHeader.String()
    headerBytes := []byte(header)
    headerSig := hex.EncodeToString(snr.Sign(headerBytes))

    return &transaction_pb2.Transaction{
        Header:          headerBytes,
        HeaderSignature: headerSig[:64],
        Payload:         []byte(payload),
    }
}

// BuildBatch will build a batch using the provided transactions
func BuildBatch(trans []*transaction_pb2.Transaction, snr *signing.Signer) *batch_pb2.Batch {

    ids := []string{}

    for _, t := range trans {
        ids = append(ids, t.HeaderSignature)
    }

    batchHeader := &batch_pb2.BatchHeader{
        SignerPublicKey: snr.GetPublicKey().AsHex(),
        TransactionIds:  ids,
    }

    return &batch_pb2.Batch{
        Header:          []byte(batchHeader.String()),
        HeaderSignature: hex.EncodeToString(snr.Sign([]byte(batchHeader.String())))[:64],
        Transactions:    trans,
    }
}

// Hexdigest will hash the string and return the result as hex
func Hexdigest(str string) string {
    hash := sha512.New()
    hash.Write([]byte(str))
    hashBytes := hash.Sum(nil)
    return strings.ToLower(hex.EncodeToString(hashBytes))
}

// GenerateNonce will generate a random string to use
func GenerateNonce() string {
    return randStringBytesMaskImprSrc(16)
}

const (
    letterBytes   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    letterIdxBits = 6                    // 6 bits to represent a letter index
    letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
    letterIdxMax  = 63 / letterIdxBits   // # of letter indices fitting in 63 bits
)

func randStringBytesMaskImprSrc(n int) string {
    rand.Seed(time.Now().UnixNano())
    b := make([]byte, n)
    // A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
    for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
        if remain == 0 {
            cache, remain = rand.Int63(), letterIdxMax
        }
        if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
            b[i] = letterBytes[idx]
            i--
        }
        cache >>= letterIdxBits
        remain--
    }

    return string(b)
}
4

2 回答 2

2

这有很多问题,希望我可以单独解释每个问题,以帮助阐明这些事务可能失败的方式。

交易完整性

正如@Frank C. 上面的评论,我的交易标头缺少几个值。这些是地址,也是随机数。

// Hexdigest will hash the string and return the result as hex
func Hexdigest(str string) string {
    hash := sha512.New()
    hash.Write([]byte(str))
    hashBytes := hash.Sum(nil)
    return strings.ToLower(hex.EncodeToString(hashBytes))
}


addr := Hexdigest(familyName)[:6] + Hexdigest(ID)[:64]
transactionHeader := &transaction_pb2.TransactionHeader{
    FamilyName:       familyName,
    FamilyVersion:    familyVersion,
    SignerPublicKey:  publicKeyHex,
    BatcherPublicKey: publicKeyHex,
    Inputs:           []string{addr},
    Outputs:          []string{addr},
    Dependencies:     []string{},
    PayloadSha512:    payloadHash,
    Nonce:            uuid.NewV4(),
} 

追踪

接下来是在批处理中启用跟踪。

return &batch_pb2.Batch{
    Header:          []byte(batchHeader.String()),
    HeaderSignature: batchHeaderSignature,
    Transactions:    trans,
    Trace: true, // Set this flag to true
}

通过上述设置,Rest API 将解码消息以打印额外的日志记录信息,并且验证器组件将输出更多有用的日志记录。

400 Bad Request
{
"error": {
"code": 35,
"message": "The protobuf BatchList you submitted was malformed and could not be read.",
"title": "Protobuf Not Decodable"
}
}

开启跟踪后,以上内容由 Rest API 输出。这证明接收到的数据有问题。

为什么会这样?
根据 Sawtooth 聊天室的一些宝贵建议,我尝试使用 SDK 对另一种语言的批次进行反序列化。

反序列化

为了测试在另一个 SDK 中反序列化批次,我在 python 中构建了一个 web api,我可以轻松地将我的批次发送到,它可以尝试反序列化它们。

from flask import Flask, request

from protobuf import batch_pb2

app = Flask(__name__)

@app.route("/batches", methods = [ 'POST' ])
def deserialise():
    received = request.data
    print(received)
    print("\n")
    print(''.join('{:02x}'.format(x) for x in received))

    batchlist = batch_pb2.BatchList()

    batchlist.ParseFromString(received)
    return ""

if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True)

将我的批次发送到此后,我收到以下错误。

RuntimeWarning: Unexpected end-group tag: Not all data was converted

这显然是我的批次出了问题,但是看到这一切都由 Hyperledger Sawtooth Go SDK 处理,我决定转向 Python 并用它构建我的应用程序。

于 2018-05-31T09:18:12.483 回答
1

[编辑]

设法使用有关在 Go 中编写客户端的信息更新文档https://sawtooth.hyperledger.org/docs/core/nightly/master/app_developers_guide/go_sdk.html

[原答案]

回答这个问题有点晚了,希望这能帮助其他遇到 Go 客户端类似问题的人。

我最近正在为 Sawtooth 试用一个示例 Go 客户端。面临您在这里提出的类似问题,目前很难调试组合批处理列表中出现的问题。问题是缺少在客户端应用程序开发中使用 Go SDK 的示例代码和文档。

以下是有效的示例 Go 代码的链接:https ://github.com/arsulegai/contentprotection/tree/master/ContentProtectionGoClient

请查看 src/client/client.go 文件,该文件由单个 Transaction 和 Batch 组成,将其放入批处理列表并将其发送给验证器。我采用的调试方法是用另一种语言编写预期的批处理列表(针对特定事务),并将每个步骤与 Go 代码中等效步骤的结果进行比较。

谈到这个问题,除了组合交易标头中缺少信息外,其他问题可能是 protobuf 消息的序列化方式。请使用 protobuf 库进行序列化。

示例:(从@danielcooperxyz 扩展答案)

transactionHeader := &transaction_pb2.TransactionHeader{
    FamilyName:       familyName,
    FamilyVersion:    familyVersion,
    SignerPublicKey:  publicKeyHex,
    BatcherPublicKey: publicKeyHex,
    Inputs:           []string{addr},
    Outputs:          []string{addr},
    Dependencies:     []string{},
    PayloadSha512:    payloadHash,
    Nonce:            uuid.NewV4(),
}
transactionHeaderSerializedForm, _ := proto.Marshal(transactionHeader)

(protobuf 库方法可以在 github.com/golang/protobuf/proto 中找到)

于 2018-09-25T07:02:03.280 回答