示例#1
0
func TestBlockDiff(t *testing.T) {

	tr1 := Transaction{Signature: []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024)))}
	tr2 := Transaction{Signature: []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024)))}
	tr3 := Transaction{Signature: []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024)))}

	diff := DiffTransactionSlices(TransactionSlice{tr1, tr2, tr3}, TransactionSlice{tr1, tr3})

	if len(diff) != 1 && !reflect.DeepEqual(diff[0].Signature, tr2.Signature) {

		t.Error("Diffing algorithm fails")
	}
}
示例#2
0
func TestTransactionMarshalling(t *testing.T) {

	kp := GenerateNewKeypair()
	tr := NewTransaction(kp.Public, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))

	tr.Header.Nonce = tr.GenerateNonce(helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, TEST_POW_PREFIX))
	tr.Signature = tr.Sign(kp)

	data, err := tr.MarshalBinary()

	if err != nil {
		t.Error(err)
	}

	newT := &Transaction{}
	rem, err := newT.UnmarshalBinary(data)

	if err != nil {
		t.Error(err)
	}

	if !reflect.DeepEqual(*newT, *tr) || len(rem) < 0 {
		t.Error("Marshall, unmarshall failed")
	}
}
示例#3
0
func TestMerkellHash(t *testing.T) {

	tr1 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))
	tr2 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))
	tr3 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))
	tr4 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))

	b := new(Block)
	b.TransactionSlice = &TransactionSlice{*tr1, *tr2, *tr3, *tr4}

	mt := b.GenerateMerkelRoot()
	manual := helpers.SHA256(append(helpers.SHA256(append(tr1.Hash(), tr2.Hash()...)), helpers.SHA256(append(tr3.Hash(), tr4.Hash()...))...))

	if !reflect.DeepEqual(mt, manual) {
		t.Error("Merkel tree generation fails")
	}
}
示例#4
0
func TestIncorrectTransactionSignatureVerification(t *testing.T) {

	pow := helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, TEST_POW_PREFIX)
	kp1, kp2 := GenerateNewKeypair(), GenerateNewKeypair()
	tr := NewTransaction(kp2.Public, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024))))
	tr.Header.Nonce = tr.GenerateNonce(pow)
	tr.Signature = tr.Sign(kp1)

	if tr.VerifyTransaction(pow) {

		t.Error("Passed validation with incorrect key")
	}
}
示例#5
0
func TestTransactionVerification(t *testing.T) {

	pow := helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, TEST_POW_PREFIX)

	kp := GenerateNewKeypair()
	tr := NewTransaction(kp.Public, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024))))

	tr.Header.Nonce = tr.GenerateNonce(pow)
	tr.Signature = tr.Sign(kp)

	if !tr.VerifyTransaction(pow) {

		t.Error("Validation failing")
	}
}
示例#6
0
func TestMessageMarshalling(t *testing.T) {

	mes := &Message{Identifier: MESSAGE_GET_NODES, Options: []byte{1, 2, 3, 4}, Data: []byte(helpers.RandomString(helpers.RandomInt(1024, 1024*4)))}
	bs, err := mes.MarshalBinary()

	if err != nil {
		t.Error(err)
	}

	newMes := new(Message)
	err = newMes.UnmarshalBinary(bs)

	if err != nil {
		t.Error(err)
	}

	if !reflect.DeepEqual(newMes, mes) {

		t.Error("Marshall unmarshall message error")
	}
}