Merge pull request #518 from nspcc-dev/feat/noerrors

core: do not return error in block.getHashableData
This commit is contained in:
Vsevolod 2019-11-26 16:20:46 +03:00 committed by GitHub
commit 34a38ccce1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 20 deletions

View file

@ -64,9 +64,7 @@ func (b *BlockBase) Hash() util.Uint256 {
// VerificationHash returns the hash of the block used to verify it. // VerificationHash returns the hash of the block used to verify it.
func (b *BlockBase) VerificationHash() util.Uint256 { func (b *BlockBase) VerificationHash() util.Uint256 {
if b.verificationHash.Equals(util.Uint256{}) { if b.verificationHash.Equals(util.Uint256{}) {
if b.createHash() != nil { b.createHash()
panic("failed to compute hash!")
}
} }
return b.verificationHash return b.verificationHash
} }
@ -94,13 +92,12 @@ func (b *BlockBase) EncodeBinary(bw *io.BinWriter) {
} }
// getHashableData returns serialized hashable data of the block. // getHashableData returns serialized hashable data of the block.
func (b *BlockBase) getHashableData() ([]byte, error) { func (b *BlockBase) getHashableData() []byte {
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
// No error can occure while encoding hashable fields.
b.encodeHashableFields(buf.BinWriter) b.encodeHashableFields(buf.BinWriter)
if buf.Err != nil {
return nil, buf.Err return buf.Bytes()
}
return buf.Bytes(), nil
} }
// createHash creates the hash of the block. // createHash creates the hash of the block.
@ -109,15 +106,10 @@ func (b *BlockBase) getHashableData() ([]byte, error) {
// version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner. // version, PrevBlock, MerkleRoot, timestamp, and height, the nonce, NextMiner.
// Since MerkleRoot already contains the hash value of all transactions, // Since MerkleRoot already contains the hash value of all transactions,
// the modification of transaction will influence the hash value of the block. // the modification of transaction will influence the hash value of the block.
func (b *BlockBase) createHash() error { func (b *BlockBase) createHash() {
bb, err := b.getHashableData() bb := b.getHashableData()
if err != nil {
return err
}
b.hash = hash.DoubleSha256(bb) b.hash = hash.DoubleSha256(bb)
b.verificationHash = hash.Sha256(bb) b.verificationHash = hash.Sha256(bb)
return nil
} }
// encodeHashableFields will only encode the fields used for hashing. // encodeHashableFields will only encode the fields used for hashing.
@ -146,6 +138,6 @@ func (b *BlockBase) decodeHashableFields(br *io.BinReader) {
// Make the hash of the block here so we dont need to do this // Make the hash of the block here so we dont need to do this
// again. // again.
if br.Err == nil { if br.Err == nil {
br.Err = b.createHash() b.createHash()
} }
} }

View file

@ -79,10 +79,7 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
if err != nil { if err != nil {
panic(err) panic(err)
} }
b, err := b.getHashableData() b := b.getHashableData()
if err != nil {
panic(err)
}
sig, err := pKey.Sign(b) sig, err := pKey.Sign(b)
if err != nil || len(sig) != 64 { if err != nil || len(sig) != 64 {
panic(err) panic(err)