tendermint/blocks/adjustment.go

167 lines
3.6 KiB
Go
Raw Normal View History

2014-06-05 02:34:45 -07:00
package blocks
import (
2014-07-01 14:50:24 -07:00
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"io"
2014-06-05 02:34:45 -07:00
)
/* Adjustment
1. Bond New validator posts a bond
2. Unbond Validator leaves
3. Timeout Validator times out
4. Dupeout Validator dupes out (signs twice)
TODO: signing a bad checkpoint (block)
*/
type Adjustment interface {
2014-07-01 14:50:24 -07:00
Type() Byte
Binary
2014-06-05 02:34:45 -07:00
}
const (
2014-07-01 14:50:24 -07:00
ADJ_TYPE_BOND = Byte(0x01)
ADJ_TYPE_UNBOND = Byte(0x02)
ADJ_TYPE_TIMEOUT = Byte(0x03)
ADJ_TYPE_DUPEOUT = Byte(0x04)
2014-06-05 02:34:45 -07:00
)
func ReadAdjustment(r io.Reader) Adjustment {
2014-07-01 14:50:24 -07:00
switch t := ReadByte(r); t {
case ADJ_TYPE_BOND:
return &Bond{
2014-08-31 01:48:40 -07:00
Fee: Readuint64(r),
UnbondTo: Readuint64(r),
Amount: Readuint64(r),
2014-07-01 14:50:24 -07:00
Signature: ReadSignature(r),
}
case ADJ_TYPE_UNBOND:
return &Unbond{
2014-08-31 01:48:40 -07:00
Fee: Readuint64(r),
Amount: Readuint64(r),
2014-07-01 14:50:24 -07:00
Signature: ReadSignature(r),
}
case ADJ_TYPE_TIMEOUT:
return &Timeout{
2014-08-31 01:48:40 -07:00
AccountId: Readuint64(r),
Penalty: Readuint64(r),
2014-07-01 14:50:24 -07:00
}
case ADJ_TYPE_DUPEOUT:
return &Dupeout{
2014-08-10 16:35:08 -07:00
VoteA: ReadBlockVote(r),
VoteB: ReadBlockVote(r),
2014-07-01 14:50:24 -07:00
}
default:
Panicf("Unknown Adjustment type %x", t)
return nil
}
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
2014-08-10 16:35:08 -07:00
/* Bond < Adjustment */
2014-06-05 02:34:45 -07:00
type Bond struct {
2014-08-31 01:48:40 -07:00
Fee uint64
UnbondTo uint64
Amount uint64
2014-07-01 14:50:24 -07:00
Signature
2014-06-05 02:34:45 -07:00
}
func (self *Bond) Type() Byte {
2014-07-01 14:50:24 -07:00
return ADJ_TYPE_BOND
2014-06-05 02:34:45 -07:00
}
func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Type(), w, n, err)
2014-08-31 01:48:40 -07:00
n, err = WriteTo(UInt64(self.Fee), w, n, err)
n, err = WriteTo(UInt64(self.UnbondTo), w, n, err)
n, err = WriteTo(UInt64(self.Amount), w, n, err)
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Signature, w, n, err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
2014-08-10 16:35:08 -07:00
/* Unbond < Adjustment */
2014-06-05 02:34:45 -07:00
type Unbond struct {
2014-08-31 01:48:40 -07:00
Fee uint64
Amount uint64
2014-07-01 14:50:24 -07:00
Signature
2014-06-05 02:34:45 -07:00
}
func (self *Unbond) Type() Byte {
2014-07-01 14:50:24 -07:00
return ADJ_TYPE_UNBOND
2014-06-05 02:34:45 -07:00
}
func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Type(), w, n, err)
2014-08-31 01:48:40 -07:00
n, err = WriteTo(UInt64(self.Fee), w, n, err)
n, err = WriteTo(UInt64(self.Amount), w, n, err)
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Signature, w, n, err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
//-----------------------------------------------------------------------------
2014-06-05 02:34:45 -07:00
2014-08-10 16:35:08 -07:00
/* Timeout < Adjustment */
2014-06-05 02:34:45 -07:00
type Timeout struct {
2014-08-31 01:48:40 -07:00
AccountId uint64
Penalty uint64
2014-06-05 02:34:45 -07:00
}
func (self *Timeout) Type() Byte {
2014-07-01 14:50:24 -07:00
return ADJ_TYPE_TIMEOUT
2014-06-05 02:34:45 -07:00
}
func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) {
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Type(), w, n, err)
2014-08-31 01:48:40 -07:00
n, err = WriteTo(UInt64(self.AccountId), w, n, err)
n, err = WriteTo(UInt64(self.Penalty), w, n, err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}
2014-08-10 16:35:08 -07:00
//-----------------------------------------------------------------------------
/*
The full vote structure is only needed when presented as evidence.
Typically only the signature is passed around, as the hash & height are implied.
*/
type BlockVote struct {
2014-08-31 01:48:40 -07:00
Height uint64
2014-08-10 16:35:08 -07:00
BlockHash ByteSlice
Signature
}
func ReadBlockVote(r io.Reader) BlockVote {
return BlockVote{
2014-08-31 01:48:40 -07:00
Height: Readuint64(r),
2014-08-10 16:35:08 -07:00
BlockHash: ReadByteSlice(r),
Signature: ReadSignature(r),
}
}
2014-06-05 02:34:45 -07:00
2014-08-10 16:35:08 -07:00
func (self BlockVote) WriteTo(w io.Writer) (n int64, err error) {
2014-08-31 01:48:40 -07:00
n, err = WriteTo(UInt64(self.Height), w, n, err)
2014-08-10 16:35:08 -07:00
n, err = WriteTo(self.BlockHash, w, n, err)
n, err = WriteTo(self.Signature, w, n, err)
return
}
/* Dupeout < Adjustment */
2014-06-05 02:34:45 -07:00
type Dupeout struct {
2014-08-10 16:35:08 -07:00
VoteA BlockVote
VoteB BlockVote
2014-06-05 02:34:45 -07:00
}
func (self *Dupeout) Type() Byte {
2014-07-01 14:50:24 -07:00
return ADJ_TYPE_DUPEOUT
2014-06-05 02:34:45 -07:00
}
func (self *Dupeout) WriteTo(w io.Writer) (n int64, err error) {
2014-07-18 21:21:42 -07:00
n, err = WriteTo(self.Type(), w, n, err)
n, err = WriteTo(self.VoteA, w, n, err)
n, err = WriteTo(self.VoteB, w, n, err)
2014-07-01 14:50:24 -07:00
return
2014-06-05 02:34:45 -07:00
}