本文整理匯總了Golang中github.com/tendermint/tendermint/account.SignBytes函數的典型用法代碼示例。如果您正苦於以下問題:Golang SignBytes函數的具體用法?Golang SignBytes怎麽用?Golang SignBytes使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SignBytes函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Verify
// Returns whether +2/3 have prevoted/committed for BlockHash.
func (pol *POL) Verify(valSet *sm.ValidatorSet) error {
if uint(len(pol.Votes)) != valSet.Size() {
return fmt.Errorf("Invalid POL votes count: Expected %v, got %v",
valSet.Size(), len(pol.Votes))
}
talliedVotingPower := uint64(0)
prevoteDoc := account.SignBytes(&types.Vote{
Height: pol.Height, Round: pol.Round, Type: types.VoteTypePrevote,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
})
seenValidators := map[string]struct{}{}
for idx, vote := range pol.Votes {
// vote may be zero, in which case skip.
if vote.Signature.IsZero() {
continue
}
voteDoc := prevoteDoc
_, val := valSet.GetByIndex(uint(idx))
// Commit vote?
if vote.Round < pol.Round {
voteDoc = account.SignBytes(&types.Vote{
Height: pol.Height, Round: vote.Round, Type: types.VoteTypeCommit,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
})
} else if vote.Round > pol.Round {
return fmt.Errorf("Invalid commit round %v for POL %v", vote.Round, pol)
}
// Validate
if _, seen := seenValidators[string(val.Address)]; seen {
return fmt.Errorf("Duplicate validator for vote %v for POL %v", vote, pol)
}
if !val.PubKey.VerifyBytes(voteDoc, vote.Signature) {
return fmt.Errorf("Invalid signature for vote %v for POL %v", vote, pol)
}
// Tally
seenValidators[string(val.Address)] = struct{}{}
talliedVotingPower += val.VotingPower
}
if talliedVotingPower > valSet.TotalVotingPower()*2/3 {
return nil
} else {
return fmt.Errorf("Invalid POL, insufficient voting power %v, needed %v",
talliedVotingPower, (valSet.TotalVotingPower()*2/3 + 1))
}
}
示例2: SetProposal
func (cs *ConsensusState) SetProposal(proposal *Proposal) error {
cs.mtx.Lock()
defer cs.mtx.Unlock()
// Already have one
if cs.Proposal != nil {
return nil
}
// Does not apply
if proposal.Height != cs.Height || proposal.Round != cs.Round {
return nil
}
// We don't care about the proposal if we're already in RoundStepCommit.
if cs.Step == RoundStepCommit {
return nil
}
// Verify signature
if !cs.Validators.Proposer().PubKey.VerifyBytes(account.SignBytes(proposal), proposal.Signature) {
return ErrInvalidProposalSignature
}
cs.Proposal = proposal
cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockParts)
cs.ProposalPOLParts = types.NewPartSetFromHeader(proposal.POLParts)
return nil
}
示例3: SetProposal
func (cs *ConsensusState) SetProposal(proposal *types.Proposal) error {
cs.mtx.Lock()
defer cs.mtx.Unlock()
// Already have one
if cs.Proposal != nil {
return nil
}
// Does not apply
if proposal.Height != cs.Height || proposal.Round != cs.Round {
return nil
}
// We don't care about the proposal if we're already in RoundStepCommit.
if RoundStepCommit <= cs.Step {
return nil
}
// Verify POLRound, which must be -1 or between 0 and proposal.Round exclusive.
if proposal.POLRound != -1 &&
(proposal.POLRound < 0 || proposal.Round <= proposal.POLRound) {
return ErrInvalidProposalPOLRound
}
// Verify signature
if !cs.Validators.Proposer().PubKey.VerifyBytes(acm.SignBytes(cs.state.ChainID, proposal), proposal.Signature) {
return ErrInvalidProposalSignature
}
cs.Proposal = proposal
cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockPartsHeader)
return nil
}
示例4: Add
// True if added, false if not.
// Returns ErrVote[UnexpectedStep|InvalidAccount|InvalidSignature|InvalidBlockHash|ConflictingSignature]
// NOTE: vote should not be mutated after adding.
// Returns the validator index of the vote unless error is set.
func (voteSet *VoteSet) Add(address []byte, vote *types.Vote) (bool, uint, error) {
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
// Make sure the step matches. (or that vote is commit && round < voteSet.round)
if vote.Height != voteSet.height ||
(vote.Type != types.VoteTypeCommit && vote.Round != voteSet.round) ||
(vote.Type != types.VoteTypeCommit && vote.Type != voteSet.type_) ||
(vote.Type == types.VoteTypeCommit && voteSet.type_ != types.VoteTypeCommit && vote.Round >= voteSet.round) {
return false, 0, types.ErrVoteUnexpectedStep
}
// Ensure that signer is a validator.
valIndex, val := voteSet.valSet.GetByAddress(address)
if val == nil {
return false, 0, types.ErrVoteInvalidAccount
}
// Check signature.
if !val.PubKey.VerifyBytes(account.SignBytes(vote), vote.Signature) {
// Bad signature.
return false, 0, types.ErrVoteInvalidSignature
}
return voteSet.addVote(valIndex, vote)
}
示例5: TestSendTxSignable
func TestSendTxSignable(t *testing.T) {
sendTx := &SendTx{
Inputs: []*TxInput{
&TxInput{
Address: []byte("input1"),
Amount: 12345,
Sequence: 67890,
},
&TxInput{
Address: []byte("input2"),
Amount: 111,
Sequence: 222,
},
},
Outputs: []*TxOutput{
&TxOutput{
Address: []byte("output1"),
Amount: 333,
},
&TxOutput{
Address: []byte("output2"),
Amount: 444,
},
},
}
signBytes := account.SignBytes(sendTx)
signStr := string(signBytes)
expected := Fmt(`{"network":"%X","tx":[1,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"outputs":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
config.GetString("network"))
if signStr != expected {
t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
}
}
示例6: TestBondTxSignable
func TestBondTxSignable(t *testing.T) {
privAccount := account.GenPrivAccountFromKey([64]byte{})
bondTx := &BondTx{
PubKey: privAccount.PubKey.(account.PubKeyEd25519),
Inputs: []*TxInput{
&TxInput{
Address: []byte("input1"),
Amount: 12345,
Sequence: 67890,
},
&TxInput{
Address: []byte("input2"),
Amount: 111,
Sequence: 222,
},
},
UnbondTo: []*TxOutput{
&TxOutput{
Address: []byte("output1"),
Amount: 333,
},
&TxOutput{
Address: []byte("output2"),
Amount: 444,
},
},
}
signBytes := account.SignBytes(bondTx)
signStr := string(signBytes)
expected := Fmt(`{"network":"%X","tx":[17,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"pub_key":[1,"3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"],"unbond_to":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
config.GetString("network"))
if signStr != expected {
t.Errorf("Got unexpected sign string for BondTx")
}
}
示例7: Hash
func (data *Data) Hash() []byte {
if data.hash == nil {
bs := make([]interface{}, len(data.Txs))
for i, tx := range data.Txs {
bs[i] = acm.SignBytes(config.GetString("chain_id"), tx)
}
data.hash = merkle.SimpleHashFromBinaries(bs) // NOTE: leaves are TxIDs.
}
return data.hash
}
示例8: Hash
func (data *Data) Hash() []byte {
if data.hash == nil {
bs := make([]interface{}, len(data.Txs))
for i, tx := range data.Txs {
bs[i] = account.SignBytes(tx)
}
data.hash = merkle.HashFromBinaries(bs)
}
return data.hash
}
示例9: TestRebondTxSignable
func TestRebondTxSignable(t *testing.T) {
rebondTx := &RebondTx{
Address: []byte("address1"),
Height: 111,
}
signBytes := account.SignBytes(rebondTx)
signStr := string(signBytes)
expected := Fmt(`{"network":"%X","tx":[19,{"address":"6164647265737331","height":111}]}`,
config.GetString("network"))
if signStr != expected {
t.Errorf("Got unexpected sign string for RebondTx")
}
}
示例10: TestUnbondTxSignable
func TestUnbondTxSignable(t *testing.T) {
unbondTx := &UnbondTx{
Address: []byte("address1"),
Height: 111,
}
signBytes := acm.SignBytes(chainID, unbondTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","tx":[18,{"address":"6164647265737331","height":111}]}`,
config.GetString("chain_id"))
if signStr != expected {
t.Errorf("Got unexpected sign string for UnbondTx")
}
}
示例11: VerifyValidation
// Verify that +2/3 of the set had signed the given signBytes
func (valSet *ValidatorSet) VerifyValidation(chainID string,
hash []byte, parts PartSetHeader, height int, v *Validation) error {
if valSet.Size() != len(v.Precommits) {
return fmt.Errorf("Invalid validation -- wrong set size: %v vs %v", valSet.Size(), len(v.Precommits))
}
if height != v.Height() {
return fmt.Errorf("Invalid validation -- wrong height: %v vs %v", height, v.Height())
}
talliedVotingPower := int64(0)
round := v.Round()
for idx, precommit := range v.Precommits {
// may be nil if validator skipped.
if precommit == nil {
continue
}
if precommit.Height != height {
return fmt.Errorf("Invalid validation -- wrong height: %v vs %v", height, precommit.Height)
}
if precommit.Round != round {
return fmt.Errorf("Invalid validation -- wrong round: %v vs %v", round, precommit.Round)
}
if precommit.Type != VoteTypePrecommit {
return fmt.Errorf("Invalid validation -- not precommit @ index %v", idx)
}
_, val := valSet.GetByIndex(idx)
// Validate signature
precommitSignBytes := account.SignBytes(chainID, precommit)
if !val.PubKey.VerifyBytes(precommitSignBytes, precommit.Signature) {
return fmt.Errorf("Invalid validation -- invalid signature: %v", precommit)
}
if !bytes.Equal(precommit.BlockHash, hash) {
continue // Not an error, but doesn't count
}
if !parts.Equals(precommit.BlockPartsHeader) {
continue // Not an error, but doesn't count
}
// Good precommit!
talliedVotingPower += val.VotingPower
}
if talliedVotingPower > valSet.TotalVotingPower()*2/3 {
return nil
} else {
return fmt.Errorf("Invalid validation -- insufficient voting power: got %v, needed %v",
talliedVotingPower, (valSet.TotalVotingPower()*2/3 + 1))
}
}
示例12: TestProposalSignable
func TestProposalSignable(t *testing.T) {
proposal := &Proposal{
Height: 12345,
Round: 23456,
BlockParts: types.PartSetHeader{111, []byte("blockparts")},
POLParts: types.PartSetHeader{222, []byte("polparts")},
Signature: nil,
}
signBytes := account.SignBytes(proposal)
signStr := string(signBytes)
expected := Fmt(`{"network":"%X","proposal":{"block_parts":{"hash":"626C6F636B7061727473","total":111},"height":12345,"pol_parts":{"hash":"706F6C7061727473","total":222},"round":23456}}`,
config.GetString("network"))
if signStr != expected {
t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
}
}
示例13: TestProposalSignable
func TestProposalSignable(t *testing.T) {
proposal := &Proposal{
Height: 12345,
Round: 23456,
BlockPartsHeader: PartSetHeader{111, []byte("blockparts")},
POLRound: -1,
}
signBytes := acm.SignBytes(config.GetString("chain_id"), proposal)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","proposal":{"block_parts_header":{"hash":"626C6F636B7061727473","total":111},"height":12345,"pol_round":-1,"round":23456}}`,
config.GetString("chain_id"))
if signStr != expected {
t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
}
}
示例14: addVote
func (voteSet *VoteSet) addVote(val *Validator, valIndex int, vote *Vote) (bool, int, error) {
// Make sure the step matches. (or that vote is commit && round < voteSet.round)
if (vote.Height != voteSet.height) ||
(vote.Round != voteSet.round) ||
(vote.Type != voteSet.type_) {
return false, 0, ErrVoteUnexpectedStep
}
// Check signature.
if !val.PubKey.VerifyBytes(acm.SignBytes(config.GetString("chain_id"), vote), vote.Signature) {
// Bad signature.
return false, 0, ErrVoteInvalidSignature
}
// If vote already exists, return false.
if existingVote := voteSet.votes[valIndex]; existingVote != nil {
if bytes.Equal(existingVote.BlockHash, vote.BlockHash) {
return false, valIndex, nil
} else {
return false, valIndex, &ErrVoteConflictingSignature{
VoteA: existingVote,
VoteB: vote,
}
}
}
// Add vote.
voteSet.votes[valIndex] = vote
voteSet.votesBitArray.SetIndex(valIndex, true)
blockKey := string(vote.BlockHash) + string(wire.BinaryBytes(vote.BlockPartsHeader))
totalBlockHashVotes := voteSet.votesByBlock[blockKey] + val.VotingPower
voteSet.votesByBlock[blockKey] = totalBlockHashVotes
voteSet.totalVotes += val.VotingPower
// If we just nudged it up to two thirds majority, add it.
if totalBlockHashVotes > voteSet.valSet.TotalVotingPower()*2/3 &&
(totalBlockHashVotes-val.VotingPower) <= voteSet.valSet.TotalVotingPower()*2/3 {
voteSet.maj23Hash = vote.BlockHash
voteSet.maj23PartsHeader = vote.BlockPartsHeader
voteSet.maj23Exists = true
}
return true, valIndex, nil
}
示例15: SignRebondTx
func (privVal *PrivValidator) SignRebondTx(rebondTx *types.RebondTx) error {
privVal.mtx.Lock()
defer privVal.mtx.Unlock()
if privVal.LastHeight < rebondTx.Height {
// Persist height/round/step
privVal.LastHeight = rebondTx.Height
privVal.LastRound = math.MaxUint64 // We can't do anything else for this rebondTx.Height.
privVal.LastStep = math.MaxUint8
privVal.save()
// Sign
rebondTx.Signature = privVal.PrivKey.Sign(account.SignBytes(rebondTx)).(account.SignatureEd25519)
return nil
} else {
return errors.New(fmt.Sprintf("Attempt of duplicate signing of rebondTx: Height %v", rebondTx.Height))
}
}