-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock_test.go
82 lines (65 loc) · 1.82 KB
/
block_test.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package blockchain_test
import (
"net/http"
"testing"
"time"
"github.com/qedus/blockchain"
)
const (
blockHash = "0000000000000bae09a7a393a8acded75aa67e46cb81f7acaa5ad94f9eacd103"
)
func TestRequestBlock(t *testing.T) {
bc := blockchain.New(http.DefaultClient)
block := &blockchain.Block{Hash: blockHash}
if err := bc.Request(block); err != nil {
t.Fatal(err)
}
if block.Fee != 200000 {
t.Fatal("fee not 200000")
}
if block.TransactionCount != 22 {
t.Fatal("transaction count not 22")
}
if len(block.Transactions) != 22 {
t.Fatal("transactions length not 22")
}
if block.Transactions[0].Hash != "5b09bbb8d3cb2f8d4edbcf30664419fb7c9deaeeb1f62cb432e7741c80dbe5ba" {
t.Fatal("first transaction hash incorrect")
}
if !block.Transactions[0].IsCoinbase() {
t.Fatal("not coinbase transaction")
}
}
func TestRequestLatestBlock(t *testing.T) {
bc := blockchain.New(http.DefaultClient)
block := &blockchain.LatestBlock{}
if err := bc.Request(block); err != nil {
t.Fatal(err)
}
if time.Unix(block.Time, 0).Before(time.Now().Add(-30 * time.Minute)) {
t.Fatalf("latest block too old at %s minutes and now is %s",
time.Unix(block.Time, 0), time.Now())
}
if len(block.TransactionIndexes) < 1 {
t.Fatal("no transactions in latest block")
}
}
func TestRequestBlockHeight(t *testing.T) {
bc := blockchain.New(http.DefaultClient)
bh := &blockchain.BlockHeight{Height: 285180}
if err := bc.Request(bh); err != nil {
t.Fatal(err)
}
/* It appears that Blockchain.info has fogotten which blocks were previously
orphan blocks. Therefore this test no longer works.
if len(bh.Blocks) != 2 {
t.Fatal("should be two blocks")
}
if bh.Blocks[1].MainChain {
t.Fatal("this block should not on the main chain")
}
*/
if !bh.Blocks[0].MainChain {
t.Fatal("this block should be on the main chain")
}
}