You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
ValidateSPV.validateHeaderChain() returns one of three different errors if it fails.
exportfunctionvalidateHeaderChain(headers){// Check header chain lengthif(headers.length%80!==0){thrownewTypeError('Header bytes not multiple of 80.');}letdigest;lettotalDifficulty=BigInt(0);for(leti=0;i<headers.length/80;i+=1){// ith header start index and ith headerconststart=i*80;constheader=utils.safeSlice(headers,start,start+80);// After the first header, check that headers are in a chainif(i!==0){if(!validateHeaderPrevHash(header,digest)){thrownewError('Header bytes not a valid chain.');}}// ith header targetconsttarget=BTCUtils.extractTarget(header);// Require that the header has sufficient workdigest=BTCUtils.hash256(header);if(!validateHeaderWork(digest,target)){thrownewError('Header does not meet its own difficulty target.');}totalDifficulty+=BTCUtils.calculateDifficulty(target);}returntotalDifficulty;}
When using this externally, this means having to do a switch/case to handle for each of the different errors messages. Comparing string error messages is prone to breakage if the message ever changes, and not optimal.
// Current implementation when using externallyswitch(e.message){case'Header bytes not multiple of 80.':
return[false,errors.ERR_INVALID_CHAIN];case'Header bytes not a valid chain.':
return[false,errors.ERR_INVALID_PREV_BLOCK];case'Header does not meet its own difficulty target.':
return[false,errors.ERR_INVALID_PROOF_OF_WORK];default:
return[false,errors.ERR_INVALID_CHAIN];}
Suggestions:
Return error codes. This requires keeping a file of error codes. The advantage is the message can change but the code remains the same. Examples:
1 is header length error
2is invalid chain
3 is doesn't meet difficulty target
This option is a bit more time intensive to implement, and also requires the user to lookup the error codes to know what they mean.
The obvious disadvantage to this is there is a limited amount of error types, and the default is never going to be reached since all error types are Errors. New error types can be constructed, which might be a nice combination of both of these suggestions.
PrevBlockError
PrevHashError
MerkleRootError
Maybe we can compile some sort of shared bitcoin/blockchain specific error codes that can be used across projects.
The text was updated successfully, but these errors were encountered:
ValidateSPV.validateHeaderChain()
returns one of three different errors if it fails.When using this externally, this means having to do a
switch/case
to handle for each of the different errors messages. Comparing string error messages is prone to breakage if the message ever changes, and not optimal.Suggestions:
1
is header length error2
is invalid chain3
is doesn't meet difficulty targetThis option is a bit more time intensive to implement, and also requires the user to lookup the error codes to know what they mean.
throw new TypeError('Header bytes not multiple of 80.')
throw new Error('Header bytes not a valid chain.')
-
throw new RangeError('Header does not meet its own difficulty target.')
The user can compare types of errors and handle it appropriately.
The obvious disadvantage to this is there is a limited amount of error types, and the
default
is never going to be reached since all error types areError
s. New error types can be constructed, which might be a nice combination of both of these suggestions.PrevBlockError
PrevHashError
MerkleRootError
Maybe we can compile some sort of shared bitcoin/blockchain specific error codes that can be used across projects.
The text was updated successfully, but these errors were encountered: