-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamodb.js
186 lines (178 loc) · 5.04 KB
/
dynamodb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const {
DynamoDBClient,
CreateTableCommand,
PutItemCommand,
GetItemCommand,
QueryCommand
} = require("@aws-sdk/client-dynamodb");
const { sendEmail } = require('./utils/utils');
const LeavesTableName = process.env.NODE_ENV == "development" ? "Leaves-dev" : "Leaves";
const ddbClient = new DynamoDBClient({
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
region: "us-east-1"
});
// We store just the leaves instead of the whole Merkle tree because we can construct the tree
// from the leaves and storing the whole tree adds unnecessary overhead (e.g., ensuring that
// series of node updates that constitutes a leaf insertion procedure is atomic).
const createLeavesTableIfNotExists = async () => {
try {
const params = {
AttributeDefinitions: [
{
// position of leaf in the array of leaves
AttributeName: "LeafIndex",
AttributeType: "N",
},
{
// The signed leaf used in the onAddLeaf proof used to add the leaf
AttributeName: "SignedLeaf",
AttributeType: "S",
}
],
KeySchema: [
{
"KeyType": "HASH",
"AttributeName": "LeafIndex"
},
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
TableName: LeavesTableName,
GlobalSecondaryIndexes: [
{
IndexName: "SignedLeavesIndex",
KeySchema: [
{
AttributeName: "SignedLeaf",
KeyType: "HASH",
},
],
Projection: {
ProjectionType: "ALL",
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
}
],
StreamSpecification: {
StreamEnabled: false,
},
};
// CreateTableCommand throws if table already exists.
const data = await ddbClient.send(new CreateTableCommand(params));
console.log(`${LeavesTableName} table created in DynamoDB`, data);
} catch (err) {
if (err.name === "ResourceInUseException") {
console.log(`${LeavesTableName} table already exists in DynamoDB`);
} else {
console.error("Error", err);
}
}
};
async function handleDynamoDbErrorNotification(err) {
if (err.name === "ProvisionedThroughputExceededException") {
// Send email to admin(s)
if (process.env.NODE_ENV !== 'development' && process.env.ADMIN_EMAILS) {
for (const email of process.env.ADMIN_EMAILS.split(',')) {
await sendEmail(
email,
"ProvisionedThroughputExceededException",
err.message
);
}
}
}
}
/**
* Put leaf into database
* @param {string} newLeaf - the new leaf to add to the Merkle tree
* @param {string} signedLeaf - the signed leaf used in the onAddLeaf proof
* @param {number} leafIndex - the index at which to put the new leaf
*/
async function putLeaf(newLeaf, signedLeaf, leafIndex) {
try {
await ddbClient.send(new PutItemCommand({
TableName: LeavesTableName,
Item: {
'LeafIndex': {
N: leafIndex.toString()
},
'LeafValue': {
S: newLeaf
},
"SignedLeaf": {
S: signedLeaf
},
"Timestamp": {
N: Date.now().toString()
}
}
}));
console.log(`Added leaf ${newLeaf} to database at index ${leafIndex}`)
} catch (err) {
await handleDynamoDbErrorNotification(err);
console.error("putLeaf: ", err);
// Throw error to display on frontend
throw new Error("Failed to put leaf");
}
}
/**
* Get the leaf item at the given index
* @param {number} index
*/
async function getLeafAtIndex(index) {
try {
return await ddbClient.send(new GetItemCommand({
TableName: LeavesTableName,
Key: {
"LeafIndex": {
N: index.toString()
}
}
}));
} catch (err) {
await handleDynamoDbErrorNotification(err);
console.error("getLeafAtIndex: ", err);
// Throw error that can be displayed on the frontend
throw new Error("An error occurred while querying the database");
}
}
/**
* Query the database for the leaf items that have the given signed leaf.
* (There should only be one leaf item for any given signed leaf.)
* @param {string} signedLeaf
*/
async function getLeavesBySignedLeaf(signedLeaf) {
try {
return await ddbClient.send(new QueryCommand({
TableName: LeavesTableName,
IndexName: "SignedLeavesIndex",
KeyConditionExpression: "SignedLeaf = :signedLeaf",
ExpressionAttributeValues: {
":signedLeaf": {
S: signedLeaf
}
}
}));
} catch (err) {
await handleDynamoDbErrorNotification(err);
console.error("getLeavesBySignedLeaf: ", err);
// Throw error to display on frontend
throw new Error("An error occurred while querying the database");
}
}
module.exports = {
LeavesTableName,
ddbClient,
createLeavesTableIfNotExists,
putLeaf,
getLeafAtIndex,
getLeavesBySignedLeaf,
};