-
Notifications
You must be signed in to change notification settings - Fork 773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blockchain: internal refactor #895
Closed
Closed
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bf68c5e
blokchain: add reorg test
jochem-brouwer 784b46e
vm: re-check skipped tests
jochem-brouwer 5b508bd
blockchain: fix tests
jochem-brouwer 73f155c
block: add static getCanonicalDifficulty method
jochem-brouwer 671de5e
blockchain: throw early on errors
jochem-brouwer b17439d
do not track dist.browser folder
jochem-brouwer 7b13006
blockchain: add DBInterface, fix test
jochem-brouwer 81939e1
stash
jochem-brouwer 046e4c8
blockchain: refactor DBManager, create databaseOperation factory
jochem-brouwer bd52c9b
blockchain: include new DB changes
jochem-brouwer a2ad1d1
blockchain: lint
jochem-brouwer 1445468
blockchain: fix tests
jochem-brouwer 13c6f01
monorepo: lint
jochem-brouwer 632101d
blockchain: add docs, rename internal functions, make functions priva…
jochem-brouwer 1224cbc
blockchain: fix race conditions on getters/iterator
jochem-brouwer 861cab4
blockchain: fix concurrency
jochem-brouwer fd19635
block: fix static canonical functions
jochem-brouwer e3fe556
blockchain: add helper functions for initializing and locks
jochem-brouwer dcb1b37
blockchain: if non-canonical genesis block, provide this is constructor
jochem-brouwer 99b4ab2
blockchain: genesis block options checks, static factory
jochem-brouwer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
test-build | ||
dist.browser |
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
File renamed without changes.
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,145 @@ | ||
import BN = require('bn.js') | ||
import { | ||
HEADS_KEY, | ||
HEAD_HEADER_KEY, | ||
HEAD_BLOCK_KEY, | ||
tdKey, | ||
headerKey, | ||
bodyKey, | ||
numberToHashKey, | ||
hashToNumberKey, | ||
} from './dbConstants' | ||
|
||
import { CacheMap } from './dbManager' | ||
|
||
export enum DatabaseOperationTarget { | ||
Heads, | ||
HeadHeader, | ||
HeadBlock, | ||
HashToNumber, | ||
NumberToHash, | ||
TotalDifficulty, | ||
Body, | ||
Header, | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export interface DBOp { | ||
type?: String | ||
key: Buffer | string | ||
keyEncoding: String | ||
valueEncoding?: String | ||
value?: Buffer | object | ||
} | ||
|
||
// a Database Key is identified by a block hash, a block number, or both | ||
export type DatabaseKey = { | ||
blockNumber?: BN | ||
blockHash?: Buffer | ||
} | ||
|
||
export class DatabaseOperation { | ||
public operationTarget: DatabaseOperationTarget | ||
public baseDBOp: DBOp | ||
public cacheString: string | undefined | ||
|
||
private constructor(operationTarget: DatabaseOperationTarget, key?: DatabaseKey) { | ||
this.operationTarget = operationTarget | ||
|
||
this.baseDBOp = { | ||
key: '', | ||
keyEncoding: 'binary', | ||
valueEncoding: 'binary', | ||
} | ||
|
||
switch (operationTarget) { | ||
case DatabaseOperationTarget.Heads: { | ||
this.baseDBOp.key = HEADS_KEY | ||
this.baseDBOp.valueEncoding = 'json' | ||
break | ||
} | ||
case DatabaseOperationTarget.HeadHeader: { | ||
this.baseDBOp.key = HEAD_HEADER_KEY | ||
break | ||
} | ||
case DatabaseOperationTarget.HeadBlock: { | ||
this.baseDBOp.key = HEAD_BLOCK_KEY | ||
break | ||
} | ||
case DatabaseOperationTarget.HashToNumber: { | ||
this.baseDBOp.key = hashToNumberKey(key!.blockHash!) | ||
this.cacheString = 'hashToNumber' | ||
break | ||
} | ||
case DatabaseOperationTarget.NumberToHash: { | ||
this.baseDBOp.key = numberToHashKey(key!.blockNumber!) | ||
this.cacheString = 'numberToHash' | ||
break | ||
} | ||
case DatabaseOperationTarget.TotalDifficulty: { | ||
this.baseDBOp.key = tdKey(key!.blockNumber!, key!.blockHash!) | ||
this.cacheString = 'td' | ||
break | ||
} | ||
case DatabaseOperationTarget.Body: { | ||
this.baseDBOp.key = bodyKey(key!.blockNumber!, key!.blockHash!) | ||
this.cacheString = 'body' | ||
break | ||
} | ||
case DatabaseOperationTarget.Header: { | ||
this.baseDBOp.key = headerKey(key!.blockNumber!, key!.blockHash!) | ||
this.cacheString = 'header' | ||
break | ||
} | ||
} | ||
} | ||
|
||
public static get( | ||
operationTarget: DatabaseOperationTarget, | ||
key?: DatabaseKey | ||
): DatabaseOperation { | ||
return new DatabaseOperation(operationTarget, key) | ||
} | ||
|
||
// set operation: note: value/key is not in default order | ||
public static set( | ||
operationTarget: DatabaseOperationTarget, | ||
value: Buffer | object, | ||
key?: DatabaseKey | ||
): DatabaseOperation { | ||
const databaseOperation = new DatabaseOperation(operationTarget, key) | ||
databaseOperation.baseDBOp.value = value | ||
databaseOperation.baseDBOp.type = 'put' | ||
|
||
if (operationTarget == DatabaseOperationTarget.Heads) { | ||
databaseOperation.baseDBOp.valueEncoding = 'json' | ||
} else { | ||
databaseOperation.baseDBOp.valueEncoding = 'binary' | ||
} | ||
|
||
return databaseOperation | ||
} | ||
|
||
public static del( | ||
operationTarget: DatabaseOperationTarget, | ||
key?: DatabaseKey | ||
): DatabaseOperation { | ||
const databaseOperation = new DatabaseOperation(operationTarget, key) | ||
databaseOperation.baseDBOp.type = 'del' | ||
return databaseOperation | ||
} | ||
|
||
public updateCache(cacheMap: CacheMap) { | ||
if (this.cacheString && cacheMap[this.cacheString] && Buffer.isBuffer(this.baseDBOp.value)) { | ||
if (this.baseDBOp.type == 'put') { | ||
cacheMap[this.cacheString].set(this.baseDBOp.key, this.baseDBOp.value) | ||
} else if (this.baseDBOp.type == 'del') { | ||
cacheMap[this.cacheString].del(this.baseDBOp.key) | ||
} else { | ||
throw new Error('unsupported db operation on cache') | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to do this change to
static
we should really drop theparent
here on all fronts and just call thisblock
. There is no information lost here and this simplifies things and this parent doesn't make any sense any more in a static context.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have just pushed fd19635 but I am actually not sure anymore why I made this static in the first place. Since we always need a
header
(or ablock
) for this function, it makes more sense to removestatic
, just use the current header and let the caller specify the timestamp + (optionally) number/common?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that makes sense, was just wondering too if this double structure with one static and one non-static method really makes sense