func TestLoadUnknownEntries(t *testing.T) { dbo := NewOverlay(new(mapdb.MapDB)) defer dbo.Close() for i := 0; i < 10; i++ { b := IntToByteSlice(i) hash, err := primitives.NewShaHash(b) if err != nil { t.Error(err) } data, err := dbo.FetchEntry(hash) if err != nil { t.Error(err) } if data != nil { t.Error("Fetched entry while we expected nil - %v", data) } all, err := dbo.FetchAllEntriesByChainID(hash) if err != nil { t.Error(err) } if len(all) != 0 { t.Error("Fetched entries while we expected nil - %v", all) } } }
func HandleV2EntryCreditBalance(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) { ecadr := new(AddressRequest) err := MapToObject(params, ecadr) if err != nil { return nil, NewInvalidParamsError() } var adr []byte if primitives.ValidateECUserStr(ecadr.Address) { adr = primitives.ConvertUserStrToAddress(ecadr.Address) } else { adr, err = hex.DecodeString(ecadr.Address) if err == nil && len(adr) != constants.HASH_LENGTH { return nil, NewInvalidAddressError() } if err != nil { return nil, NewInvalidAddressError() } } if len(adr) != constants.HASH_LENGTH { return nil, NewInvalidAddressError() } address, err := primitives.NewShaHash(adr) if err != nil { return nil, NewInvalidAddressError() } resp := new(EntryCreditBalanceResponse) resp.Balance = state.GetFactoidState().GetECBalance(address.Fixed()) return resp, nil }
func (db *Overlay) FetchAllBlockKeysFromBucket(bucket []byte) ([]interfaces.IHash, error) { entries, err := db.ListAllKeys(bucket) if err != nil { return nil, err } answer := make([]interfaces.IHash, len(entries)) for i := range entries { answer[i], err = primitives.NewShaHash(entries[i]) if err != nil { return nil, err } } return answer, nil }
func (db *Overlay) FetchAllEntryIDs() ([]interfaces.IHash, error) { ids, err := db.ListAllKeys(ENTRY) if err != nil { return nil, err } entries := []interfaces.IHash{} for _, id := range ids { h, err := primitives.NewShaHash(id) if err != nil { return nil, err } entries = append(entries, h) } return entries, nil }
func createSmallTestAdminHeader() *ABlockHeader { header := new(ABlockHeader) p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") hash, _ := primitives.NewShaHash(p) header.PrevBackRefHash = hash header.DBHeight = 123 header.HeaderExpansionSize = 0 header.HeaderExpansionArea = []byte{} header.MessageCount = 234 header.BodySize = 345 return header }
func (db *Overlay) FetchAllEBlockChainIDs() ([]interfaces.IHash, error) { ids, err := db.ListAllKeys(ENTRYBLOCK) if err != nil { return nil, err } entries := []interfaces.IHash{} for _, id := range ids { h, err := primitives.NewShaHash(id) if err != nil { return nil, err } str := h.String() if strings.Contains(str, "000000000000000000000000000000000000000000000000000000000000000") { //skipping basic blocks continue } entries = append(entries, h) } return entries, nil }
func TestLoadUnknownDBlocks(t *testing.T) { dbo := NewOverlay(new(mapdb.MapDB)) defer dbo.Close() for i := 0; i < 10; i++ { b := IntToByteSlice(i) hash, err := primitives.NewShaHash(b) if err != nil { t.Error(err) } data, err := dbo.FetchDBlockByHash(hash) if err != nil { t.Error(err) } if data != nil { t.Error("Fetched entry while we expected nil - %v", data) } data, err = dbo.FetchDBlockByKeyMR(hash) if err != nil { t.Error(err) } if data != nil { t.Error("Fetched entry while we expected nil - %v", data) } data, err = dbo.FetchDirectoryBlockHead() if err != nil { t.Error(err) } if data != nil { t.Error("Fetched entry while we expected nil - %v", data) } all, err := dbo.FetchAllDBlocks() if err != nil { t.Error(err) } if len(all) != 0 { t.Error("Fetched entries while we expected nil - %v", all) } } }
func TestSaveLoadEBlockChain(t *testing.T) { blocks := []*EBlock{} max := 10 var prev *EBlock = nil dbo := NewOverlay(new(mapdb.MapDB)) defer dbo.Close() for i := 0; i < max; i++ { prev, _ = CreateTestEntryBlock(prev) blocks = append(blocks, prev) err := dbo.SaveEBlockHead(prev, false) if err != nil { t.Error(err) } } chain, err := primitives.NewShaHash(prev.GetChainID().Bytes()) if err != nil { t.Error(err) } current, err := dbo.FetchEBlockHead(chain) if err != nil { t.Error(err) } zero := primitives.NewZeroHash() fetchedCount := 1 for { keyMR := current.(*EBlock).GetHeader().GetPrevKeyMR() if keyMR.IsSameAs(zero) { break } //t.Logf("KeyMR - %v", keyMR.String()) hash := current.GetHeader().GetPrevFullHash() current, err = dbo.FetchEBlockByPrimary(keyMR) if err != nil { t.Error(err) } if current == nil { t.Fatal("Block not found") } fetchedCount++ byHash, err := dbo.FetchEBlockBySecondary(hash) same, err := primitives.AreBinaryMarshallablesEqual(current, byHash) if err != nil { t.Error(err) } if same == false { t.Error("Blocks fetched by keyMR and hash are not identical") } } if fetchedCount != max { t.Errorf("Wrong number of entries fetched - %v vs %v", fetchedCount, max) } all, err := dbo.FetchAllEBlocksByChain(chain) if err != nil { t.Error(err) } if len(all) != max { t.Errorf("Wrong number of entries fetched - %v vs %v", len(all), max) } for i := range all { same, err := primitives.AreBinaryMarshallablesEqual(blocks[i], all[i]) if err != nil { t.Error(err) } if same == false { t.Error("Blocks fetched by all and original blocks are not identical") t.Logf("\n%v\nvs\n%v", blocks[i].String(), all[i].String()) } } }
func TestSaveLoadEBlockHead(t *testing.T) { b1, _ := CreateTestEntryBlock(nil) chain, err := primitives.NewShaHash(b1.GetChainID().Bytes()) if err != nil { t.Error(err) } dbo := NewOverlay(new(mapdb.MapDB)) defer dbo.Close() err = dbo.SaveEBlockHead(b1, false) if err != nil { t.Error(err) } head, err := dbo.FetchEBlockHead(chain) if err != nil { t.Error(err) } if head == nil { t.Error("DBlock head is nil") } m1, err := b1.MarshalBinary() if err != nil { t.Error(err) } m2, err := head.MarshalBinary() if err != nil { t.Error(err) } if primitives.AreBytesEqual(m1, m2) == false { t.Error("Blocks are not equal") } b2, _ := CreateTestEntryBlock(b1) err = dbo.SaveEBlockHead(b2, false) if err != nil { t.Error(err) } head, err = dbo.FetchEBlockHead(chain) if err != nil { t.Error(err) } if head == nil { t.Error("DBlock head is nil") } m1, err = b2.MarshalBinary() if err != nil { t.Error(err) } m2, err = head.MarshalBinary() if err != nil { t.Error(err) } if primitives.AreBytesEqual(m1, m2) == false { t.Error("Blocks are not equal") } }
func CreateTestBlockSet(prev *BlockSet) *BlockSet { var err error height := 0 if prev != nil { height = prev.Height + 1 } if prev == nil { prev = new(BlockSet) } answer := new(BlockSet) answer.Height = height dbEntries := []interfaces.IDBEntry{} answer.ABlock = CreateTestAdminBlock(prev.ABlock) de := new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.ABlock.GetChainID()) if err != nil { panic(err) } de.KeyMR, err = answer.ABlock.GetKeyMR() if err != nil { panic(err) } dbEntries = append(dbEntries, de) answer.FBlock = CreateTestFactoidBlock(prev.FBlock) de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.FBlock.GetChainID()) if err != nil { panic(err) } de.KeyMR = answer.FBlock.GetKeyMR() dbEntries = append(dbEntries, de) answer.EBlock, answer.Entries = CreateTestEntryBlock(prev.EBlock) de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.EBlock.GetChainID()) if err != nil { panic(err) } de.KeyMR, err = answer.EBlock.KeyMR() if err != nil { panic(err) } dbEntries = append(dbEntries, de) anchor, entries := CreateTestAnchorEntryBlock(prev.AnchorEBlock, prev.DBlock) answer.AnchorEBlock = anchor answer.Entries = append(answer.Entries, entries...) de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.AnchorEBlock.GetChainID()) if err != nil { panic(err) } de.KeyMR, err = answer.AnchorEBlock.KeyMR() if err != nil { panic(err) } dbEntries = append(dbEntries, de) answer.ECBlock = CreateTestEntryCreditBlock(prev.ECBlock) ecEntries := createECEntriesfromBlocks(answer.FBlock, []*entryBlock.EBlock{answer.EBlock, answer.AnchorEBlock}, height) answer.ECBlock.GetBody().SetEntries(ecEntries) de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.ECBlock.GetChainID()) if err != nil { panic(err) } de.KeyMR, err = answer.ECBlock.HeaderHash() if err != nil { panic(err) } dbEntries = append(dbEntries, de) answer.DBlock = CreateTestDirectoryBlock(prev.DBlock) answer.DBlock.SetDBEntries(dbEntries) return answer }
func CreateTestBlockSetForFER(prev *BlockSet, db *databaseOverlay.Overlay, testEntries []FEREntryWithHeight) *BlockSet { var err error height := 0 if prev != nil { height = prev.Height + 1 } if prev == nil { prev = newBlockSet() } answer := new(BlockSet) answer.Height = height dbEntries := []interfaces.IDBEntry{} //ABlock answer.ABlock = CreateTestAdminBlock(prev.ABlock) de := new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.ABlock.GetChainID().Bytes()) if err != nil { panic(err) } de.KeyMR, err = answer.ABlock.GetKeyMR() if err != nil { panic(err) } dbEntries = append(dbEntries, de) //FBlock answer.FBlock = CreateTestFactoidBlock(prev.FBlock) de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.FBlock.GetChainID().Bytes()) if err != nil { panic(err) } de.KeyMR = answer.FBlock.GetKeyMR() dbEntries = append(dbEntries, de) //EBlock answer.EBlock, answer.Entries = CreateTestEntryBlockForFER(prev.EBlock, uint32(height)) // Loop through the passed FEREntries and see which ones need to go into this EBlock for _, testEntry := range testEntries { if testEntry.Height == uint32(height) { answer.EBlock.AddEBEntry(testEntry.AnFEREntry) // db.InsertEntry(testEntry.AnFEREntry) // I don't think I need this } } de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.EBlock.GetChainID().Bytes()) if err != nil { panic(err) } de.KeyMR, err = answer.EBlock.KeyMR() if err != nil { panic(err) } dbEntries = append(dbEntries, de) //Anchor EBlock anchor, entries := CreateTestAnchorEntryBlock(prev.AnchorEBlock, prev.DBlock) answer.AnchorEBlock = anchor answer.Entries = append(answer.Entries, entries...) de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.AnchorEBlock.GetChainID().Bytes()) if err != nil { panic(err) } de.KeyMR, err = answer.AnchorEBlock.KeyMR() if err != nil { panic(err) } dbEntries = append(dbEntries, de) //ECBlock answer.ECBlock = CreateTestEntryCreditBlock(prev.ECBlock) ecEntries := createECEntriesfromBlocks(answer.FBlock, []*entryBlock.EBlock{answer.EBlock, answer.AnchorEBlock}, height) answer.ECBlock.GetBody().SetEntries(ecEntries) de = new(directoryBlock.DBEntry) de.ChainID, err = primitives.NewShaHash(answer.ECBlock.GetChainID().Bytes()) if err != nil { panic(err) } de.KeyMR, err = answer.ECBlock.GetFullHash() if err != nil { panic(err) } dbEntries = append(dbEntries[:1], append([]interfaces.IDBEntry{de}, dbEntries[1:]...)...) answer.DBlock = CreateTestDirectoryBlock(prev.DBlock) err = answer.DBlock.SetDBEntries(dbEntries) if err != nil { panic(err) } return answer }