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 TestUnconfirmedTransactions(t *testing.T) { bc := blockchain.New(http.DefaultClient) ut := &blockchain.UnconfirmedTransactions{} if err := bc.Request(ut); err != nil { t.Fatal(err) } if len(ut.Transactions) == 0 { t.Fatal("no transactions") } count := 0 for { tx, err := ut.NextTransaction() if err == blockchain.IterDone { break } else if err != nil { t.Fatal(err) } if tx.Hash == "" { t.Fatal("no transaction hash") } count++ } t.Logf("%d unconfirmed transactions", count) }
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 TestTransactionFee(t *testing.T) { bc := blockchain.New(http.DefaultClient) b := &blockchain.Block{Index: 312373} if err := bc.Request(b); err != nil { t.Fatal(err) } feeSum := int64(0) for _, tx := range b.Transactions { feeSum = feeSum + tx.Fee() } if feeSum != b.Fee { t.Fatalf("fees do not tally feeSum (%d) vs b.Fee (%d)", feeSum, b.Fee) } }
func TestRequestLargeAddress(t *testing.T) { bc := blockchain.New(http.DefaultClient) address := &blockchain.Address{Address: largeAddress, TxSortDescending: false} if err := bc.Request(address); err != nil { t.Fatal(err) } if len(address.Transactions) != 50 { t.Fatalf("tx count not 50") } tx, err := address.NextTransaction() if err != nil { t.Fatal(err) } if tx.Hash != largeAddressTxHashOne { t.Fatalf("tx hash incorrect %s but should be %s", tx.Hash, largeAddressTxHashOne) } for i := 0; i < 49; i++ { tx, err = address.NextTransaction() if err != nil { t.Fatal(err) } } // Check the address iterator goes to the server again. tx, err = address.NextTransaction() if err != nil { t.Fatal(err) } if tx.Hash != largeAddressTxHashFiftyOne { t.Fatalf("tx hash incorrect %s but should be %s", tx.Hash, largeAddressTxHashFiftyOne) } if len(address.Transactions) != 50 { t.Fatalf("tx count not 50") } }
func TestTransactionIndex(t *testing.T) { bc := blockchain.New(http.DefaultClient) tx := &blockchain.Transaction{Index: 30187542} if err := bc.Request(tx); err != nil { t.Fatal(err) } if len(tx.Inputs) != 14 { t.Fatal("should be 14 inputs") } if len(tx.Outputs) != 1 { t.Fatal("should be 1 output") } if tx.Index != 30187542 { t.Fatalf("incorrect index for transaction (%d)", tx.Index) } }
func TestTransactionHash(t *testing.T) { bc := blockchain.New(http.DefaultClient) tx := &blockchain.Transaction{ Hash: "2355913fc1a3d71efbc228c69dc5d74340e07b9012377b4b9f6d5522116d0509"} if err := bc.Request(tx); err != nil { t.Fatal(err) } if len(tx.Inputs) != 14 { t.Fatal("should be 14 inputs") } if len(tx.Outputs) != 1 { t.Fatal("should be 1 output") } if tx.Index != 30187542 { t.Fatalf("incorrect index for transaction (%d)", tx.Index) } }
func TestRequestSmallAddress(t *testing.T) { bc := blockchain.New(http.DefaultClient) address := &blockchain.Address{Address: smallAddress} if err := bc.Request(address); err != nil { t.Fatal(err) } count := 0 for { _, err := address.NextTransaction() if err == blockchain.IterDone { break } else if err != nil { t.Fatal(err) } count++ } if count != 6 { t.Fatalf("expected 6 iterations but got %d", count) } }
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") } }
package blockchain_test import ( "net/http" "testing" "github.com/qedus/blockchain" ) var ( bc = blockchain.New(http.DefaultClient) ) func init() { bc.GUID = "[required wallet GUID]" bc.Password = "******" bc.SecondPassword = "******" bc.APICode = "[optional API code]" } func TestNewAddress(t *testing.T) { na := &blockchain.NewAddress{Label: "testlabel"} if err := bc.Request(na); err != nil { t.Fatal(err) } if na.Label != "testlabel" { t.Fatalf("not correct label in new address [%s]", na.Label) } if na.Address == "" {