-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core-state): wallet repositories (#3501)
- Loading branch information
Showing
8 changed files
with
84 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./temp-wallet-repository"; | ||
export * from "./wallet-repository"; | ||
export * from "./wallet-repository-clone"; | ||
export * from "./wallet-repository-copy-on-write"; | ||
export * from "./wallet"; |
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
packages/core-state/src/wallets/wallet-repository-clone.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Container, Contracts } from "@arkecosystem/core-kernel"; | ||
|
||
import { WalletRepository } from "./wallet-repository"; | ||
|
||
@Container.injectable() | ||
export class WalletRepositoryClone extends WalletRepository { | ||
@Container.inject(Container.Identifiers.WalletRepository) | ||
@Container.tagged("state", "blockchain") | ||
private readonly blockchainWalletRepository!: Contracts.State.WalletRepository; | ||
|
||
@Container.postConstruct() | ||
public initialize(): void { | ||
for (const index of this.blockchainWalletRepository.getIndexNames()) { | ||
this.indexes[index] = this.blockchainWalletRepository.getIndex(index).clone(); | ||
} | ||
} | ||
|
||
public reindex(wallet: Contracts.State.Wallet): void { | ||
super.reindex(wallet.clone()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/core-state/src/wallets/wallet-repository-copy-on-write.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Container, Contracts } from "@arkecosystem/core-kernel"; | ||
|
||
import { WalletRepository } from "./wallet-repository"; | ||
|
||
// ! This isn't copy-on-write, but copy-on-read and with many asterisks. | ||
// ! It only covers current pool use-cases. | ||
// ! It should be replaced with proper implementation eventually. | ||
|
||
@Container.injectable() | ||
export class WalletRepositoryCopyOnWrite extends WalletRepository { | ||
@Container.inject(Container.Identifiers.WalletRepository) | ||
@Container.tagged("state", "blockchain") | ||
private readonly blockchainWalletRepository!: Contracts.State.WalletRepository; | ||
|
||
public findByAddress(address: string): Contracts.State.Wallet { | ||
if (address && !this.hasByAddress(address)) { | ||
const walletClone = this.blockchainWalletRepository.findByAddress(address).clone(); | ||
this.reindex(walletClone); | ||
} | ||
return this.findByIndex(Contracts.State.WalletIndexes.Addresses, address)!; | ||
} | ||
|
||
public hasByIndex(index: string, key: string): boolean { | ||
if (super.hasByIndex(index, key)) { | ||
return true; | ||
} | ||
if (this.blockchainWalletRepository.hasByIndex(index, key) === false) { | ||
return false; | ||
} | ||
const walletClone = this.blockchainWalletRepository.findByIndex(index, key).clone(); | ||
this.reindex(walletClone); | ||
return true; | ||
} | ||
|
||
public allByUsername(): ReadonlyArray<Contracts.State.Wallet> { | ||
for (const wallet of this.blockchainWalletRepository.allByUsername()) { | ||
if (super.hasByAddress(wallet.address) === false) { | ||
this.reindex(wallet.clone()); | ||
} | ||
} | ||
return super.allByUsername(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters