Skip to content
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

feat: store proof, round on block #45

Merged
merged 9 commits into from
Apr 2, 2020
Merged

Conversation

egonspace
Copy link
Contributor

@egonspace egonspace commented Mar 27, 2020

closes #7
closes #8

Added two field of round and proof in block.

  • modified createBlock adding vrf proof
  • modified verifyBlock verifying vrf proof

Testing

// build link using our tendermint
// go to link repository and replace tendermint module to ours
$ go mod edit -replace github.com/tendermint/tendermint=/Users/user/go/src/github.com/line/tendermint
$ make build; make install

// init blockchain and start linkd
$ sh .initialize
$ linkd start

// verifying block structure
// you can see round and proof in a block
$linkcli query block
{
  "block_id": {
    "hash": "BD2C84A3AB80C3A45539F59AE49375B38D37347572A8DE510DDBB649091C8781",
    "parts": {
      "total": "1",
      "hash": "6FE324361BBE21A863EAB91FAF2F467846515636E657EFB2A7B7DE35DC9512A7"
    }
  },
  "block": {
    "header": {
      "version": {
        "block": "10",
        "app": "0"
      },
      "chain_id": "link",
      "height": "3",
      "time": "2020-03-26T23:38:12.903301Z",
      "last_block_id": {
        "hash": "6EDBD65DF5CF48D3B6B51327B9AC583A7C493E842135B81B0390E3A0FD7116BE",
        "parts": {
          "total": "1",
          "hash": "375921117E7123F7DFA2E91336862F1A2320E0D60D205904EB3D62BA576A66FB"
        }
      },
      "last_commit_hash": "EE80912725DD3064E72D017ADE4009B20085F1DBC0D1EB1FA6AF69A86A1EC14E",
      "data_hash": "",
      "validators_hash": "AB01E18C20A4215119834DD01ED20A933EDA520984085FCBA70212067E434AAC",
      "next_validators_hash": "AB01E18C20A4215119834DD01ED20A933EDA520984085FCBA70212067E434AAC",
      "consensus_hash": "048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F",
      "app_hash": "A6D82028BE639725C2BC0AA8367FADA20681CB409A1F9DDEFB3A8675C1232284",
      "last_results_hash": "",
      "evidence_hash": "",
      "proposer_address": "D8C77E2F207738DC7043175548C24FE39436F177",
      "round": "0",
      "proof": "0312BABECD883B76714B705E5F7CDDA467218199B6F900D9B3D54CE189DB5B808D29A494CFFAAE0539BB3567A5D7D1115E074C00C9A761FE4BFE85C4D6EEF8019DBDA93F03F0AF753F71DF881E733EC955"
    },
    "data": {
      "txs": null
    },
    "evidence": {
      "evidence": null
    },
    "last_commit": {
      "height": "2",
      "round": "0",
      "block_id": {
        "hash": "6EDBD65DF5CF48D3B6B51327B9AC583A7C493E842135B81B0390E3A0FD7116BE",
        "parts": {
          "total": "1",
          "hash": "375921117E7123F7DFA2E91336862F1A2320E0D60D205904EB3D62BA576A66FB"
        }
      },
      "signatures": [
        {
          "block_id_flag": 2,
          "validator_address": "D8C77E2F207738DC7043175548C24FE39436F177",
          "timestamp": "2020-03-26T23:38:12.903301Z",
          "signature": "G2utCelTWSYfWHZ68KEOLy5b+miWTQB6Y1fAT7+BGqeZnfXUhzExfA6H0sQ6rVCjsCBh79OAB9m5P6vbKpwODA=="
        }
      ]
    }
  }
}


  • Referenced an issue explaining the need for the change
  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG_PENDING.md

@egonspace egonspace requested review from torao and zemyblue March 27, 2020 00:05
@egonspace egonspace self-assigned this Mar 27, 2020
@egonspace egonspace added the C: enhancement Classification: New feature or its request, or improvement in maintainability of code label Mar 27, 2020
state/state.go Outdated
Comment on lines 104 to 109
b := make([]byte, 16)
binary.PutVarint(b, state.LastBlockHeight)
binary.PutVarint(b[8:], int64(round))
hash := tmhash.New()
hash.Write(seed)
return hash.Sum(b), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Meaning of this codes hash + height + round?
Thus, can other language like java make hash by this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. hash.Write() and hash.Sum() is standard function of sha256 so other language can implement it.
tmhash is widely used in tendermint. If tmhash is not possible in other language, all other hash function has same problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thmash of Tendermint appears to be SHA-256, In a 64-bit environment, SHA-512 is significantly faster. How about it? (I think we should make a better decision than following Tendermint as long as it's substantially advantageous in the scope of standard technology.)

return types.NewErrInvalidProof(err.Error())
}
_, val := state.Validators.GetByAddress(block.ProposerAddress)
verified, err := vrf.Verify(val.PubKey.(ed25519.PubKeyEd25519), block.Proof.Bytes(), message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If err is not nil, it does not handle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

Copy link
Member

@zemyblue zemyblue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -345,6 +346,10 @@ type Header struct {
// consensus info
EvidenceHash tmbytes.HexBytes `json:"evidence_hash"` // evidence included in the block
ProposerAddress Address `json:"proposer_address"` // original proposer of the block

// vrf info
Round int `json:"round"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to define the type as uint32 since the int in golang varies in size depending on the compilation environment, and we don't need a sign. Related this, it's also better to be the type of the parameter using round in this PR from int to uint32.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoundState.Round is already defined as int. I think it's better to define the type same to RoundState.Round.

state/state.go Outdated
}
b := make([]byte, 16)
binary.PutVarint(b, state.LastBlockHeight)
binary.PutVarint(b[8:], int64(round))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PutVarint() is a variable-length encoding feature, and the write range may exceed 8byte depending on the output value. Since variable-length encoding isn't particularly useful here and only unnecessarily reduces portability, I think it's better to be explicitly endian and size.

binary.LittleEndian.PutUint64(b, uint64(state.LastBlockHeight))
binary.LittleEndian.PutUint64(b[8:], uint64(round))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I apply it.

state/state.go Outdated
Comment on lines 104 to 109
b := make([]byte, 16)
binary.PutVarint(b, state.LastBlockHeight)
binary.PutVarint(b[8:], int64(round))
hash := tmhash.New()
hash.Write(seed)
return hash.Sum(b), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thmash of Tendermint appears to be SHA-256, In a 64-bit environment, SHA-512 is significantly faster. How about it? (I think we should make a better decision than following Tendermint as long as it's substantially advantageous in the scope of standard technology.)

@egonspace
Copy link
Contributor Author

It's good suggestion. If we decide to use sha512, then we should just modify following code.

func New() hash.Hash {
	return sha256.New()  // -> sha512.New()
}

@zemyblue
Copy link
Member

It's good suggestion. If we decide to use sha512, then we should just modify following code.

func New() hash.Hash {
	return sha256.New()  // -> sha512.New()
}

Besides this, I think there seems to be more to check.
Thus if this has many changes, how about handling in another PR?

@zemyblue
Copy link
Member

@egonspace, please change the merge target to develop

@egonspace egonspace changed the base branch from master to develop March 30, 2020 01:57
@egonspace
Copy link
Contributor Author

@egonspace, please change the merge target to develop

Ah, thank you. Done.

@egonspace
Copy link
Contributor Author

It's good suggestion. If we decide to use sha512, then we should just modify following code.

func New() hash.Hash {
	return sha256.New()  // -> sha512.New()
}

Besides this, I think there seems to be more to check.
Thus if this has many changes, how about handling in another PR?

Sure. I think so.

@torao
Copy link
Contributor

torao commented Apr 2, 2020

My previous comment was the idea that intended to make the SHA-512 only new features that we add with VRF. If we replace the Tendermint's func New() hash.Hash, I think New512_256() is better because SHA-256 and SHA-512 have different byte sizes.

@egonspace
Copy link
Contributor Author

My previous comment was the idea that intended to make the SHA-512 only new features that we add with VRF. If we replace the Tendermint's func New() hash.Hash, I think New512_256() is better because SHA-256 and SHA-512 have different byte sizes.

Well, I think the reason they defined tmhash is to unify all hash functions in tendermint into one. There is no reason to use 512 in some cases and 256 in others. I think it's better to change tmhash if 512 is judged to be better in every way.

@egonspace egonspace merged commit 0168be6 into develop Apr 2, 2020
@egonspace egonspace mentioned this pull request Apr 8, 2020
5 tasks
@egonspace egonspace deleted the feature/proof_in_block branch June 22, 2020 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: enhancement Classification: New feature or its request, or improvement in maintainability of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What to use as a seed for VRF random number generation? Add VRF proof and output in proposal block
3 participants