Skip to content

Commit 07a5307

Browse files
TinyChain --> event names added.
1 parent bdc4914 commit 07a5307

2 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/TinyChain/Events.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@ class TinyChainEvents {
1616
);
1717
}
1818

19+
/** Triggered when initial balances are updated. */
20+
static InitialBalancesUpdated = 'InitialBalancesUpdated';
21+
22+
/** Triggered when the blockchain instance finishes its initialization. */
23+
static Initialized = 'Initialized';
24+
25+
/** Triggered when a new block is successfully added to the chain. */
26+
static NewBlock = 'NewBlock';
27+
28+
/** Triggered when all balances are initialized. */
29+
static BalancesInitialized = 'BalancesInitialized';
30+
31+
/** Triggered when a balance record is first created for an address. */
32+
static BalanceStarted = 'BalanceStarted';
33+
34+
/** Triggered when a balance value is updated for an existing address. */
35+
static BalanceUpdated = 'BalanceUpdated';
36+
37+
/** Triggered when a payload is emitted as part of a block or transaction. */
38+
static Payload = 'Payload';
39+
40+
/** Triggered when the miner's balance is updated after a block is mined. */
41+
static MinerBalanceUpdated = 'MinerBalanceUpdated';
42+
43+
/** Triggered when balances are recalculated during import. */
44+
static BalanceRecalculated = 'BalanceRecalculated';
45+
46+
/** Triggered when the entire blockchain is cleared or reset. */
47+
static ChainCleared = 'ChainCleared';
48+
49+
/** Triggered when a new chain is imported and replaces the current one. */
50+
static ImportChain = 'ImportChain';
51+
1952
/**
2053
* @returns {string[]}
2154
*/

src/TinyChain/Instance.mjs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TinyPromiseQueue } from 'tiny-essentials';
33
import { EventEmitter } from 'events';
44
import TinyCryptoParser from '../lib/TinyCryptoParser.mjs';
55

6+
import TinyChainEvents from './Events.mjs';
67
import TinyChainBlock from './Block.mjs';
78
import TinySecp256k1 from './Secp256k1/index.mjs';
89

@@ -445,7 +446,7 @@ class TinyChainInstance {
445446
this.initialBalances = initialBalances;
446447

447448
// Emit the event
448-
this.#emit('InitialBalancesUpdated', this.initialBalances);
449+
this.#emit(TinyChainEvents.InitialBalancesUpdated, this.initialBalances);
449450
}
450451

451452
/**
@@ -521,7 +522,7 @@ class TinyChainInstance {
521522
async init(signer = this.#signer) {
522523
return this.#queue.enqueue(async () => {
523524
const block = this.#init(signer);
524-
this.#emit('Initialized', block);
525+
this.#emit(TinyChainEvents.Initialized, block);
525526
});
526527
}
527528

@@ -767,7 +768,7 @@ class TinyChainInstance {
767768

768769
if (this.currencyMode || this.payloadMode) this.updateBalance(newBlock);
769770
this.chain.push(newBlock);
770-
this.#emit('NewBlock', newBlock);
771+
this.#emit(TinyChainEvents.NewBlock, newBlock);
771772
return newBlock;
772773
};
773774
return mineNow();
@@ -797,7 +798,7 @@ class TinyChainInstance {
797798

798799
if (this.currencyMode || this.payloadMode) this.updateBalance(minedBlock);
799800
this.chain.push(minedBlock);
800-
this.#emit('NewBlock', minedBlock);
801+
this.#emit(TinyChainEvents.NewBlock, minedBlock);
801802
return minedBlock;
802803
});
803804
}
@@ -837,11 +838,11 @@ class TinyChainInstance {
837838
*/
838839
startBalances() {
839840
this.balances = {};
840-
this.#emit('BalancesInitialized', this.balances);
841+
this.#emit(TinyChainEvents.BalancesInitialized, this.balances);
841842
if (this.currencyMode) {
842843
for (const [address, balance] of Object.entries(this.getInitialBalances())) {
843844
this.balances[address] = BigInt(balance);
844-
this.#emit('BalanceStarted', address, this.balances[address]);
845+
this.#emit(TinyChainEvents.BalanceStarted, address, this.balances[address]);
845846
}
846847
}
847848
}
@@ -960,7 +961,8 @@ class TinyChainInstance {
960961

961962
if (!balances[execAddress]) {
962963
balances[execAddress] = 0n;
963-
if (emitEvents) this.#emit('BalanceStarted', execAddress, balances[execAddress]);
964+
if (emitEvents)
965+
this.#emit(TinyChainEvents.BalanceStarted, execAddress, balances[execAddress]);
964966
}
965967
const isSufficientBalance = balances[execAddress] >= totalFee ? true : false;
966968

@@ -976,11 +978,11 @@ class TinyChainInstance {
976978
for (const { from, to, amount } of transfers) {
977979
if (!balances[from]) {
978980
balances[from] = 0n;
979-
if (emitEvents) this.#emit('BalanceStarted', from, balances[from]);
981+
if (emitEvents) this.#emit(TinyChainEvents.BalanceStarted, from, balances[from]);
980982
}
981983
if (!balances[to]) {
982984
balances[to] = 0n;
983-
if (emitEvents) this.#emit('BalanceStarted', to, balances[to]);
985+
if (emitEvents) this.#emit(TinyChainEvents.BalanceStarted, to, balances[to]);
984986
}
985987
// @ts-ignore
986988
balances[from] -= amount;
@@ -990,7 +992,7 @@ class TinyChainInstance {
990992
}
991993

992994
if (emitEvents)
993-
this.#emit('BalanceUpdated', {
995+
this.#emit(TinyChainEvents.BalanceUpdated, {
994996
transfers,
995997
address: execAddress,
996998
isAdmin,
@@ -1000,19 +1002,20 @@ class TinyChainInstance {
10001002
});
10011003
}
10021004

1003-
if (this.payloadMode && emitEvents) this.#emit('Payload', execAddress, data.payload);
1005+
if (this.payloadMode && emitEvents)
1006+
this.#emit(TinyChainEvents.Payload, execAddress, data.payload);
10041007
}
10051008

10061009
const totalReward = reward + totalGasCollected;
10071010
const minerAddr = isMinerAddress ? minerAddress : '0';
10081011
if (!balances[minerAddr]) {
10091012
balances[minerAddr] = 0n;
1010-
if (emitEvents) this.#emit('BalanceStarted', minerAddr, balances[minerAddr]);
1013+
if (emitEvents) this.#emit(TinyChainEvents.BalanceStarted, minerAddr, balances[minerAddr]);
10111014
}
10121015
balances[minerAddr] += totalReward;
10131016

10141017
if (emitEvents)
1015-
this.#emit('MinerBalanceUpdated', {
1018+
this.#emit(TinyChainEvents.MinerBalanceUpdated, {
10161019
totalGasCollected,
10171020
reward,
10181021
address: minerAddr,
@@ -1116,7 +1119,7 @@ class TinyChainInstance {
11161119
this.startBalances();
11171120
if (this.currencyMode || this.payloadMode)
11181121
for (const block of this.chain) this.updateBalance(block);
1119-
this.#emit('BalanceRecalculated', this.balances);
1122+
this.#emit(TinyChainEvents.BalanceRecalculated, this.balances);
11201123
}
11211124

11221125
/**
@@ -1176,7 +1179,7 @@ class TinyChainInstance {
11761179
cleanChain() {
11771180
this.chain = [];
11781181
this.balances = {};
1179-
this.#emit('ChainCleared');
1182+
this.#emit(TinyChainEvents.ChainCleared);
11801183
this.startBalances();
11811184
}
11821185

@@ -1219,7 +1222,7 @@ class TinyChainInstance {
12191222
const isValid = this.isValid();
12201223
if (isValid === null) throw new Error('The data chain is null or corrupted.');
12211224
if (!isValid) throw new Error('The data chain is invalid or corrupted.');
1222-
this.#emit('ImportChain', this.chain);
1225+
this.#emit(TinyChainEvents.ImportChain, this.chain);
12231226
}
12241227

12251228
/**

0 commit comments

Comments
 (0)