func TestBalanceAdd(t *testing.T) { b := visor.NewBalance(uint64(10), uint64(20)) c := visor.NewBalance(uint64(15), uint64(25)) d := b.Add(c) assert.Equal(t, d.Coins, uint64(25)) assert.Equal(t, d.Hours, uint64(45)) e := c.Add(b) assert.Equal(t, d, e) }
func TestBalanceEquals(t *testing.T) { b := visor.NewBalance(10, 10) assert.True(t, b.Equals(b)) c := visor.NewBalance(10, 20) assert.False(t, b.Equals(c)) assert.False(t, c.Equals(b)) c = visor.NewBalance(20, 10) assert.False(t, b.Equals(c)) assert.False(t, c.Equals(b)) c = visor.NewBalance(20, 20) assert.False(t, b.Equals(c)) assert.False(t, c.Equals(b)) }
// Transfers all the coins and hours in genesis block to an address func transferAllToAddress(v *visor.Visor, gb visor.SignedBlock, dest coin.Address) (visor.SignedBlock, error) { sb := visor.SignedBlock{} if gb.Block.Head.BkSeq != uint64(0) { return sb, errors.New("Must be genesis block") } // Send the entire genesis block to dest if len(gb.Block.Body.Transactions) != 1 { log.Panic("Genesis block has only 1 txn") } tx := gb.Block.Body.Transactions[0] if len(tx.Out) != 1 { log.Panic("Genesis block has only 1 output") } amt := visor.NewBalance(tx.Out[0].Coins, tx.Out[0].Hours) tx, err := v.Spend(amt, 0, dest) if err != nil { return sb, err } // Add the tx to the unconfirmed pool so it can get picked up err, _ = v.RecordTxn(tx) if err != nil { return sb, err } // Put the tx in a block and commit sb, err = v.CreateAndExecuteBlock() if err != nil { return sb, err } return sb, nil }
func walletSpendHandler(gateway *daemon.Gateway) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { sdst := r.FormValue("dst") if sdst == "" { Error400(w, "Missing destination address \"dst\"") return } dst, err := coin.DecodeBase58Address(sdst) if err != nil { Error400(w, "Invalid destination address") return } sfee := r.FormValue("fee") fee, err := strconv.ParseUint(sfee, 10, 64) if err != nil { Error400(w, "Invalid \"fee\" value") return } scoins := r.FormValue("coins") shours := r.FormValue("hours") coins, err := strconv.ParseUint(scoins, 10, 64) if err != nil { Error400(w, "Invalid \"coins\" value") return } hours, err := strconv.ParseUint(shours, 10, 64) if err != nil { Error400(w, "Invalid \"hours\" value") return } SendOr404(w, gateway.Spend(visor.NewBalance(coins, hours), fee, dst)) } }
func TestBalanceIsZero(t *testing.T) { b := visor.NewBalance(uint64(0), uint64(0)) assert.True(t, b.IsZero()) b.Coins = uint64(1) assert.False(t, b.IsZero()) b.Hours = uint64(1) assert.False(t, b.IsZero()) b.Coins = uint64(0) assert.False(t, b.IsZero()) }
func TestBalanceSub(t *testing.T) { b := visor.NewBalance(uint64(10), uint64(20)) c := visor.NewBalance(uint64(15), uint64(25)) d := c.Sub(b) assert.Equal(t, d.Coins, uint64(5)) assert.Equal(t, d.Hours, uint64(5)) assert.Panics(t, func() { b.Sub(c) }) // Sub with bad coins b = visor.NewBalance(10, 20) c = visor.NewBalance(20, 10) assert.Panics(t, func() { b.Sub(c) }) // Sub with bad hours b = visor.NewBalance(20, 10) c = visor.NewBalance(10, 20) assert.Panics(t, func() { b.Sub(c) }) // Sub equal b = visor.NewBalance(20, 20) c = visor.NewBalance(20, 20) assert.Equal(t, visor.NewBalance(0, 0), b.Sub(c)) assert.Equal(t, visor.NewBalance(0, 0), c.Sub(b)) }
func TestNewBalance(t *testing.T) { b := visor.NewBalance(uint64(10), uint64(20)) assert.Equal(t, b.Coins, uint64(10)) assert.Equal(t, b.Hours, uint64(20)) }