forked from dfuse-io/client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
never-miss-a-beat.ts
212 lines (179 loc) · 6.25 KB
/
never-miss-a-beat.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { DFUSE_API_KEY, runMain, DFUSE_API_NETWORK } from "../../config"
import {
createDfuseClient,
waitFor,
Stream,
DfuseClient,
dynamicMessageDispatcher,
ProgressInboundMessage,
ActionTraceInboundMessage,
Action,
} from "@dfuse/client"
/**
* In this example, we will showcase how to implement bulletproof
* data integrity while using the dfuse Stream by ensuring you never
* miss a single beat.
*
* This pattern can be used when you want to process messages only
* once, while still ensuring you correctly receive all the blocks,
* transactions and actions you want to process.
*
* We go through an example of how to easily mark the stream progress
* and how the marker is then used when the socket reconnects to
* restart the stream at the exact location you need.
*
* In the example we will implement an action persistence storer,
* having our code restart at the exact correct place a commit had
* occurred.
*
* @see https://docs.dfuse.io/#websocket-based-api-never-missing-a-beat
*/
async function main(): Promise<void> {
const client = createDfuseClient({
apiKey: DFUSE_API_KEY,
network: DFUSE_API_NETWORK,
streamClientOptions: {
socketOptions: {
reconnectDelayInMs: 250,
},
},
})
const engine = new Engine(client)
await engine.start()
await waitFor(50000)
await engine.stop()
client.release()
}
type KarmaTransfer = {
from: string
to: string
quantity: string
memo: string
}
class Engine {
private client: DfuseClient
private stream?: Stream
private pendingActions: Action<KarmaTransfer>[] = []
private lastCommittedBlockNum = 0
private committedActions: Action<KarmaTransfer>[] = []
constructor(client: DfuseClient) {
this.client = client
}
public async start(): Promise<void> {
const dispatcher = dynamicMessageDispatcher({
listening: this.onListening,
action_trace: this.onAction,
progress: this.onProgress,
})
console.log("Engine starting")
this.stream = await this.client.streamActionTraces(
{
accounts: "therealkarma",
action_names: "transfer",
},
dispatcher,
{
// You can use the `with_progress` to be sure to commit
// actions at least each 10 blocks. This is useful if your stream
// is low traffic so you don't need to wait until the next
// action to commit all changes.
with_progress: 10,
}
)
this.stream.onPostRestart = () => {
console.log()
console.log(
"<============= Stream has reconnected to the socket correctly (at latest `mark()`) =============>"
)
console.log()
// Upon a reconnection, we need to clear previously accumulated actions
this.flushPending()
}
console.log("Stream connected, ready to receive messages")
}
private onListening = (): void => {
console.log("Stream is now listening for action(s)")
}
private onProgress = (message: ProgressInboundMessage): void => {
const { block_id, block_num } = message.data
/**
* Once a progress message is seen, it means we've seen all messages for
* blocks prior it, so let's commit until this point.
*/
console.log()
console.log("Committing changes due to seeing a message from a progress message")
this.commit(block_id, block_num)
}
private onAction = (message: ActionTraceInboundMessage<KarmaTransfer>): void => {
/**
* Once a message from a block ahead of the last committed block is seen,
* commit all changes up to this point.
*/
const { block_id, block_num } = message.data
if (block_num > this.lastCommittedBlockNum) {
console.log()
console.log(
"Comitting changes due to seeing a message from a block ahead of our last committed block"
)
this.commit(block_id, block_num)
}
const action = message.data.trace.act
const { from, to, quantity } = action.data
console.log(
`Pending transfer [${from} -> ${to} ${quantity}] @ ${printBlock(block_id, block_num)}`
)
this.pendingActions.push(message.data.trace.act)
}
private commit(blockId: string, blockNum: number): void {
console.log(`Committing all actions up to block ${printBlock(blockId, blockNum)}`)
if (this.pendingActions.length > 0) {
// Here, in your production code, action would be saved in a database, as well as error handling
this.pendingActions.forEach((action) => this.committedActions.push(action))
}
console.log(`Bumping last committed block and clearing pending actions`)
this.pendingActions = []
this.lastCommittedBlockNum = blockNum
/**
* This is one of the most important calls of the example. By marking the stream
* at the right block, upon restarting, the stream will automatically start back
* at this block ensuring you never miss a single action.
*/
console.log(`Marking stream up to block ${printBlock(blockId, blockNum)}`)
this.ensureStream().mark({ atBlockNum: blockNum })
/**
* In a real-word production code, you would also need to persist the
* `this.lastCommittedBlockNum` value to ensure that upon a process
* restart, you start back from this exact value.
*/
console.log("")
}
/**
* When the stream reconnects, we must flush all of the current pending transactions
* as the stream restarts at our last marked block, inclusively.
*
* Since we mark after commit, anything currently in pending was not committed.
* As such, let's flush all pending actions. The dfuse Stream API will stream them back.
*/
public flushPending(): void {
console.log("Flushing pending action(s) due to refresh")
this.pendingActions = []
}
public async stop(): Promise<void> {
await this.ensureStream().close()
console.log("Committed actions")
this.committedActions.forEach((action) => {
const { from, to, quantity } = action.data
console.log(`- Commit transfer [${from} -> ${to} ${quantity}]`)
})
}
private ensureStream(): Stream {
if (this.stream) {
return this.stream
}
throw new Error("Stream should be set at this runtime execution point")
}
}
function printBlock(blockId: string, blockNum: number): string {
return `${blockId.slice(0, 8)}...${blockId.slice(-8)} (${blockNum})`
}
runMain(main)