Remote Nonce Caching
This guide will walk you through successfully installing the API and initialising for Remote Nonce Caching.
Why a Remote Nonce Cache is Needed
In blockchain systems, nonces play a critical role in ensuring that each transaction signature is unique. The Avn chain uses nonces within signatures to guarantee this uniqueness, which prevents replay attacks and secures transaction integrity.
Key Reasons
-
Unique Signatures: Every transaction signature must be unique. In blockchain, nonces ensure that no two transactions can have the same signature. The Avn chain leverages nonces for this purpose, ensuring that each transaction is distinct and secure.
-
Sequential Incrementing Across Multiple Instances: In environments where the SDK is deployed on multiple backend instances (e.g., in horizontal scaling scenarios), it is crucial that transaction nonces are incremented sequentially. Without coordination, different instances might use conflicting nonces. A remote nonce cache centralizes the management, ensuring that nonces are tracked and incremented in the correct order across all instances.
-
Handling High Transaction Loads: Under high load, the SDK may send transactions back-to-back. Although the blockchain’s on-chain nonce increases with each block produced, multiple transactions can be included in the same block. The SDK must manage nonce incrementation locally while periodically synchronizing with the blockchain’s current value. This approach avoids conflicts and ensures that all transactions are processed correctly.
Setup
The first thing we need to do is ensure that you have npm
installed. You'll find instructions on how to do this here. You'll also find instructions on how to install the avn-api library.
Create an account The easiest way to create an account is to create it using a supported wallet. Supported wallets are: PolkadotJS, Subwallet, Nova, Talisman.
We Need AVT
AVT is the utility token of the Aventus network. It's required to do the below:
-
Access the Blockchain network via the API. You need 1 AVT in your account to use the API. The AVT needs to be lifted (moved) to the Aventus Network, and there are also instructions for that too here.
-
Transaction Fee: Note, all transactions on the AVN must be paid for using AVT, so going forward it's important for you to have some AVT in your account. There are multiple ways to purchase AVT and this page provides instructions on how to get some.
Once you have some AVT in your account, of which you can check your account balance by typing your address into the explorer here, we can now configure the Remote Nonce Caching.
First we install the prerequisites.
const { AvnApi, SetupMode, SigningMode } = require("avn-api");
const AVN_GATEWAY_URL = "replace with AVN_GATEWAY_URL";
Then we initialise the API
const TestNonceCacheProvider =
"import the example nonce cache provider, the link is above"; //require('./testRedisNonceCacheProvider');
const testCacheProvider = new TestNonceCacheProvider();
const options = {
setupMode: SetupMode.MultiUser,
signingMode: SigningMode.RemoteSigner,
hasPayer: false,
defaultLogLevel: "error",
signer,
nonceCacheType: NonceCacheType.Remote,
cacheProvider: testCacheProvider,
};
const avnSdk = new AvnApi(AVN_GATEWAY_URL, options);
async function main() {
// initialise the api sdk and return
await avnSdk.init();
return avnSdk;
}