Ejemplo n.º 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")
	}
}
Ejemplo n.º 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")
	}
}
Ejemplo n.º 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")
	}
}
Ejemplo n.º 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")
	}
}
Ejemplo n.º 5
0
func carState(cars []Car) []Car {

	return f.Map(func(c Car) Car {

		if helpers.RandomInt(0, 3) == 0 {

			return Car{Position: c.Position + 1}
		}

		return Car{Position: c.Position}

	}, cars).([]Car)
}
Ejemplo n.º 6
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")
	}
}
Ejemplo n.º 7
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")
	}
}