Пример #1
0
func TestEBlockMarshal(t *testing.T) {
	t.Logf("\n---\nTestEBlockMarshal\n---\n")

	// build an EBlock for testing
	eb := common.NewEBlock()
	eb.Header.ChainID.SetBytes(byteof(0x11))
	eb.Header.BodyMR.SetBytes(byteof(0x22))
	eb.Header.PrevKeyMR.SetBytes(byteof(0x33))
	eb.Header.PrevLedgerKeyMR.SetBytes(byteof(0x44))
	eb.Header.EBSequence = 5
	eb.Header.EBHeight = 6
	eb.Header.EntryCount = 7
	ha := common.NewHash()
	ha.SetBytes(byteof(0xaa))
	hb := common.NewHash()
	hb.SetBytes(byteof(0xbb))
	eb.Body.EBEntries = append(eb.Body.EBEntries, ha)
	eb.AddEndOfMinuteMarker(0xcc)
	eb.Body.EBEntries = append(eb.Body.EBEntries, hb)

	t.Log(eb)
	p, err := eb.MarshalBinary()
	if err != nil {
		t.Error(err)
	}

	eb2 := common.NewEBlock()
	if err := eb2.UnmarshalBinary(p); err != nil {
		t.Error(err)
	}
	t.Log(eb2)
	p2, err := eb2.MarshalBinary()
	if err != nil {
		t.Error(err)
	}

	if string(p) != string(p2) {
		t.Logf("eb1 = %x\n", p)
		t.Logf("eb2 = %x\n", p2)
		t.Fail()
	}
}
Пример #2
0
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgEBlock) BtcDecode(r io.Reader, pver uint32) error {

	bytes, err := readVarBytes(r, pver, uint32(MaxBlockMsgPayload), CmdEBlock)
	if err != nil {
		return err
	}

	msg.EBlk = common.NewEBlock()
	err = msg.EBlk.UnmarshalBinary(bytes)
	if err != nil {
		return err
	}

	return nil
}
Пример #3
0
// FetchEntryBlock gets an entry by hash from the database.
func (db *LevelDb) FetchEBlockByHash(eBlockHash *common.Hash) (*common.EBlock, error) {
	var key []byte = []byte{byte(TBL_EB)}
	key = append(key, eBlockHash.Bytes()...)
	db.dbLock.RLock()
	data, err := db.lDb.Get(key, db.ro)
	db.dbLock.RUnlock()
	if err != nil {
		return nil, err
	}

	eBlock := common.NewEBlock()
	if data != nil {
		_, err := eBlock.UnmarshalBinaryData(data)
		if err != nil {
			return nil, err
		}
	}
	return eBlock, nil
}
Пример #4
0
// FetchAllEBlocksByChain gets all of the blocks by chain id
func (db *LevelDb) FetchAllEBlocksByChain(chainID *common.Hash) (eBlocks *[]common.EBlock, err error) {
	db.dbLock.RLock()
	defer db.dbLock.RUnlock()

	var fromkey []byte = []byte{byte(TBL_EB_CHAIN_NUM)} // Table Name (1 bytes)
	fromkey = append(fromkey, chainID.Bytes()...)       // Chain Type (32 bytes)
	var tokey []byte = addOneToByteArray(fromkey)

	eBlockSlice := make([]common.EBlock, 0, 10)

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

	for iter.Next() {
		eBlockHash := common.NewHash()
		_, err := eBlockHash.UnmarshalBinaryData(iter.Value())
		if err != nil {
			return nil, err
		}

		var key []byte = []byte{byte(TBL_EB)}
		key = append(key, eBlockHash.Bytes()...)
		data, err := db.lDb.Get(key, db.ro)
		if err != nil {
			return nil, err
		}

		eBlock := common.NewEBlock()
		if data != nil {
			_, err := eBlock.UnmarshalBinaryData(data)
			if err != nil {
				return nil, err
			}
			eBlockSlice = append(eBlockSlice, *eBlock)
		}
	}
	iter.Release()
	err = iter.Error()

	return &eBlockSlice, nil
}
func ParseEntryBlock(chainID, hash string, rawBlock []byte, blockTime string) (*Block, error) {
	Log("ParseEntryBlock - %x", rawBlock)
	answer := new(Block)

	eBlock := common.NewEBlock()
	_, err := eBlock.UnmarshalBinaryData(rawBlock)
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}

	answer.ChainID = chainID
	h, err := eBlock.KeyMR()
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}
	answer.PartialHash = h.String()
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}
	h, err = eBlock.Hash()
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}
	answer.FullHash = h.String()

	answer.PrevBlockHash = eBlock.Header.PrevKeyMR.String()

	answer.EntryCount = 0
	answer.EntryList = []*Entry{}
	answer.BinaryString = fmt.Sprintf("%x", rawBlock)

	answer.JSONString, err = eBlock.JSONString()
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}

	Log("Block - %v", answer.JSONString)
	lastMinuteMarkedEntry := 0
	for _, v := range eBlock.Body.EBEntries {
		if IsMinuteMarker(v.String()) {
			for i := lastMinuteMarkedEntry; i < len(answer.EntryList); i++ {
				answer.EntryList[i].MinuteMarker = v.String()
			}
			lastMinuteMarkedEntry = len(answer.EntryList)
		} else {
			entry, err := FetchAndParseEntry(v.String(), blockTime, IsHashZeroes(answer.PrevBlockHash) && answer.EntryCount == 0)
			if err != nil {
				Log("Error - %v", err)
				return nil, err
			}
			answer.EntryCount++
			answer.EntryList = append(answer.EntryList, entry)
		}
	}
	answer.SpewString = eBlock.Spew()

	answer.IsEntryBlock = true

	return answer, nil
}