插入到QLDB时摘要不匹配

2024-09-19 23:32:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Go中的AWS SDK将记录插入QLDB分类账。我以PythonQLDB驱动程序为例,记录了在那里生成的最终事务哈希。这在事务提交期间用于与QLDB端生成的哈希进行比较,以验证事务并允许它提交,python驱动程序成功地完成了这项工作

不过,目前还没有一个Go版本的IonHash,因此我在Go中实现了StartTransaction、InsertInto和CommitTransaction步骤,并包含了一个Python可执行IonHash实现,用于计算最后用于比较摘要的IonHash

// Go (pseudocode)
import "github.com/fernomac/ion-go" as ion
import python_hash_module as python

func (client qldbClient) StartTransaction(transactionID string) {
 // hash transactionID using python ionhash
}

func (client) InsertInto (statement string, params string) {
    // MarshalText using ion module in aws-sdk
    ionParam := ion.MarshalText(params)

    // hash statement using python executable
    client.statementHash = python.ion_hash(statement)

    // hash parameters using python executable (only one parameter)
    client.paramHash = python.ion_hash(ionParam)

    // dot paramHash with statement hash
    client.statementHash = client.statementHash.dot(client.paramHash)

    // dot statement hash with transactionhash - this transaction hash matches the python calculation!
    client.transactionHash = client.transactionHash.dot(statementHash)
}

func (client) Commit() {
    res, err := client.execute(statement) // compares calculated transaction hash with AWS calculated transaction hash
    if err != nil {
        log.Prinln(err)
}

代码在提交步骤中失败,出现以下错误:

{
  Code_: "412",
  Message_: "Digests don't match"
}
2020/03/22 11:16:41 xxxx.go:xxx: BadRequestException: Digests don't match
{
  Code_: "412",
  Message_: "Digests don't match"
}

我不明白为什么在go中提交时,这些摘要不匹配,而这个实现正在生成与python代码相同的摘要,而python代码正在提交。为什么python代码在生成与go代码相同的提交时不抱怨摘要不匹配?更重要的是,如何通过Go(而不是python或节点驱动程序)成功插入QLDB


Tags: 代码clientgo驱动程序hash事务dotstatement
2条回答

离子散列go不可用的理解是正确的

在计算哈希时,您可能需要检查以下几项:

  1. 散列值与事务id的离子散列一起播种 从技术上讲,两个事务的最终散列永远不会相同
  2. 正如您所注意到的,哈希值是使用QLDB“dot”运算符更新的。 点运算符是QLDB合并散列值和定义散列值的方式 作为两个哈希值串联的哈希值,由 (有符号,小尾端)两个散列之间的字节比较。 参考号- https://github.com/awslabs/amazon-qldb-driver-python/blob/39fecdf2ed0521ae1d19f342a4cab38846b96c9a/pyqldb/util/qldb_hash.py#L99
  3. 哈希算法需要是SHA 256
  4. 在使用参数散列查询时,正确的实现是 离子散列 任何语句(其中可能包含任何PartiQL文本) 然后是作为参数传入的每个IonValue的IonHash (与点运算符组合)

从您的伪代码中我可以猜到您可能缺少1)和2)。更具体地说,我的意思是,伪代码表明哈希没有使用事务id作为种子,并且点运算符没有按正确的顺序应用于哈希

有关QLDB哈希计算的更多详细信息,请参阅Marc的答案-How to get/compute CommitDigest when committing a transaction in AWS QLDB?

愿意在这方面进一步提供帮助

更新:

离子散列go现在可用-https://github.com/amzn/ion-hash-go

还发布了与QLDB交互的go驱动程序-https://github.com/awslabs/amazon-qldb-driver-go

它们目前都是测试版

不确定这是否仍然有用,但Amazon最近发布了QLDB Go驱动程序的预览版本(https://github.com/awslabs/amazon-qldb-driver-go

它的依赖项是Ion和Ion Hash,因此这将使您在使用QLDB时更加容易

相关问题 更多 >