diff --git a/core/tx.go b/core/tx.go index 4ad4c0d..e329b23 100644 --- a/core/tx.go +++ b/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, diff --git a/core/tx_test.go b/core/tx_test.go index 5f250f8..e172b09 100644 --- a/core/tx_test.go +++ b/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)) } diff --git a/node/node.go b/node/node.go index a960c9f..433fb31 100644 --- a/node/node.go +++ b/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 } diff --git a/node/node_test.go b/node/node_test.go index 0f0395f..431a9bb 100644 --- a/node/node_test.go +++ b/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) +}