Go 自定义事务处理器未接收请求

Go 自定义事务处理器未接收请求,go,hyperledger-sawtooth,Go,Hyperledger Sawtooth,为什么我的事务处理器没有收到我通过rest API发布的请求? 我在Golang中构建了一个客户机和事务处理器(TP),这与XO示例没有太大区别。我已经成功地使TP在本地运行到Sawtooth组件,并从单独的cli工具发送批处理列表。目前TP中的apply方法没有被命中,并且没有收到任何my事务 编辑:为了尽可能简化和澄清我的问题,我放弃了我原来的源代码,构建了一个更简单的客户端,为XO sdk示例发送事务。* 当我运行我构建的工具时,restapi成功地接收请求、处理并返回202响应,但似乎从

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

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

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

当我运行我构建的工具时,restapi成功地接收请求、处理并返回202响应,但似乎从batchstatusesurl中省略了批的id。检查日志时,验证程序似乎从未收到来自RESTAPI的请求,如下面的日志所示

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)
}
主程序包
进口(
“字节”
“加密/sha512”
“编码/base64”
“编码/十六进制”
“旗帜”
“fmt”
“io/ioutil”
“日志”
“数学/兰德”
“net/http”
“字符串”
“时间”
“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字符串
func main(){
变量主机名,端口字符串
flag.StringVar(&hostname,“hostname”,“localhost”,“承载应用程序的主机名(默认值:localhost)。”)
flag.StringVar(&port,“port”,“8080”,“侦听连接的端口(默认值:8080)”)
flag.StringVar(&restAPI,“restAPI”,”http://localhost:8008“,“锯齿形REST API的地址”)
flag.Parse()
s:=时间。现在()
ctx:=signing.CreateContext(“secp256k1”)
key:=ctx.NewRandomPrivateKey()
snr:=signing.NewCryptoFactory(ctx).NewSigner(密钥)
有效负载:=“测试\新建,创建,”
编码:=base64.StdEncoding.EncodeToString([]字节(有效负载))
trn:=BuildTransaction(
“测试新”,
编码的,
“xo”,
"1.0",
信噪比)
trn.Payload=[]字节(编码)
batchList:=&batch\u pb2.batchList{
批次:[]*batch\U pb2.batch{
BuildBatch(
[]*事务_pb2.事务{trn},
信噪比),
},
}
序列化:=batchList.String()
fmt.Println(连载)
resp,err:=http.Post(
restAPI+“/批”,
“应用程序/八位字节流”,
bytes.NewReader([]字节(序列化)),
)
如果错误!=零{
fmt.Println(“错误”)
fmt.Println(err.Error())
返回
}
延迟响应主体关闭()
fmt打印LN(各自状态)
body,err:=ioutil.ReadAll(resp.body)
格式打印项次(字符串(正文))
已用时间:=自开始的时间
log.Printf(“创建耗时%s”,已过)
响应关闭=真
}
//BuildTransaction将根据提供的信息构建事务
func BuildTransaction(ID、有效负载、familyName、familyVersion字符串、snr*signing.Signer)*transaction\u pb2.transaction{
publicKeyHex:=snr.GetPublicKey().AsHex()
payloadHash:=Hexdigest(字符串(有效负载))
地址:=Hexdigest(familyName)[:6]+Hexdigest(ID)[:64]
transactionHeader:=&transaction_pb2.transactionHeader{
FamilyName:FamilyName,
家庭版本:家庭版本,
签名人Publickey:publicKeyHex,
BatcherPublicKey:publicKeyHex,
输入:[]字符串{addr},
输出:[]字符串{addr},
依赖项:[]字符串{},
PayloadSha512:payloadHash,
Nonce:generateOnce(),
}
标头:=transactionHeader.String()
headerBytes:=[]字节(头)
headerSig:=十六进制编码字符串(信噪比符号(headerBytes))
返回和事务\u pb2.transaction{
标题:头字节,
HeaderSign:headerSig[:64],
有效负载:[]字节(有效负载),
}
}
//BuildBatch将使用提供的事务生成一个批
func BuildBatch(trans[]*transaction\u pb2.transaction,snr*signing.Signer)*batch\u pb2.batch{
ID:=[]字符串{}
对于u,t:=量程转换{
ids=附加(ids,t.HeaderSignature)
}
batchHeader:=&batch_pb2.batchHeader{
SignerPublicKey:snr.GetPublicKey().AsHex(),
TransactionID:ID,
}
退货和批处理\u pb2.batch{
标头:[]字节(batchHeader.String()),
HeaderSignature:hex.EncodeToString(信噪比符号([]字节(batchHeader.String()))[:64],
交易:trans,
}
}
//Hexdigest将对字符串进行散列,并以十六进制形式返回结果
func Hexdigest(str字符串)字符串{
hash:=sha512.New()
hash.Write([]字节(str))
hashBytes:=hash.Sum(nil)
返回strings.ToLower(十六进制编码字符串(hashBytes))
}
//GenerateNonce将生成要使用的随机字符串
func generateOnce()字符串{
返回RandStringBytesMaskimpsrc(16)
}
常数(
letterBytes=“abcdefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyz”
letterIdxBits=6//6位表示字母索引
letterIdxMask=1>=letterIdxBits
留--
}
返回字符串(b)
}

这方面存在许多问题,希望我能单独解释每一个问题,以帮助阐明这些事务可能失败的方式

事务完整性 正如@Frank C。我的事务标题上面的注释缺少几个值。这些值是地址和nonce

// 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
}
通过上述设置,RESTAPI将对消息进行解码以打印额外的日志信息,并且验证程序组件将输出更有用的日志

400 Bad Request
{
"error": {
"code": 35,
"message": "The protobuf BatchList you submitted was malformed and could not be read.",
"title": "Protobuf Not Decodable"
}
}
一旦tra
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
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)