numeric fields are all native unboxed.

This commit is contained in:
Jae Kwon
2014-08-30 16:28:51 -07:00
parent d300a67bb1
commit fa382a3b05
9 changed files with 131 additions and 90 deletions

View File

@@ -168,8 +168,8 @@ func (cm *ConsensusManager) switchEventsRoutine() {
func (cm *ConsensusManager) makeKnownBlockPartsMessage() *KnownBlockPartsMessage {
rs := cm.csc.RoundState()
return &KnownBlockPartsMessage{
Height: UInt32(rs.Height),
SecondsSinceStartTime: UInt32(time.Now().Sub(rs.StartTime).Seconds()),
Height: rs.Height,
SecondsSinceStartTime: uint32(time.Now().Sub(rs.StartTime).Seconds()),
BlockPartsBitArray: rs.BlockPartSet.BitArray(),
}
}
@@ -203,8 +203,8 @@ OUTER_LOOP:
msg := msg_.(*BlockPartMessage)
// Add the block part if the height matches.
if uint32(msg.BlockPart.Height) == rs.Height &&
uint16(msg.BlockPart.Round) == rs.Round {
if msg.BlockPart.Height == rs.Height &&
msg.BlockPart.Round == rs.Round {
// TODO Continue if we've already voted, then no point processing the part.
@@ -471,7 +471,7 @@ func (cm *ConsensusManager) commitBlock(blockPartSet *BlockPartSet, commitTime t
block, blockParts := blockPartSet.Block(), blockPartSet.BlockParts()
err := cm.blockStore.SaveBlockParts(uint32(block.Height), blockParts)
err := cm.blockStore.SaveBlockParts(block.Height, blockParts)
if err != nil {
return err
}
@@ -684,9 +684,9 @@ func (ps *PeerState) WantsBlockPart(part *BlockPart) bool {
ps.mtx.Lock()
defer ps.mtx.Unlock()
// Only wants the part if peer's current height and round matches.
if ps.height == uint32(part.Height) {
if ps.height == part.Height {
round, _, _, _, elapsedRatio := calcRoundInfo(ps.startTime)
if round == uint16(part.Round) && elapsedRatio < roundDeadlineBare {
if round == part.Round && elapsedRatio < roundDeadlineBare {
// Only wants the part if it doesn't already have it.
if ps.blockPartsBitArray[part.Index/8]&byte(1<<(part.Index%8)) == 0 {
return true
@@ -700,7 +700,7 @@ func (ps *PeerState) WantsVote(vote *Vote) bool {
ps.mtx.Lock()
defer ps.mtx.Unlock()
// Only wants the vote if votesWanted says so
if ps.votesWanted[uint64(vote.SignerId)] <= 0 {
if ps.votesWanted[vote.SignerId] <= 0 {
// TODO: sometimes, send unsolicited votes to see if peer wants it.
return false
}
@@ -724,10 +724,10 @@ func (ps *PeerState) ApplyKnownBlockPartsMessage(msg *KnownBlockPartsMessage) er
ps.mtx.Lock()
defer ps.mtx.Unlock()
// TODO: Sanity check len(BlockParts)
if uint32(msg.Height) < ps.height {
if msg.Height < ps.height {
return ErrPeerStateHeightRegression
}
if uint32(msg.Height) == ps.height {
if msg.Height == ps.height {
if len(ps.blockPartsBitArray) == 0 {
ps.blockPartsBitArray = msg.BlockPartsBitArray
} else if len(msg.BlockPartsBitArray) > 0 {
@@ -756,7 +756,7 @@ func (ps *PeerState) ApplyKnownBlockPartsMessage(msg *KnownBlockPartsMessage) er
return ErrPeerStateInvalidStartTime
}
ps.startTime = newStartTime
ps.height = uint32(msg.Height)
ps.height = msg.Height
ps.blockPartsBitArray = msg.BlockPartsBitArray
}
return nil
@@ -822,23 +822,23 @@ func (m *BlockPartMessage) String() string {
//-------------------------------------
type KnownBlockPartsMessage struct {
Height UInt32
SecondsSinceStartTime UInt32
Height uint32
SecondsSinceStartTime uint32
BlockPartsBitArray ByteSlice
}
func readKnownBlockPartsMessage(r io.Reader) *KnownBlockPartsMessage {
return &KnownBlockPartsMessage{
Height: ReadUInt32(r),
SecondsSinceStartTime: ReadUInt32(r),
Height: Readuint32(r),
SecondsSinceStartTime: Readuint32(r),
BlockPartsBitArray: ReadByteSlice(r),
}
}
func (m *KnownBlockPartsMessage) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(msgTypeKnownBlockParts, w, n, err)
n, err = WriteTo(m.Height, w, n, err)
n, err = WriteTo(m.SecondsSinceStartTime, w, n, err)
n, err = WriteTo(UInt32(m.Height), w, n, err)
n, err = WriteTo(UInt32(m.SecondsSinceStartTime), w, n, err)
n, err = WriteTo(m.BlockPartsBitArray, w, n, err)
return
}
@@ -852,21 +852,21 @@ func (m *KnownBlockPartsMessage) String() string {
// XXX use this.
type VoteRankMessage struct {
ValidatorId UInt64
Rank UInt8
ValidatorId uint64
Rank uint8
}
func readVoteRankMessage(r io.Reader) *VoteRankMessage {
return &VoteRankMessage{
ValidatorId: ReadUInt64(r),
Rank: ReadUInt8(r),
ValidatorId: Readuint64(r),
Rank: Readuint8(r),
}
}
func (m *VoteRankMessage) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(msgTypeVoteRank, w, n, err)
n, err = WriteTo(m.ValidatorId, w, n, err)
n, err = WriteTo(m.Rank, w, n, err)
n, err = WriteTo(UInt64(m.ValidatorId), w, n, err)
n, err = WriteTo(UInt8(m.Rank), w, n, err)
return
}

View File

@@ -50,7 +50,7 @@ We omit details of dealing with membership changes.
*/
func getProposer(validators map[uint64]*Validator) (proposer *Validator) {
highestAccum := Int64(0)
highestAccum := int64(0)
for _, validator := range validators {
if validator.Accum > highestAccum {
highestAccum = validator.Accum
@@ -65,18 +65,18 @@ func getProposer(validators map[uint64]*Validator) (proposer *Validator) {
}
func incrementAccum(validators map[uint64]*Validator) {
totalDelta := UInt64(0)
totalDelta := int64(0)
for _, validator := range validators {
validator.Accum += Int64(validator.VotingPower)
totalDelta += validator.VotingPower
validator.Accum += int64(validator.VotingPower)
totalDelta += int64(validator.VotingPower)
}
proposer := getProposer(validators)
proposer.Accum -= Int64(totalDelta)
proposer.Accum -= totalDelta
// NOTE: sum(validators) here should be zero.
if true {
totalAccum := int64(0)
for _, validator := range validators {
totalAccum += int64(validator.Accum)
totalAccum += validator.Accum
}
if totalAccum != 0 {
Panicf("Total Accum of validators did not equal 0. Got: ", totalAccum)
@@ -90,7 +90,7 @@ func incrementAccum(validators map[uint64]*Validator) {
func copyValidators(validators map[uint64]*Validator) map[uint64]*Validator {
mapCopy := map[uint64]*Validator{}
for _, val := range validators {
mapCopy[uint64(val.Id)] = val.Copy()
mapCopy[val.Id] = val.Copy()
}
return mapCopy
}
@@ -133,14 +133,14 @@ func (csc *ConsensusStateControl) Load() {
csc.setupHeight(height, validators, startTime)
} else {
reader := bytes.NewReader(buf)
height := ReadUInt32(reader)
height := Readuint32(reader)
validators := make(map[uint64]*Validator)
startTime := ReadTime(reader)
for reader.Len() > 0 {
validator := ReadValidator(reader)
validators[uint64(validator.Id)] = validator
validators[validator.Id] = validator
}
csc.setupHeight(uint32(height), validators, startTime.Time)
csc.setupHeight(height, validators, startTime.Time)
}
}
@@ -212,7 +212,7 @@ func (csc *ConsensusStateControl) CommitBlock(block *Block, commitTime time.Time
csc.mtx.Lock()
defer csc.mtx.Unlock()
// Ensure that block is the next block needed.
if uint32(block.Height) != csc.height {
if block.Height != csc.height {
return Errorf("Cannot commit block %v to csc. Expected height %v", block, csc.height+1)
}
// Update validator.
@@ -221,7 +221,7 @@ func (csc *ConsensusStateControl) CommitBlock(block *Block, commitTime time.Time
// TODO if there are new validators in the block, add them.
// XXX: it's not commitTime we want...
csc.setupHeight(uint32(block.Height)+1, validators, commitTime)
csc.setupHeight(block.Height+1, validators, commitTime)
// Save the state.
csc.Save()

View File

@@ -13,21 +13,21 @@ import (
// Meant to be discarded every round of the consensus protocol.
type Validator struct {
Account
BondHeight UInt32
VotingPower UInt64
Accum Int64
BondHeight uint32
VotingPower uint64
Accum int64
}
// Used to persist the state of ConsensusStateControl.
func ReadValidator(r io.Reader) *Validator {
return &Validator{
Account: Account{
Id: ReadUInt64(r),
Id: Readuint64(r),
PubKey: ReadByteSlice(r),
},
BondHeight: ReadUInt32(r),
VotingPower: ReadUInt64(r),
Accum: ReadInt64(r),
BondHeight: Readuint32(r),
VotingPower: Readuint64(r),
Accum: Readint64(r),
}
}
@@ -43,11 +43,11 @@ func (v *Validator) Copy() *Validator {
// Used to persist the state of ConsensusStateControl.
func (v *Validator) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(&v.Id, w, n, err)
n, err = WriteTo(&v.PubKey, w, n, err)
n, err = WriteTo(&v.BondHeight, w, n, err)
n, err = WriteTo(&v.VotingPower, w, n, err)
n, err = WriteTo(&v.Accum, w, n, err)
n, err = WriteTo(UInt64(v.Id), w, n, err)
n, err = WriteTo(v.PubKey, w, n, err)
n, err = WriteTo(UInt32(v.BondHeight), w, n, err)
n, err = WriteTo(UInt64(v.VotingPower), w, n, err)
n, err = WriteTo(Int64(v.Accum), w, n, err)
return
}

View File

@@ -37,9 +37,9 @@ type Vote struct {
func ReadVote(r io.Reader) *Vote {
return &Vote{
Height: uint32(ReadUInt32(r)),
Round: uint16(ReadUInt16(r)),
Type: byte(ReadByte(r)),
Height: Readuint32(r),
Round: Readuint16(r),
Type: Readbyte(r),
Hash: ReadByteSlice(r),
Signature: ReadSignature(r),
}
@@ -115,7 +115,7 @@ type VoteSet struct {
func NewVoteSet(height uint32, round uint16, type_ byte, validators map[uint64]*Validator) *VoteSet {
totalVotingPower := uint64(0)
for _, val := range validators {
totalVotingPower += uint64(val.VotingPower)
totalVotingPower += val.VotingPower
}
return &VoteSet{
height: height,
@@ -140,7 +140,7 @@ func (vs *VoteSet) AddVote(vote *Vote) (bool, error) {
return false, ErrVoteUnexpectedPhase
}
val := vs.validators[uint64(vote.SignerId)]
val := vs.validators[vote.SignerId]
// Ensure that signer is a validator.
if val == nil {
return false, ErrVoteInvalidAccount
@@ -151,16 +151,16 @@ func (vs *VoteSet) AddVote(vote *Vote) (bool, error) {
return false, ErrVoteInvalidSignature
}
// If vote already exists, return false.
if existingVote, ok := vs.votes[uint64(vote.SignerId)]; ok {
if existingVote, ok := vs.votes[vote.SignerId]; ok {
if bytes.Equal(existingVote.Hash, vote.Hash) {
return false, nil
} else {
return false, ErrVoteConflictingSignature
}
}
vs.votes[uint64(vote.SignerId)] = vote
vs.votesByHash[string(vote.Hash)] += uint64(val.VotingPower)
vs.totalVotes += uint64(val.VotingPower)
vs.votes[vote.SignerId] = vote
vs.votesByHash[string(vote.Hash)] += val.VotingPower
vs.totalVotes += val.VotingPower
return true, nil
}
@@ -169,7 +169,7 @@ func (vs *VoteSet) AddVote(vote *Vote) (bool, error) {
func (vs *VoteSet) TwoThirdsMajority() (hash []byte, ok bool) {
vs.mtx.Lock()
defer vs.mtx.Unlock()
twoThirdsMajority := (vs.totalVotingPower*uint64(2) + uint64(2)) / uint64(3)
twoThirdsMajority := (vs.totalVotingPower*2 + 2) / 3
if vs.totalVotes < twoThirdsMajority {
return nil, false
}
@@ -190,7 +190,7 @@ func (vs *VoteSet) TwoThirdsMajority() (hash []byte, ok bool) {
func (vs *VoteSet) OneThirdMajority() (hashes []interface{}) {
vs.mtx.Lock()
defer vs.mtx.Unlock()
oneThirdMajority := (vs.totalVotingPower + uint64(2)) / uint64(3)
oneThirdMajority := (vs.totalVotingPower + 2) / 3
if vs.totalVotes < oneThirdMajority {
return nil
}