fix basic tests.

This commit is contained in:
Jae Kwon
2014-10-04 19:16:49 -07:00
parent 11a79f11e0
commit 1ae9ecd2a9
18 changed files with 443 additions and 270 deletions
+31 -4
View File
@@ -8,19 +8,30 @@ import (
"io"
)
const (
AccountBalanceStatusNominal = byte(0x00)
AccountBalanceStatusBonded = byte(0x01)
)
type Account struct {
Id uint64 // Numeric id of account, incrementing.
PubKey []byte
}
func ReadAccount(r io.Reader, n *int64, err *error) *Account {
return &Account{
func ReadAccount(r io.Reader, n *int64, err *error) Account {
return Account{
Id: ReadUInt64(r, n, err),
PubKey: ReadByteSlice(r, n, err),
}
}
func (account *Account) Verify(msg []byte, sig Signature) bool {
func (account Account) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt64(w, account.Id, &n, &err)
WriteByteSlice(w, account.PubKey, &n, &err)
return
}
func (account Account) Verify(msg []byte, sig Signature) bool {
if sig.SignerId != account.Id {
panic("Account.Id doesn't match sig.SignerId")
}
@@ -38,6 +49,22 @@ func (account *Account) Verify(msg []byte, sig Signature) bool {
type AccountBalance struct {
Account
Balance uint64
Status byte
}
func ReadAccountBalance(r io.Reader, n *int64, err *error) *AccountBalance {
return &AccountBalance{
Account: ReadAccount(r, n, err),
Balance: ReadUInt64(r, n, err),
Status: ReadByte(r, n, err),
}
}
func (accBal AccountBalance) WriteTo(w io.Writer) (n int64, err error) {
WriteBinary(w, accBal.Account, &n, &err)
WriteUInt64(w, accBal.Balance, &n, &err)
WriteByte(w, accBal.Status, &n, &err)
return
}
//-----------------------------------------------------------------------------
@@ -50,7 +77,7 @@ type PrivAccount struct {
// Generates a new account with private key.
// The Account.Id is empty since it isn't in the blockchain.
func GenPrivAccount() *PrivAccount {
privKey := RandBytes(32)
privKey := CRandBytes(32)
pubKey := crypto.MakePubKey(privKey)
return &PrivAccount{
Account: Account{
+1 -1
View File
@@ -10,7 +10,7 @@ func TestSignAndValidate(t *testing.T) {
privAccount := GenPrivAccount()
account := &privAccount.Account
msg := RandBytes(128)
msg := CRandBytes(128)
sig := privAccount.Sign(msg)
t.Logf("msg: %X, sig: %X", msg, sig)
+32 -8
View File
@@ -8,7 +8,7 @@ import (
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/blocks"
db_ "github.com/tendermint/tendermint/db"
. "github.com/tendermint/tendermint/db"
"github.com/tendermint/tendermint/merkle"
)
@@ -20,7 +20,7 @@ var (
type State struct {
mtx sync.Mutex
db db_.Db
db DB
height uint32 // Last known block height
blockHash []byte // Last known block hash
commitTime time.Time
@@ -28,11 +28,35 @@ type State struct {
validators *ValidatorSet
}
func GenesisState(commitTime time.Time, accounts merkle.Tree, validators *ValidatorSet) *State {
func GenesisState(db DB, genesisTime time.Time, accountBalances []*AccountBalance) *State {
accounts := merkle.NewIAVLTree(db)
validators := map[uint64]*Validator{}
for _, account := range accountBalances {
// XXX make codec merkle tree.
//accounts.Set(account.Id, BinaryBytes(account))
validators[account.Id] = &Validator{
Account: account.Account,
BondHeight: 0,
VotingPower: account.Balance,
Accum: 0,
}
}
validatorSet := NewValidatorSet(validators)
return &State{
db: db,
height: 0,
blockHash: nil,
commitTime: genesisTime,
accounts: accounts,
validators: validatorSet,
}
}
func LoadState(db db_.Db) *State {
s := &State{}
func LoadState(db DB) *State {
s := &State{db: db}
buf := db.Get(stateKey)
if len(buf) == 0 {
return nil
@@ -160,7 +184,7 @@ func (s *State) Validators() *ValidatorSet {
return s.validators
}
func (s *State) Account(accountId uint64) (*Account, error) {
func (s *State) AccountBalance(accountId uint64) (*AccountBalance, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
idBytes, err := BasicCodec.Write(accountId)
@@ -172,6 +196,6 @@ func (s *State) Account(accountId uint64) (*Account, error) {
return nil, nil
}
n, err := int64(0), error(nil)
account := ReadAccount(bytes.NewBuffer(accountBytes), &n, &err)
return account, err
accountBalance := ReadAccountBalance(bytes.NewBuffer(accountBytes), &n, &err)
return accountBalance, err
}
+42
View File
@@ -0,0 +1,42 @@
package state
import (
. "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/db"
"testing"
"time"
)
func randAccountBalance(id uint64, status byte) *AccountBalance {
return &AccountBalance{
Account: Account{
Id: id,
PubKey: CRandBytes(32),
},
Balance: RandUInt64(),
Status: status,
}
}
// The first numValidators accounts are validators.
func randGenesisState(numAccounts int, numValidators int) *State {
db := NewMemDB()
accountBalances := make([]*AccountBalance, numAccounts)
for i := 0; i < numAccounts; i++ {
if i < numValidators {
accountBalances[i] = randAccountBalance(uint64(i), AccountBalanceStatusNominal)
} else {
accountBalances[i] = randAccountBalance(uint64(i), AccountBalanceStatusBonded)
}
}
s0 := GenesisState(db, time.Now(), accountBalances)
return s0
}
func TestGenesisSaveLoad(t *testing.T) {
s0 := randGenesisState(10, 5)
t.Log(s0)
}
+3 -7
View File
@@ -12,7 +12,7 @@ import (
// TODO consider moving this to another common types package.
type Validator struct {
Account
BondHeight uint32
BondHeight uint32 // TODO: is this needed?
VotingPower uint64
Accum int64
}
@@ -20,10 +20,7 @@ type Validator struct {
// Used to persist the state of ConsensusStateControl.
func ReadValidator(r io.Reader, n *int64, err *error) *Validator {
return &Validator{
Account: Account{
Id: ReadUInt64(r, n, err),
PubKey: ReadByteSlice(r, n, err),
},
Account: ReadAccount(r, n, err),
BondHeight: ReadUInt32(r, n, err),
VotingPower: ReadUInt64(r, n, err),
Accum: ReadInt64(r, n, err),
@@ -42,8 +39,7 @@ func (v *Validator) Copy() *Validator {
// Used to persist the state of ConsensusStateControl.
func (v *Validator) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt64(w, v.Id, &n, &err)
WriteByteSlice(w, v.PubKey, &n, &err)
WriteBinary(w, v.Account, &n, &err)
WriteUInt32(w, v.BondHeight, &n, &err)
WriteUInt64(w, v.VotingPower, &n, &err)
WriteInt64(w, v.Accum, &n, &err)