在AWS QLDB中提交事务时如何获取/计算CommitDigest? [英] How to get/compute CommitDigest when committing a transaction in AWS QLDB?

查看:121
本文介绍了在AWS QLDB中提交事务时如何获取/计算CommitDigest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读文档,试图弄清楚如何在QLDB中提交事务,为此,需要CommitDigest,文档将其描述为:

I've been reading through the docs trying to figure out how to commit a transaction in QLDB, and in order to do so, a CommitDigest is required, and the docs describe it as:

指定要提交的事务的提交摘要.对于每个活动事务,必须传递提交摘要.如果客户端上计算的摘要与QLDB计算的摘要不匹配,则QLDB会验证CommitDigest并拒绝提交,并显示错误.

Specifies the commit digest for the transaction to commit. For every active transaction, the commit digest must be passed. QLDB validates CommitDigest and rejects the commit with an error if the digest computed on the client does not match the digest computed by QLDB.

因此必须计算CommitDigest,但在此示例中,我不太确定其计算需要什么:

So CommitDigest must be computed, but I'm not quite sure what's required for its computation given this example:

// ** Start Session **
const startSessionResult = await qldbSession.sendCommand({
        StartSession: {
            LedgerName: ledgerName
        }
    }).promise(),
    sessionToken = startSessionResult.StartSession!.SessionToken!;

// ** Start Transaction **
const startTransactionResult = await qldbSession.sendCommand({
        StartTransaction: {},
        SessionToken: sessionToken
    }).promise(),
    transactionId = startTransactionResult.StartTransaction!.TransactionId!;

// ** Insert Document **
const executeStatementResult = await qldbSession.sendCommand({
        ExecuteStatement: {
            TransactionId: transactionId,
            Statement: `INSERT INTO sometable { 'id': 'abc123', 'userId': '123abc' }`
        },
        SessionToken: sessionToken
    }).promise(),
    documentId = getDocumentIdFromExecuteStateResult(executeStatementResult)

// ** Get Ledger Digest
const getDigestResult = await qldb.getDigest({
        Name: ledgerName
    }).promise(),
    ledgerDigest = getDigestResult.Digest;


// ** Commit Transaction **
// ** **The API call in question** **
const commitTransactionResult = await qldbSession.sendCommand({
    CommitTransaction: {
        TransactionId: transactionId,
        CommitDigest: `${commitDigest}` // <-- How to compute?
    },
    SessionToken: sessionToken
}).promise();
// *******************************


// ** End Session **
const endSession = await qldbSession.sendCommand({
    EndSession: {},
    SessionToken: sessionToken
}).promise();

CommitTransaction api调用中我需要为CommitDigest散列什么?

What do I need to hash for CommitDigest in the CommitTransaction api call?

推荐答案

更新:Node.js驱动程序现在可用.看看 https://github.com/awslabs/amazon-qldb-driver -nodejs/.

Update: the Node.js driver is now available. Take a look at https://github.com/awslabs/amazon-qldb-driver-nodejs/.

在撰写本文时,QLDB Node.js驱动程序仍在开发中.如果您尝试自己创建一个人,这将相当困难,所以我警告您不要这样做.也就是说,我可以解释CommitDigest的目的和算法.

At the time of writing, the QLDB Node.js driver is still in development. It is going to be fairly difficult if you try and create one yourself, so I would caution against doing so. That said, I can explain both the purpose and algorithm behind CommitDigest.

目的很简单:确保仅在服务器处理完客户端发送的确切语句集后,才提交事务(所有顺序,无重复). HTTP是请求-响应,因此可能会丢弃请求,无序处理或重复请求. QLDB驱动程序可以正确管理与QLDB的通信,但是协议中包含提交摘要,因此实现不可能错误地重试请求并仍然提交事务.例如,考虑两次增加银行存款余额,因为即使第一个请求成功,也会重试HTTP消息.

The purpose is fairly straight-forward: to ensure that transactions are only committed iff the server has processed the exact set of statements sent by the client (all, in order, no duplicates). HTTP is request-response and it is therefore possible that requests may be dropped, processed out of order or duplicated. The QLDB drivers manage the communication with QLDB correctly, but having commit digest being in the protocol makes it impossible for an implementation to incorrectly retry requests and still commit the transaction. For example, consider incrementing a bank balance twice because a HTTP message is retried even though the first request succeeded.

该算法也非常简单:将哈希值填充到交易ID中,然后使用QLDB点"运算符进行更新.语句哈希(PartiQL字符串的sha256)和 IonHash .点运算符是QLDB合并哈希值的方式(这与

The algorithm is also pretty straight-forward: a hash value is seeded with the transaction id and then updated with the QLDB ‘dot’ operator. Each update ‘dots’ in the statement hash (sha256 of the PartiQL string) as well as the IonHash of all of the bind values. The dot operator is the way QLDB merges hash values (this is the same operator used in the verification APIs) and is defined as the hash of the concatenation of the two hashes, ordered by the (signed, little-endian) byte-wise comparison between the two hashes. The client and server run this algorithm in lock-step and the server will only process the commit command if the value the client passes matches what the server computed. In this way, the server will never commit a transaction that isn’t exactly what the client requested.

这篇关于在AWS QLDB中提交事务时如何获取/计算CommitDigest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆