func TestPaymentValueServedToOcID(t *testing.T) { defer os.RemoveAll(testutil.InitDir(t)) id := msg.OcID("id-123") otherID := msg.OcID("id-other") _, err := Put(&Record{ Role: SERVER, ID: id, Status: SUCCESS_PAID, PaymentValue: &msg.PaymentValue{Amount: 1000, Currency: msg.BTC}, }) if err != nil { t.Fatal(err) } _, err = Put(&Record{ Role: SERVER, ID: id, Status: SUCCESS_UNPAID, PaymentValue: &msg.PaymentValue{Amount: 2000, Currency: msg.BTC}, }) if err != nil { t.Fatal(err) } _, err = Put(&Record{ Role: SERVER, ID: otherID, Status: SUCCESS_PAID, PaymentValue: &msg.PaymentValue{Amount: 3000, Currency: msg.BTC}, }) if err != nil { t.Fatal(err) } _, err = Put(&Record{ Role: SERVER, ID: id, Status: PENDING, PaymentValue: &msg.PaymentValue{Amount: 4000, Currency: msg.BTC}, }) if err != nil { t.Fatal(err) } _, err = Put(&Record{ Role: SERVER, ID: id, Status: FAILURE, PaymentValue: &msg.PaymentValue{Amount: 5000, Currency: msg.BTC}, }) if err != nil { t.Fatal(err) } pv, err := PaymentValueServedToOcID(id) if err != nil { t.Fatal(err) } if 3000 != pv.Amount { t.Fatalf("%v != %v", 3000, pv.Amount) } }
func TestAmountPaid(t *testing.T) { defer os.RemoveAll(testutil.InitDir(t)) btcConf, err := util.LoadBitcoindConf("") if err != nil { t.Fatal(err) } peer := Peer{ID: msg.OcID("123id")} otherPeer := Peer{ID: msg.OcID("456otherid")} addr, err := peer.PaymentAddr(1, btcConf) if err != nil { t.Fatal(err) } otherAddr, err := otherPeer.PaymentAddr(1, btcConf) if err != nil { t.Fatal(err) } fmt.Printf("addr %v other addr %v\n", addr, otherAddr) // Send some BTC to ourselves. amt := int64(1e6) cmd, err := btcjson.NewSendToAddressCmd("", addr, amt) if err != nil { log.Fatal(err) } sendBtcResp, err := util.SendBtcRpc(cmd, btcConf) _, ok := sendBtcResp.Result.(string) if !ok { log.Fatal(sendBtcResp) } // Send some BTC to another address. cmd, err = btcjson.NewSendToAddressCmd("", otherAddr, 1e6) if err != nil { log.Fatal(err) } sendBtcResp, err = util.SendBtcRpc(cmd, btcConf) _, ok = sendBtcResp.Result.(string) if !ok { log.Fatal(sendBtcResp) } // Verify balance pv, err := peer.AmountPaid(0, btcConf) if err != nil { log.Fatal(err) } if pv.Amount != amt { t.Fatalf("%v != %v", paid, amt) } }
func Reduce(sel *Record, reduceFn func(rec *Record)) error { db, err := openOrCreate() if err != nil { return err } defer db.Close() query := selectLikeRecord(sel) rows, err := db.Query(query) if err != nil { return fmt.Errorf("error while querying %v: %v", query, err.Error()) } for rows.Next() { var role, service, method, ocID, status, pvType, pvCurr, perfHex []byte var pvAmt int64 var timestamp int err := rows.Scan( &role, &service, &method, ×tamp, &ocID, &status, &pvType, &pvAmt, &pvCurr, &perfHex) var rec Record if len(role) != 0 { rec.Role = Role(role) } if len(service) != 0 { rec.Service = string(service) } if len(method) != 0 { rec.Method = string(method) } rec.Timestamp = timestamp if len(ocID) != 0 { rec.ID = msg.OcID(ocID) } if len(status) != 0 { rec.Status = Status(status) } if len(pvType) != 0 { rec.PaymentType = msg.PaymentType(pvType) } if len(pvCurr) != 0 { pv := msg.PaymentValue{ Amount: pvAmt, Currency: msg.Currency(pvCurr), } rec.PaymentValue = &pv } if len(perfHex) != 0 { panic("TODO: implement perf decoding") } if err != nil { return fmt.Errorf("error scanning with %v: %v", query, err.Error()) } reduceFn(&rec) } return nil }
func ocIDForCoin(coin string) (*msg.OcID, error) { fmt.Printf("get oc ID for coin: %v\n", coin) d := util.GetOrCreateDB(peerDBPath()) v, _ := d.Read(coin) if v == nil || len(v) == 0 { return nil, nil } id := msg.OcID(v) return &id, nil }
func (ss *StoreService) PeriodicWake() { now := time.Now().Unix() if ss.lastWake == 0 { ss.lastWake = now } period := int64(10) if now-ss.lastWake < period { return } ss.lastWake = now d := util.GetOrCreateDB(containersDB()) keys := d.Keys() for { key := <-keys if len(key) == 0 { break } bytesUsed := 0 id := msg.OcID(key) container := NewContainerFromDisk(id) seenBlocks := make(map[string]bool) for _, blobID := range container.BlobIDs { // TODO(ortutay): don't read blocks from disk just to find sizes blob, err := NewBlobFromDisk(blobID) if err != nil { continue } for _, block := range blob.Blocks { if _, ok := seenBlocks[block.ID.String()]; ok { continue } seenBlocks[block.ID.String()] = true bytesUsed += len(block.Data) } } costPv := costForBytesSeconds(bytesUsed, int(period)) fmt.Printf("bytes %v used by %v..., cost += %f %v\n", bytesUsed, id.String()[:8], util.S2B(costPv.Amount), costPv.Currency) rec := rep.Record{ Role: rep.SERVER, Service: SERVICE_NAME, Method: PUT_METHOD, Timestamp: int(now), ID: id, Status: rep.SUCCESS_UNPAID, PaymentValue: costPv, Perf: nil, } rep.Put(&rec) } }
func TestSuccessRate(t *testing.T) { defer os.RemoveAll(testutil.InitDir(t)) id := msg.OcID("id-123") otherID := msg.OcID("id-other") _, err := Put(&Record{Role: SERVER, ID: id, Status: SUCCESS_PAID}) if err != nil { t.Fatal(err) } _, err = Put(&Record{Role: SERVER, ID: id, Status: PENDING}) if err != nil { t.Fatal(err) } _, err = Put(&Record{Role: SERVER, ID: id, Status: FAILURE}) if err != nil { t.Fatal(err) } _, err = Put(&Record{Role: SERVER, ID: otherID, Status: FAILURE}) if err != nil { t.Fatal(err) } rate, err := SuccessRate(&Record{Role: SERVER, ID: id}) if err != nil { t.Fatal(err) } if .5 != rate { t.Fatal("Expected %v, got %v", .5, rate) } rate, err = SuccessRate(&Record{Role: CLIENT, ID: id}) if err != nil { t.Fatal(err) } if -1 != rate { t.Fatal("Expected %v, got %v", -1, rate) } }
func TestGetAndSet(t *testing.T) { defer os.RemoveAll(testutil.InitDir(t)) id := msg.OcID("1abc") err := setOcIDForCoin("1abc", &id) if err != nil { t.Fatalf("err: %v", err.Error()) } id2, err := ocIDForCoin("1abc") if err != nil { t.Fatalf("err: %v", err) } fmt.Printf("id2: %v\n", id2) if id != *id2 { t.Fatalf("%v %v", id, id2) } }
func TestGetAddr(t *testing.T) { defer os.RemoveAll(testutil.InitDir(t)) btcConf, err := util.LoadBitcoindConf("") if err != nil { t.Errorf("%v", err) } p := Peer{ID: msg.OcID("123id")} addr1, err := p.PaymentAddr(1, btcConf) if err != nil { log.Fatal(err) } addr2, err := p.PaymentAddr(1, btcConf) if err != nil { log.Fatal(err) } if addr1 != addr2 { t.Fatalf("%v != %v\n", addr1, addr2) } }
func TestRepPut(t *testing.T) { defer os.RemoveAll(testutil.InitDir(t)) rec := Record{ Role: SERVER, Service: "store", Method: "put", Timestamp: 1234, ID: msg.OcID("id-123"), Status: SUCCESS_PAID, PaymentType: msg.TXID, PaymentValue: &msg.PaymentValue{Amount: 1000, Currency: msg.BTC}, Perf: nil, } _, err := Put(&rec) if err != nil { t.Fatal(err) } }
func (o *OcCred) ID() msg.OcID { // TODO(ortutay): compress pub key return msg.OcID(fmt.Sprintf("%c%x,%x", OC_ID_PREFIX, o.Priv.PublicKey.X, o.Priv.PublicKey.Y)) }