-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
52 lines (44 loc) · 1.15 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package test_util
import (
"crypto/ecdsa"
"testing"
"github.com/eliothedeman/heath/block"
"github.com/eliothedeman/heath/util"
"github.com/eliothedeman/randutil"
)
func GenKeys(t *testing.T, n int) ([]*ecdsa.PrivateKey, []ecdsa.PublicKey) {
var priv []*ecdsa.PrivateKey
var pub []ecdsa.PublicKey
for i := 0; i < n; i++ {
k, err := util.GenerateKey()
if err != nil {
t.Fatal(err)
}
priv = append(priv, k)
pub = append(pub, k.PublicKey)
}
return priv, pub
}
func GenTestBlock(t *testing.T, keys, transactions int, parent *block.Block) *block.Block {
a, _ := GenKeys(t, keys)
x := GenTestTransactions(t, a, transactions)
var hash []byte
if parent != nil {
hash = parent.Hash
}
return block.NewBlock(hash, x)
}
func GenTestTransaction(t *testing.T, k *ecdsa.PrivateKey) *block.Transaction {
tx, err := block.NewTransaction(k, randutil.Bytes(100), block.Transaction_Raw)
if err != nil {
t.Fatal(err)
}
return tx
}
func GenTestTransactions(t *testing.T, keys []*ecdsa.PrivateKey, n int) []*block.Transaction {
var tx []*block.Transaction
for i := 0; i < n; i++ {
tx = append(tx, GenTestTransaction(t, keys[i%len(keys)]))
}
return tx
}