Beispiel #1
0
// FetchAllFBlocks gets all of the factoid blocks
func (db *LevelDb) FetchAllFBlocks() (FBlocks []block.IFBlock, err error) {
	db.dbLock.Lock()
	defer db.dbLock.Unlock()

	var fromkey []byte = []byte{byte(TBL_SC)}   // Table Name (1 bytes)						// Timestamp  (8 bytes)
	var tokey []byte = []byte{byte(TBL_SC + 1)} // Table Name (1 bytes)

	FBlockSlice := make([]block.IFBlock, 0, 10)

	iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)

	for iter.Next() {
		FBlock := new(block.FBlock)
		_, err := FBlock.UnmarshalBinaryData(iter.Value())
		if err != nil {
			return nil, err
		}

		FBlockSlice = append(FBlockSlice, FBlock)

	}
	iter.Release()
	err = iter.Error()

	return FBlockSlice, nil
}
func ParseFactoidBlock(chainID, hash string, rawBlock []byte, blockTime string) (*Block, error) {
	answer := new(Block)

	fBlock := new(block.FBlock)
	_, err := fBlock.UnmarshalBinaryData(rawBlock)
	if err != nil {
		return nil, err
	}

	answer.ChainID = chainID
	answer.PartialHash = fBlock.GetHash().String()
	answer.FullHash = fBlock.GetLedgerKeyMR().String()
	answer.PrevBlockHash = fmt.Sprintf("%x", fBlock.PrevKeyMR.Bytes())

	transactions := fBlock.GetTransactions()
	answer.EntryCount = len(transactions)
	answer.EntryList = make([]*Entry, answer.EntryCount)
	answer.BinaryString = fmt.Sprintf("%x", rawBlock)
	for i, v := range transactions {
		entry := new(Entry)
		bin, err := v.MarshalBinary()

		if err != nil {
			return nil, err
		}

		entry.BinaryString = fmt.Sprintf("%x", bin)
		entry.Timestamp = TimestampToString(v.GetMilliTimestamp() / 1000)
		entry.Hash = v.GetHash().String()
		entry.ChainID = chainID

		entry.JSONString, err = v.JSONString()
		if err != nil {
			return nil, err
		}
		entry.SpewString = v.Spew()

		answer.EntryList[i] = entry
	}
	answer.JSONString, err = fBlock.JSONString()
	if err != nil {
		return nil, err
	}
	answer.SpewString = fBlock.Spew()
	answer.IsFactoidBlock = true

	return answer, nil
}
Beispiel #3
0
func Test_create_block(test *testing.T) {
	w := new(wallet.SCWallet) // make me a wallet
	w.Init()
	w.NewSeed([]byte("slfkjasdlfjasflajsfl"))
	scb := block.NewFBlock(1000, 0)
	cb := w.CreateTransaction(uint64(time.Now().UnixNano() / 1000000))
	scb.AddCoinbase(cb)

	for i := 0; i < 3; i++ {
		h0, err := w.GenerateFctAddress([]byte("test "+cv.Itoa(i)+"-0"), 1, 1)
		if err != nil {
			sc.Prtln("Error 1")
			test.Fail()
		}
		h1, err := w.GenerateFctAddress([]byte("test "+cv.Itoa(i)+"-1"), 1, 1)
		if err != nil {
			sc.Prtln("Error 2")
			test.Fail()
		}
		h2, err := w.GenerateFctAddress([]byte("test "+cv.Itoa(i)+"-2"), 1, 1)
		if err != nil {
			sc.Prtln("Error 3")
			test.Fail()
		}
		h3, err := w.GenerateFctAddress([]byte("test "+cv.Itoa(i)+"-3"), 1, 1)
		if err != nil {
			sc.Prtln("Error 4")
			test.Fail()
		}
		h4, err := w.GenerateFctAddress([]byte("test "+cv.Itoa(i)+"-4"), 1, 1)
		if err != nil {
			sc.Prtln("Error 5")
			test.Fail()
		}
		h5, err := w.GenerateFctAddress([]byte("test "+cv.Itoa(i)+"-5"), 1, 1)
		if err != nil {
			sc.Prtln("Error 6")
			test.Fail()
		}

		t := w.CreateTransaction(uint64(time.Now().UnixNano() / 1000000))

		w.AddInput(t, h1, 1000000)
		w.AddInput(t, h2, 1000000)
		w.AddOutput(t, h3, 1000000)
		w.AddOutput(t, h4, 500000)
		w.AddECOutput(t, h5, 500000)
		w.AddInput(t, h0, 0)
		fee, err := t.CalculateFee(1000)
		w.UpdateInput(t, 2, h0, fee)

		signed, err := w.SignInputs(t)
		if err != nil {
			sc.Prtln("Error found: ", err)
			test.Fail()
			return
		}
		if !signed {
			sc.Prtln("Not valid")
			test.Fail()
			return
		}

		err = scb.AddTransaction(t)
		if err != nil {
			sc.Prtln("Error found: ", err)
			test.Fail()
			return
		}
	}
	data, err := scb.MarshalBinary()
	if err != nil {
		fmt.Println(err)
		test.Fail()
		return
	}
	scb2 := new(block.FBlock)
	_, err = scb2.UnmarshalBinaryData(data)

	fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", scb2)

	if err != nil {
		fmt.Println(err)
		test.Fail()
		return
	}
	//sc.Prtln("FIRST\n",scb,"SECOND\n",scb2)
	if scb.IsEqual(scb2) != nil {
		fmt.Println(err)
		test.Fail()
		return
	}

}