tendermint/blocks/adjustment.go

143 lines
3.1 KiB
Go
Raw Normal View History

2014-06-05 02:34:45 -07:00
package blocks
import (
2014-06-17 01:28:43 -07:00
. "github.com/tendermint/tendermint/common"
2014-06-05 02:34:45 -07:00
. "github.com/tendermint/tendermint/binary"
"io"
)
/* 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 {
Type() Byte
Binary
}
const (
ADJ_TYPE_BOND = Byte(0x01)
ADJ_TYPE_UNBOND = Byte(0x02)
ADJ_TYPE_TIMEOUT = Byte(0x03)
2014-06-05 11:04:56 -07:00
ADJ_TYPE_DUPEOUT = Byte(0x04)
2014-06-05 02:34:45 -07:00
)
func ReadAdjustment(r io.Reader) Adjustment {
2014-06-05 11:04:56 -07:00
switch t := ReadByte(r); t {
case ADJ_TYPE_BOND:
return &Bond{
Fee: ReadUInt64(r),
UnbondTo: ReadAccountId(r),
Amount: ReadUInt64(r),
Signature: ReadSignature(r),
}
case ADJ_TYPE_UNBOND:
return &Unbond{
Fee: ReadUInt64(r),
Amount: ReadUInt64(r),
Signature: ReadSignature(r),
}
case ADJ_TYPE_TIMEOUT:
return &Timeout{
Account: ReadAccountId(r),
Penalty: ReadUInt64(r),
}
case ADJ_TYPE_DUPEOUT:
return &Dupeout{
VoteA: ReadVote(r),
VoteB: ReadVote(r),
}
default:
2014-06-18 20:48:07 -07:00
Panicf("Unknown Adjustment type %x", t)
2014-06-05 11:04:56 -07:00
return nil
}
2014-06-05 02:34:45 -07:00
}
/* Bond < Adjustment */
type Bond struct {
Fee UInt64
UnbondTo AccountId
Amount UInt64
2014-06-05 11:04:56 -07:00
Signature
2014-06-05 02:34:45 -07:00
}
func (self *Bond) Type() Byte {
return ADJ_TYPE_BOND
}
func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
2014-06-05 11:45:18 -07:00
n, err = WriteOnto(self.Type(), w, n, err)
n, err = WriteOnto(self.Fee, w, n, err)
n, err = WriteOnto(self.UnbondTo, w, n, err)
n, err = WriteOnto(self.Amount, w, n, err)
n, err = WriteOnto(self.Signature, w, n, err)
return
2014-06-05 02:34:45 -07:00
}
/* Unbond < Adjustment */
type Unbond struct {
Fee UInt64
Amount UInt64
2014-06-05 11:04:56 -07:00
Signature
2014-06-05 02:34:45 -07:00
}
func (self *Unbond) Type() Byte {
return ADJ_TYPE_UNBOND
}
func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
2014-06-05 11:45:18 -07:00
n, err = WriteOnto(self.Type(), w, n, err)
n, err = WriteOnto(self.Fee, w, n, err)
n, err = WriteOnto(self.Amount, w, n, err)
n, err = WriteOnto(self.Signature, w, n, err)
return
2014-06-05 02:34:45 -07:00
}
/* Timeout < Adjustment */
type Timeout struct {
Account AccountId
Penalty UInt64
}
func (self *Timeout) Type() Byte {
return ADJ_TYPE_TIMEOUT
}
func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) {
2014-06-05 11:45:18 -07:00
n, err = WriteOnto(self.Type(), w, n, err)
n, err = WriteOnto(self.Account, w, n, err)
n, err = WriteOnto(self.Penalty, w, n, err)
return
2014-06-05 02:34:45 -07:00
}
/* Dupeout < Adjustment */
type Dupeout struct {
VoteA Vote
VoteB Vote
}
func (self *Dupeout) Type() Byte {
return ADJ_TYPE_DUPEOUT
}
func (self *Dupeout) WriteTo(w io.Writer) (n int64, err error) {
2014-06-05 21:20:55 -07:00
n, err = WriteOnto(self.Type(), w, n, err)
2014-06-05 11:45:18 -07:00
n, err = WriteOnto(self.VoteA, w, n, err)
n, err = WriteOnto(self.VoteB, w, n, err)
return
2014-06-05 02:34:45 -07:00
}