This commit is contained in:
Jae Kwon 2014-10-03 17:59:54 -07:00
parent 8e452aa0d2
commit 11a79f11e0
3 changed files with 72 additions and 13 deletions

View File

@ -3,10 +3,11 @@ package state
import ( import (
. "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/blocks" . "github.com/tendermint/tendermint/blocks"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/crypto"
"io" "io"
) )
// NOTE: consensus/Validator embeds this, so..
type Account struct { type Account struct {
Id uint64 // Numeric id of account, incrementing. Id uint64 // Numeric id of account, incrementing.
PubKey []byte PubKey []byte
@ -19,11 +20,24 @@ func ReadAccount(r io.Reader, n *int64, err *error) *Account {
} }
} }
func (self *Account) Verify(msg []byte, sig Signature) bool { func (account *Account) Verify(msg []byte, sig Signature) bool {
if sig.SignerId != self.Id { if sig.SignerId != account.Id {
return false panic("Account.Id doesn't match sig.SignerId")
} }
return false v1 := &crypto.Verify{
Message: msg,
PubKey: account.PubKey,
Signature: sig.Bytes,
}
ok := crypto.VerifyBatch([]*crypto.Verify{v1})
return ok
}
//-----------------------------------------------------------------------------
type AccountBalance struct {
Account
Balance uint64
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -33,6 +47,24 @@ type PrivAccount struct {
PrivKey []byte PrivKey []byte
} }
func (self *PrivAccount) Sign(msg []byte) Signature { // Generates a new account with private key.
return Signature{} // The Account.Id is empty since it isn't in the blockchain.
func GenPrivAccount() *PrivAccount {
privKey := RandBytes(32)
pubKey := crypto.MakePubKey(privKey)
return &PrivAccount{
Account: Account{
Id: uint64(0),
PubKey: pubKey,
},
PrivKey: privKey,
}
}
func (pa *PrivAccount) Sign(msg []byte) Signature {
signature := crypto.SignMessage(msg, pa.PrivKey, pa.PubKey)
return Signature{
SignerId: pa.Id,
Bytes: signature,
}
} }

28
state/account_test.go Normal file
View File

@ -0,0 +1,28 @@
package state
import (
. "github.com/tendermint/tendermint/common"
"testing"
)
func TestSignAndValidate(t *testing.T) {
privAccount := GenPrivAccount()
account := &privAccount.Account
msg := RandBytes(128)
sig := privAccount.Sign(msg)
t.Logf("msg: %X, sig: %X", msg, sig)
// Test the signature
if !account.Verify(msg, sig) {
t.Errorf("Account message signature verification failed")
}
// Mutate the signature, just one bit.
sig.Bytes[0] ^= byte(0x01)
if account.Verify(msg, sig) {
t.Errorf("Account message signature verification should have failed but passed instead")
}
}

View File

@ -28,15 +28,14 @@ type State struct {
validators *ValidatorSet validators *ValidatorSet
} }
func GenesisState(commitTime time.Time, accounts merkle.Tree, validators *ValidatorSet) *State {
}
func LoadState(db db_.Db) *State { func LoadState(db db_.Db) *State {
s := &State{} s := &State{}
buf := db.Get(stateKey) buf := db.Get(stateKey)
if len(buf) == 0 { if len(buf) == 0 {
s.height = uint32(0) return nil
s.commitTime = time.Unix(0, 0) // XXX BOOTSTRAP
s.blockHash = nil // XXX BOOTSTRAP
s.accounts = merkle.NewIAVLTree(db) // XXX BOOTSTRAP
s.validators = NewValidatorSet(nil) // XXX BOOTSTRAP
} else { } else {
reader := bytes.NewReader(buf) reader := bytes.NewReader(buf)
var n int64 var n int64
@ -112,7 +111,7 @@ func (s *State) commitTx(tx Tx) error {
return ErrStateInvalidSequenceNumber return ErrStateInvalidSequenceNumber
} }
*/ */
// TODO commit the tx // XXX commit the tx
panic("Implement CommitTx()") panic("Implement CommitTx()")
return nil return nil
} }