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

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.swp
.bak
tendermint

View File

@ -65,6 +65,10 @@ func ReadByte(r io.Reader) Byte {
return b
}
func Readbyte(r io.Reader) byte {
return byte(ReadByte(r))
}
// Int8
func (self Int8) Equals(other interface{}) bool {
@ -113,6 +117,10 @@ func ReadInt8(r io.Reader) Int8 {
return b
}
func Readint8(r io.Reader) int8 {
return int8(ReadInt8(r))
}
// UInt8
func (self UInt8) Equals(other interface{}) bool {
@ -161,6 +169,10 @@ func ReadUInt8(r io.Reader) UInt8 {
return b
}
func Readuint8(r io.Reader) uint8 {
return uint8(ReadUInt8(r))
}
// Int16
func (self Int16) Equals(other interface{}) bool {
@ -211,6 +223,10 @@ func ReadInt16(r io.Reader) Int16 {
return b
}
func Readint16(r io.Reader) int16 {
return int16(ReadInt16(r))
}
// UInt16
func (self UInt16) Equals(other interface{}) bool {
@ -261,6 +277,10 @@ func ReadUInt16(r io.Reader) UInt16 {
return b
}
func Readuint16(r io.Reader) uint16 {
return uint16(ReadUInt16(r))
}
// Int32
func (self Int32) Equals(other interface{}) bool {
@ -311,6 +331,10 @@ func ReadInt32(r io.Reader) Int32 {
return b
}
func Readint32(r io.Reader) int32 {
return int32(ReadInt32(r))
}
// UInt32
func (self UInt32) Equals(other interface{}) bool {
@ -361,6 +385,10 @@ func ReadUInt32(r io.Reader) UInt32 {
return b
}
func Readuint32(r io.Reader) uint32 {
return uint32(ReadUInt32(r))
}
// Int64
func (self Int64) Equals(other interface{}) bool {
@ -411,6 +439,10 @@ func ReadInt64(r io.Reader) Int64 {
return b
}
func Readint64(r io.Reader) int64 {
return int64(ReadInt64(r))
}
// UInt64
func (self UInt64) Equals(other interface{}) bool {
@ -460,3 +492,7 @@ func ReadUInt64(r io.Reader) UInt64 {
}
return b
}
func Readuint64(r io.Reader) uint64 {
return uint64(ReadUInt64(r))
}

View File

@ -62,3 +62,7 @@ func ReadString(r io.Reader) (str String) {
}
return str
}
func Readstring(r io.Reader) (str string) {
return string(ReadString(r))
}

View File

@ -7,7 +7,7 @@ import (
// NOTE: consensus/Validator embeds this, so..
type Account struct {
Id UInt64 // Numeric id of account, incrementing.
Id uint64 // Numeric id of account, incrementing.
PubKey ByteSlice
}
@ -41,13 +41,13 @@ It usually follows the message to be signed.
*/
type Signature struct {
SignerId UInt64
SignerId uint64
Bytes ByteSlice
}
func ReadSignature(r io.Reader) Signature {
return Signature{
SignerId: ReadUInt64(r),
SignerId: Readuint64(r),
Bytes: ReadByteSlice(r),
}
}
@ -57,7 +57,7 @@ func (sig Signature) IsZero() bool {
}
func (sig Signature) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(sig.SignerId, w, n, err)
n, err = WriteTo(UInt64(sig.SignerId), w, n, err)
n, err = WriteTo(sig.Bytes, w, n, err)
return
}

View File

@ -82,8 +82,8 @@ func (b *Block) ToBlockParts() (parts []*BlockPart) {
copy(partBytes, blockBytes[start:end]) // Do not ref the original byteslice.
part := &BlockPart{
Height: b.Height,
Index: UInt16(i),
Total: UInt16(total),
Index: uint16(i),
Total: uint16(total),
Bytes: partBytes,
Signature: Signature{}, // No signature.
}
@ -100,10 +100,10 @@ Each block is divided into fixed length chunks (e.g. 4Kb)
for faster propagation across the gossip network.
*/
type BlockPart struct {
Height UInt32
Round UInt16 // Add Round? Well I need to know...
Index UInt16
Total UInt16
Height uint32
Round uint16 // Add Round? Well I need to know...
Index uint16
Total uint16
Bytes ByteSlice
Signature
@ -113,22 +113,22 @@ type BlockPart struct {
func ReadBlockPart(r io.Reader) *BlockPart {
return &BlockPart{
Height: ReadUInt32(r),
Round: ReadUInt16(r),
Index: ReadUInt16(r),
Total: ReadUInt16(r),
Height: Readuint32(r),
Round: Readuint16(r),
Index: Readuint16(r),
Total: Readuint16(r),
Bytes: ReadByteSlice(r),
Signature: ReadSignature(r),
}
}
func (bp *BlockPart) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(&bp.Height, w, n, err)
n, err = WriteTo(&bp.Round, w, n, err)
n, err = WriteTo(&bp.Index, w, n, err)
n, err = WriteTo(&bp.Total, w, n, err)
n, err = WriteTo(&bp.Bytes, w, n, err)
n, err = WriteTo(&bp.Signature, w, n, err)
n, err = WriteTo(UInt32(bp.Height), w, n, err)
n, err = WriteTo(UInt16(bp.Round), w, n, err)
n, err = WriteTo(UInt16(bp.Index), w, n, err)
n, err = WriteTo(UInt16(bp.Total), w, n, err)
n, err = WriteTo(bp.Bytes, w, n, err)
n, err = WriteTo(bp.Signature, w, n, err)
return
}
@ -174,8 +174,8 @@ func (bp *BlockPart) ValidateWithSigner(signer *Account) error {
/* Header is part of a Block */
type Header struct {
Name String
Height UInt32
Fees UInt64
Height uint32
Fees uint64
Time Time
PrevHash ByteSlice
ValidationHash ByteSlice
@ -188,8 +188,8 @@ type Header struct {
func ReadHeader(r io.Reader) Header {
return Header{
Name: ReadString(r),
Height: ReadUInt32(r),
Fees: ReadUInt64(r),
Height: Readuint32(r),
Fees: Readuint64(r),
Time: ReadTime(r),
PrevHash: ReadByteSlice(r),
ValidationHash: ReadByteSlice(r),
@ -199,8 +199,8 @@ func ReadHeader(r io.Reader) Header {
func (h *Header) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(h.Name, w, n, err)
n, err = WriteTo(h.Height, w, n, err)
n, err = WriteTo(h.Fees, w, n, err)
n, err = WriteTo(UInt32(h.Height), w, n, err)
n, err = WriteTo(UInt64(h.Fees), w, n, err)
n, err = WriteTo(h.Time, w, n, err)
n, err = WriteTo(h.PrevHash, w, n, err)
n, err = WriteTo(h.ValidationHash, w, n, err)
@ -232,14 +232,14 @@ type Validation struct {
}
func ReadValidation(r io.Reader) Validation {
numSigs := int(ReadUInt32(r))
numAdjs := int(ReadUInt32(r))
numSigs := Readuint32(r)
numAdjs := Readuint32(r)
sigs := make([]Signature, 0, numSigs)
for i := 0; i < numSigs; i++ {
for i := uint32(0); i < numSigs; i++ {
sigs = append(sigs, ReadSignature(r))
}
adjs := make([]Adjustment, 0, numAdjs)
for i := 0; i < numAdjs; i++ {
for i := uint32(0); i < numAdjs; i++ {
adjs = append(adjs, ReadAdjustment(r))
}
return Validation{
@ -283,9 +283,9 @@ type Txs struct {
}
func ReadTxs(r io.Reader) Txs {
numTxs := int(ReadUInt32(r))
numTxs := Readuint32(r)
txs := make([]Tx, 0, numTxs)
for i := 0; i < numTxs; i++ {
for i := uint32(0); i < numTxs; i++ {
txs = append(txs, ReadTx(r))
}
return Txs{Txs: txs}

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
}