|
@ -3,19 +3,27 @@ package core |
|
|
import ( |
|
|
import ( |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"testing" |
|
|
"testing" |
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
"github.com/arnaucube/slowlorisdb/db" |
|
|
"github.com/arnaucube/slowlorisdb/db" |
|
|
"github.com/stretchr/testify/assert" |
|
|
"github.com/stretchr/testify/assert" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func TestBlockchainDataStructure(t *testing.T) { |
|
|
func TestBlockchainDataStructure(t *testing.T) { |
|
|
dir, err := ioutil.TempDir("", "db") |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
db, err := db.New(dir) |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
|
|
|
|
bc := NewBlockchain(db, uint64(1)) |
|
|
|
|
|
block := bc.NewBlock([]Tx{}) |
|
|
|
|
|
|
|
|
// dir, err := ioutil.TempDir("", "db")
|
|
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
|
|
// db, err := db.New(dir)
|
|
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
|
|
//
|
|
|
|
|
|
// bc := NewBlockchain(db, uint64(1))
|
|
|
|
|
|
block := &Block{ |
|
|
|
|
|
PrevHash: Hash{}, |
|
|
|
|
|
Txs: []Tx{}, |
|
|
|
|
|
Miner: Address{}, |
|
|
|
|
|
Timestamp: time.Now(), |
|
|
|
|
|
Nonce: 0, |
|
|
|
|
|
Hash: HashBytes([]byte("blockhash")), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
block2, err := BlockFromBytes(block.Bytes()) |
|
|
block2, err := BlockFromBytes(block.Bytes()) |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
@ -30,7 +38,15 @@ func TestGetBlock(t *testing.T) { |
|
|
|
|
|
|
|
|
bc := NewBlockchain(db, uint64(1)) |
|
|
bc := NewBlockchain(db, uint64(1)) |
|
|
|
|
|
|
|
|
block := bc.NewBlock([]Tx{}) |
|
|
|
|
|
|
|
|
block := &Block{ |
|
|
|
|
|
Height: uint64(1), |
|
|
|
|
|
PrevHash: Hash{}, |
|
|
|
|
|
Txs: []Tx{}, |
|
|
|
|
|
Miner: Address{}, |
|
|
|
|
|
Timestamp: time.Now(), |
|
|
|
|
|
Nonce: 0, |
|
|
|
|
|
Hash: HashBytes([]byte("blockhash")), |
|
|
|
|
|
} |
|
|
assert.Equal(t, block.Height, uint64(1)) |
|
|
assert.Equal(t, block.Height, uint64(1)) |
|
|
|
|
|
|
|
|
err = bc.AddBlock(block) |
|
|
err = bc.AddBlock(block) |
|
@ -49,10 +65,20 @@ func TestGetPrevBlock(t *testing.T) { |
|
|
|
|
|
|
|
|
bc := NewBlockchain(db, uint64(1)) |
|
|
bc := NewBlockchain(db, uint64(1)) |
|
|
|
|
|
|
|
|
|
|
|
var prevHash Hash |
|
|
for i := 0; i < 10; i++ { |
|
|
for i := 0; i < 10; i++ { |
|
|
block := bc.NewBlock([]Tx{}) |
|
|
|
|
|
|
|
|
block := &Block{ |
|
|
|
|
|
Height: uint64(i + 1), |
|
|
|
|
|
PrevHash: prevHash, |
|
|
|
|
|
Txs: []Tx{}, |
|
|
|
|
|
Miner: Address{}, |
|
|
|
|
|
Timestamp: time.Now(), |
|
|
|
|
|
Nonce: 0, |
|
|
|
|
|
Hash: Hash{}, |
|
|
|
|
|
} |
|
|
block.CalculatePoW(bc.Difficulty) |
|
|
block.CalculatePoW(bc.Difficulty) |
|
|
assert.Equal(t, block.Height, uint64(i+1)) |
|
|
assert.Equal(t, block.Height, uint64(i+1)) |
|
|
|
|
|
prevHash = block.Hash |
|
|
|
|
|
|
|
|
err = bc.AddBlock(block) |
|
|
err = bc.AddBlock(block) |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
@ -73,6 +99,7 @@ func TestGetPrevBlock(t *testing.T) { |
|
|
assert.Equal(t, err.Error(), "This was the oldest block") |
|
|
assert.Equal(t, err.Error(), "This was the oldest block") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
func TestAddBlockWithTx(t *testing.T) { |
|
|
func TestAddBlockWithTx(t *testing.T) { |
|
|
addr0 := Address(HashBytes([]byte("addr0"))) |
|
|
addr0 := Address(HashBytes([]byte("addr0"))) |
|
|
addr1 := Address(HashBytes([]byte("addr1"))) |
|
|
addr1 := Address(HashBytes([]byte("addr1"))) |
|
@ -87,9 +114,47 @@ func TestAddBlockWithTx(t *testing.T) { |
|
|
var txs []Tx |
|
|
var txs []Tx |
|
|
tx := NewTx(addr0, addr1, []Input{}, []Output{}) |
|
|
tx := NewTx(addr0, addr1, []Input{}, []Output{}) |
|
|
txs = append(txs, *tx) |
|
|
txs = append(txs, *tx) |
|
|
block := bc.NewBlock(txs) |
|
|
|
|
|
|
|
|
block := &Block{ |
|
|
|
|
|
PrevHash: Hash{}, |
|
|
|
|
|
NextHash: Hash{}, |
|
|
|
|
|
Txs: txs, |
|
|
|
|
|
Miner: Address{}, |
|
|
|
|
|
Timestamp: time.Now(), |
|
|
|
|
|
Nonce: 0, |
|
|
|
|
|
Hash: HashBytes([]byte("blockhash")), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
block2, err := BlockFromBytes(block.Bytes()) |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
assert.Equal(t, block2.Bytes(), block.Bytes()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestPoABlockchainDataStructure(t *testing.T) { |
|
|
|
|
|
dir, err := ioutil.TempDir("", "db") |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
db, err := db.New(dir) |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
|
|
|
|
var authNodesPubK []*ecdsa.PublicKey |
|
|
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
|
|
privK, err := NewKey() |
|
|
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
authNodesPubK = append(authNodesPubK, &privK.PublicKey) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bc := NewPoABlockchain(db, authNodesPubK) |
|
|
|
|
|
block := &Block{ |
|
|
|
|
|
PrevHash: Hash{}, |
|
|
|
|
|
NextHash: Hash{}, |
|
|
|
|
|
Txs: []Tx{}, |
|
|
|
|
|
Miner: Address{}, |
|
|
|
|
|
Timestamp: time.Now(), |
|
|
|
|
|
Nonce: 0, |
|
|
|
|
|
Hash: HashBytes([]byte("blockhash")), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
block2, err := BlockFromBytes(block.Bytes()) |
|
|
block2, err := BlockFromBytes(block.Bytes()) |
|
|
assert.Nil(t, err) |
|
|
assert.Nil(t, err) |
|
|
assert.Equal(t, block2.Bytes(), block.Bytes()) |
|
|
assert.Equal(t, block2.Bytes(), block.Bytes()) |
|
|
} |
|
|
} |
|
|
|
|
|
*/ |