mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-08 23:26:42 +00:00

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
141 lines
4.5 KiB
Go
141 lines
4.5 KiB
Go
// Copyright (c) 2015-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package txscript
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"testing"
|
|
|
|
"github.com/kaspanet/kaspad/ecc"
|
|
"github.com/kaspanet/kaspad/util/daghash"
|
|
)
|
|
|
|
// genRandomSig returns a random message, a signature of the message under the
|
|
// public key and the public key. This function is used to generate randomized
|
|
// test data.
|
|
func genRandomSig() (*daghash.Hash, *ecc.Signature, *ecc.PublicKey, error) {
|
|
privKey, err := ecc.NewPrivateKey(ecc.S256())
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
|
|
var msgHash daghash.Hash
|
|
if _, err := rand.Read(msgHash[:]); err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
|
|
sig, err := privKey.Sign(msgHash[:])
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
|
|
return &msgHash, sig, privKey.PubKey(), nil
|
|
}
|
|
|
|
// TestSigCacheAddExists tests the ability to add, and later check the
|
|
// existence of a signature triplet in the signature cache.
|
|
func TestSigCacheAddExists(t *testing.T) {
|
|
sigCache := NewSigCache(200)
|
|
|
|
// Generate a random sigCache entry triplet.
|
|
msg1, sig1, key1, err := genRandomSig()
|
|
if err != nil {
|
|
t.Errorf("unable to generate random signature test data")
|
|
}
|
|
|
|
// Add the triplet to the signature cache.
|
|
sigCache.Add(*msg1, sig1, key1)
|
|
|
|
// The previously added triplet should now be found within the sigcache.
|
|
sig1Copy, _ := ecc.ParseSignature(sig1.Serialize())
|
|
key1Copy, _ := ecc.ParsePubKey(key1.SerializeCompressed(), ecc.S256())
|
|
if !sigCache.Exists(*msg1, sig1Copy, key1Copy) {
|
|
t.Errorf("previously added item not found in signature cache")
|
|
}
|
|
}
|
|
|
|
// TestSigCacheAddEvictEntry tests the eviction case where a new signature
|
|
// triplet is added to a full signature cache which should trigger randomized
|
|
// eviction, followed by adding the new element to the cache.
|
|
func TestSigCacheAddEvictEntry(t *testing.T) {
|
|
// Create a sigcache that can hold up to 100 entries.
|
|
sigCacheSize := uint(100)
|
|
sigCache := NewSigCache(sigCacheSize)
|
|
|
|
// Fill the sigcache up with some random sig triplets.
|
|
for i := uint(0); i < sigCacheSize; i++ {
|
|
msg, sig, key, err := genRandomSig()
|
|
if err != nil {
|
|
t.Fatalf("unable to generate random signature test data")
|
|
}
|
|
|
|
sigCache.Add(*msg, sig, key)
|
|
|
|
sigCopy, _ := ecc.ParseSignature(sig.Serialize())
|
|
keyCopy, _ := ecc.ParsePubKey(key.SerializeCompressed(), ecc.S256())
|
|
if !sigCache.Exists(*msg, sigCopy, keyCopy) {
|
|
t.Errorf("previously added item not found in signature" +
|
|
"cache")
|
|
}
|
|
}
|
|
|
|
// The sigcache should now have sigCacheSize entries within it.
|
|
if uint(len(sigCache.validSigs)) != sigCacheSize {
|
|
t.Fatalf("sigcache should now have %v entries, instead it has %v",
|
|
sigCacheSize, len(sigCache.validSigs))
|
|
}
|
|
|
|
// Add a new entry, this should cause eviction of a randomly chosen
|
|
// previous entry.
|
|
msgNew, sigNew, keyNew, err := genRandomSig()
|
|
if err != nil {
|
|
t.Fatalf("unable to generate random signature test data")
|
|
}
|
|
sigCache.Add(*msgNew, sigNew, keyNew)
|
|
|
|
// The sigcache should still have sigCache entries.
|
|
if uint(len(sigCache.validSigs)) != sigCacheSize {
|
|
t.Fatalf("sigcache should now have %v entries, instead it has %v",
|
|
sigCacheSize, len(sigCache.validSigs))
|
|
}
|
|
|
|
// The entry added above should be found within the sigcache.
|
|
sigNewCopy, _ := ecc.ParseSignature(sigNew.Serialize())
|
|
keyNewCopy, _ := ecc.ParsePubKey(keyNew.SerializeCompressed(), ecc.S256())
|
|
if !sigCache.Exists(*msgNew, sigNewCopy, keyNewCopy) {
|
|
t.Fatalf("previously added item not found in signature cache")
|
|
}
|
|
}
|
|
|
|
// TestSigCacheAddMaxEntriesZeroOrNegative tests that if a sigCache is created
|
|
// with a max size <= 0, then no entries are added to the sigcache at all.
|
|
func TestSigCacheAddMaxEntriesZeroOrNegative(t *testing.T) {
|
|
// Create a sigcache that can hold up to 0 entries.
|
|
sigCache := NewSigCache(0)
|
|
|
|
// Generate a random sigCache entry triplet.
|
|
msg1, sig1, key1, err := genRandomSig()
|
|
if err != nil {
|
|
t.Errorf("unable to generate random signature test data")
|
|
}
|
|
|
|
// Add the triplet to the signature cache.
|
|
sigCache.Add(*msg1, sig1, key1)
|
|
|
|
// The generated triplet should not be found.
|
|
sig1Copy, _ := ecc.ParseSignature(sig1.Serialize())
|
|
key1Copy, _ := ecc.ParsePubKey(key1.SerializeCompressed(), ecc.S256())
|
|
if sigCache.Exists(*msg1, sig1Copy, key1Copy) {
|
|
t.Errorf("previously added signature found in sigcache, but" +
|
|
"shouldn't have been")
|
|
}
|
|
|
|
// There shouldn't be any entries in the sigCache.
|
|
if len(sigCache.validSigs) != 0 {
|
|
t.Errorf("%v items found in sigcache, no items should have"+
|
|
"been added", len(sigCache.validSigs))
|
|
}
|
|
}
|