Browse Source

tmp use PubK instead of Addr, Add test add genesis block

master
arnaucube 5 years ago
parent
commit
985f03bb14
4 changed files with 65 additions and 16 deletions
  1. +6
    -3
      core/tx.go
  2. +9
    -5
      core/tx_test.go
  3. +3
    -2
      node/node.go
  4. +47
    -6
      node/node_test.go

+ 6
- 3
core/tx.go

@ -1,25 +1,28 @@
package core
import "crypto/ecdsa"
type Input struct {
TxId Hash
Vout int // index of the output from the TxId
Value uint64
}
type Output struct {
Value uint64
}
// Tx holds the data structure of a transaction
type Tx struct {
From Address
To Address
From *ecdsa.PublicKey
To *ecdsa.PublicKey
InputCount uint64
Inputs []Input
Outputs []Output
Signature []byte
}
func NewTx(from, to Address, in []Input, out []Output) *Tx {
func NewTx(from, to *ecdsa.PublicKey, in []Input, out []Output) *Tx {
tx := &Tx{
From: from,
To: to,

+ 9
- 5
core/tx_test.go

@ -7,13 +7,17 @@ import (
)
func TestTx(t *testing.T) {
addr0 := Address(HashBytes([]byte("addr0")))
addr1 := Address(HashBytes([]byte("addr1")))
privK0, err := NewKey()
assert.Nil(t, err)
pubK0 := privK0.PublicKey
privK1, err := NewKey()
assert.Nil(t, err)
pubK1 := privK1.PublicKey
tx := NewTx(addr0, addr1, []Input{}, []Output{})
tx := NewTx(&pubK0, &pubK1, []Input{}, []Output{})
assert.Equal(t, tx.From, addr0)
assert.Equal(t, tx.To, addr1)
assert.Equal(t, tx.From, &pubK0)
assert.Equal(t, tx.To, &pubK1)
assert.True(t, CheckTx(tx))
}

+ 3
- 2
node/node.go

@ -78,8 +78,8 @@ func (node *Node) NewBlock(txs []core.Tx) *core.Block {
func (node *Node) CreateGenesis() (*core.Block, error) {
block := &core.Block{
Height: uint64(0),
PrevHash: core.Hash{},
Height: node.Bc.LastBlock.Height,
PrevHash: node.Bc.LastBlock.Hash,
Txs: []core.Tx{},
Miner: node.Addr,
MinerPubK: &node.PrivK.PublicKey,
@ -93,5 +93,6 @@ func (node *Node) CreateGenesis() (*core.Block, error) {
if err != nil {
return nil, err
}
block.CalculateHash()
return block, nil
}

+ 47
- 6
node/node_test.go

@ -62,9 +62,13 @@ func TestBlockFromPendingTxs(t *testing.T) {
node, err := NewNode(privK, bc, true)
assert.Nil(t, err)
addr0 := core.Address(core.HashBytes([]byte("addr0")))
addr1 := core.Address(core.HashBytes([]byte("addr1")))
tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
privK0, err := core.NewKey()
assert.Nil(t, err)
pubK0 := privK0.PublicKey
privK1, err := core.NewKey()
assert.Nil(t, err)
pubK1 := privK1.PublicKey
tx := core.NewTx(&pubK0, &pubK1, []core.Input{}, []core.Output{})
node.AddToPendingTxs(*tx)
block, err := node.BlockFromPendingTxs()
assert.Nil(t, err)
@ -87,10 +91,15 @@ func TestBlockFromPendingTxsIteration(t *testing.T) {
node, err := NewNode(privK, bc, true)
assert.Nil(t, err)
addr0 := core.Address(core.HashBytes([]byte("addr0")))
addr1 := core.Address(core.HashBytes([]byte("addr1")))
privK0, err := core.NewKey()
assert.Nil(t, err)
pubK0 := privK0.PublicKey
privK1, err := core.NewKey()
assert.Nil(t, err)
pubK1 := privK1.PublicKey
for i := 0; i < 10; i++ {
tx := core.NewTx(addr0, addr1, []core.Input{}, []core.Output{})
tx := core.NewTx(&pubK0, &pubK1, []core.Input{}, []core.Output{})
node.AddToPendingTxs(*tx)
}
block, err := node.BlockFromPendingTxs()
@ -98,3 +107,35 @@ func TestBlockFromPendingTxsIteration(t *testing.T) {
assert.True(t, core.CheckBlockPoW(block, node.Bc.Difficulty))
assert.True(t, node.Bc.VerifyBlock(block))
}
func TestFromGenesisToTenBlocks(t *testing.T) {
dir, err := ioutil.TempDir("", "db")
assert.Nil(t, err)
db, err := db.New(dir)
assert.Nil(t, err)
privK, err := core.NewKey()
assert.Nil(t, err)
dif := uint64(1)
bc := core.NewBlockchain(db, dif)
node, err := NewNode(privK, bc, true)
assert.Nil(t, err)
// create the genesis block
genesisBlock, err := node.CreateGenesis()
assert.Nil(t, err)
assert.NotEqual(t, genesisBlock.Signature, core.Signature{})
assert.NotEqual(t, genesisBlock.Hash, core.Hash{})
// add the genesis block into the blockchain
err = node.Bc.AddBlock(genesisBlock)
assert.Nil(t, err)
assert.NotEqual(t, genesisBlock.Hash, core.Hash{})
assert.Equal(t, genesisBlock.Hash, node.Bc.LastBlock.Hash)
// TODO add another block
block := node.NewBlock([]core.Tx{})
err = node.Bc.AddBlock(block)
assert.Nil(t, err)
}

Loading…
Cancel
Save