Hands-on :Quantum Ledger -QLDB for Off-chain Strategy

Rangesh Sripathi
3 min readApr 5, 2020

--

Quantum Ledger would emerge as one of key element for off-chain strategy. I would not be surprised if QLDB is rolled along with every Enterprise Blockchain application that interacts heavily with data .

What is QLDB ?

It is a database that provides Transparent,Immutable,Cryptographically verifiable Transaction. It is completely managed by AWS .

Why QLDB ?

QLDB provides the following features

a) Version History — It keeps track of version changes that is associated with a document ,It gives you history of changes that is associated with document,

b) Immutable — Documents that are stored cannot be modified/deleted , quite similar to Blockchain

c) Secure — Use SHA256 Digest for transactions

When to use QLDB ?

QLDB can be used when there is need to prove data integrity / data authenticity when data is been shared to Multiple parties. The need exactly matches Blockchain ,especially when storing large amount of data in Blockchain would cause bottlenecks. Ideally solution to such problem would be onchain -offchain strategy ,where intention is to store only required elements to onchain and leverage rest of elements to offchain . When it means offchain it would be ideally database and this is where the characteristics of QLDB differs from rest of relational or No-sql database.

Getting started with QLDB:

QLDB Drivers / Connectors is supported for now in .NET/Java/Node/Python. I picked up .NET Connector /Driver.

  1. Get AWS request and access key respectively from IAM AWS .
  2. Make sure the role under IAM has full access to operate on QLDB

1. Create Ledger — Connect to QLDB and Create database

string ledgerName = "my-ledger";
var awsCredentials = new BasicAWSCredentials("request", "key");
Console.WriteLine($"Create the ledger '{ledgerName}'"); AmazonQLDBClient qldbClient = new AmazonQLDBClient(awsCredentials, Amazon.RegionEndpoint.USEast2);
CreateLedgerRequest createLedgerRequest = new CreateLedgerRequest
{
Name = ledgerName,
PermissionsMode = PermissionsMode.ALLOW_ALL
};
qldbClient.CreateLedgerAsync(createLedgerRequest).GetAwaiter().GetResult();
Console.WriteLine($"Waiting for ledger to be active");
DescribeLedgerRequest describeLedgerRequest = new DescribeLedgerRequest
{
Name = ledgerName
};
while (true)
{
DescribeLedgerResponse describeLedgerResponse = qldbClient.DescribeLedgerAsync(describeLedgerRequest).GetAwaiter().GetResult();
if (describeLedgerResponse.State.Equals(LedgerState.ACTIVE.Value))
{
Console.WriteLine($"'{ ledgerName }' ledger created sucessfully.");
break;
}
Console.WriteLine($"Creating the '{ ledgerName }' ledger...");
Thread.Sleep(1000);
}

2. Once Ledger is created , we can interact with Ledger for Creating Table and other related ACID operations associated with database. Here is a sample of inserting documents into table.

AmazonQLDBSessionConfig amazonQldbSessionConfig = new AmazonQLDBSessionConfig();
amazonQldbSessionConfig.RegionEndpoint = Amazon.RegionEndpoint.USEast2;
Console.WriteLine($"Create the QLDB Driver");
IQldbDriver driver = PooledQldbDriver.Builder().WithAWSCredentials(awsCredentials)
.WithQLDBSessionConfig(amazonQldbSessionConfig)
.WithLedger(ledgerName)
.Build();
string tableName = "MyTable1";
var objsample = new SampleData() { FName = "Rangesh", LName = "Sripathi" };
string jsonresult= JsonConvert.SerializeObject(objsample,Formatting.Indented);
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
writer.QuoteChar = '\'';
JsonSerializer ser = new JsonSerializer();
ser.Serialize(writer, objsample);
}
Console.WriteLine(sb.ToString());
using (IQldbSession qldbSession = driver.GetSession())
{
var text = $"INSERT INTO {tableName} VALUE" + sb;
Console.WriteLine(text);
// qldbSession.Execute will start a transaction and commit it.
//IResult result = qldbSession.Execute($"CREATE TABLE {tableName}");
qldbSession.Execute($"INSERT INTO {tableName} VALUE "+sb);
Console.WriteLine($"Table '{tableName}' created");
}
}

3. Verify Authenticity / Version History of document — I have used console to verify the same , however it can be done programmatic. Please look into block-address and digest.

4.Here is Verification Digest/ Proof from console :

Final Thoughts

QLDB will be inevitable,specially when it comes to onchain-offchain strategy , since QLDB possess characteristics similar to blockchain ,but its centrally owned. QLDB should not be treated like normal database and should be used only if Immutability / Verification of Data or Trust is required. It does have some limitations and as well it does not support KMS for now.

Source Code of Sample :

https://github.com/rangesh-/QLDB/

Reference :

https://docs.aws.amazon.com/qldb/index.html

--

--

Responses (2)