-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from ltonetwork/bundle-anchor
Ability to batch hash requests into a single anchor tx
- Loading branch information
Showing
22 changed files
with
269 additions
and
160 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IndexEvent, IndexEventsReturnType } from '../index/index.events'; | ||
import { EmitterService } from '../emitter/emitter.service'; | ||
import { Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { HashService } from './hash.service'; | ||
import { ConfigService } from '../config/config.service'; | ||
import { LoggerService } from '../logger/logger.service'; | ||
|
||
@Injectable() | ||
export class HashListenerService implements OnModuleInit { | ||
constructor( | ||
private readonly indexEmitter: EmitterService<IndexEventsReturnType>, | ||
private readonly hashService: HashService, | ||
private readonly config: ConfigService, | ||
private readonly logger: LoggerService, | ||
) { } | ||
|
||
onModuleInit() { | ||
if (!this.config.isAnchorBatched()) { | ||
this.logger.debug(`hash-listener: Not batching anchors`); | ||
return; | ||
} | ||
|
||
this.onIndexBlock(); | ||
} | ||
|
||
async onIndexBlock() { | ||
this.indexEmitter.on( | ||
IndexEvent.IndexBlock, | ||
() => this.hashService.trigger(), | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {Injectable} from '@nestjs/common'; | ||
import {NodeService} from '../node/node.service'; | ||
import {EncoderService} from '../encoder/encoder.service'; | ||
import {ConfigService} from '../config/config.service'; | ||
|
||
@Injectable() | ||
export class HashService | ||
{ | ||
private hashes; | ||
|
||
constructor( | ||
private readonly config: ConfigService, | ||
private readonly node: NodeService, | ||
private readonly encoder: EncoderService, | ||
) { } | ||
|
||
async anchor( | ||
hash: string, | ||
encoding: string, | ||
): Promise<{ | ||
'@context'; | ||
type; | ||
targetHash; | ||
anchors; | ||
} | null> { | ||
if (this.config.isAnchorBatched()) { | ||
this.hashes.push(this.encoder.base58Encode(this.encoder.decode(hash, encoding))); | ||
return null; | ||
} | ||
|
||
return await this.node.anchor(hash, encoding); | ||
} | ||
|
||
async trigger(): Promise<void> | ||
{ | ||
if (this.hashes.length === 0) return; | ||
|
||
const chunks = [...Array(Math.ceil(this.hashes.length / 100))].map(_ => this.hashes.splice(0, 100)); | ||
await Promise.all(chunks.map(chunk => this.node.anchorAll(...chunk))); | ||
} | ||
} |
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
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
Oops, something went wrong.