// FetchChainByHash gets a chain by chainID func (db *LevelDb) FetchChainByHash(chainID *common.Hash) (*common.EChain, error) { var key []byte = []byte{byte(TBL_CHAIN_HASH)} key = append(key, chainID.Bytes()...) db.dbLock.RLock() data, err := db.lDb.Get(key, db.ro) db.dbLock.RUnlock() if err != nil { return nil, err } chain := common.NewEChain() if data != nil { _, err := chain.UnmarshalBinaryData(data) if err != nil { return nil, err } } return chain, nil }
// FetchAllChains get all of the cahins func (db *LevelDb) FetchAllChains() (chains []*common.EChain, err error) { db.dbLock.RLock() defer db.dbLock.RUnlock() var fromkey []byte = []byte{byte(TBL_CHAIN_HASH)} // Table Name (1 bytes) var tokey []byte = []byte{byte(TBL_CHAIN_HASH + 1)} // Table Name (1 bytes) chainSlice := make([]*common.EChain, 0, 10) iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro) for iter.Next() { chain := common.NewEChain() _, err := chain.UnmarshalBinaryData(iter.Value()) if err != nil { return nil, err } chainSlice = append(chainSlice, chain) } iter.Release() err = iter.Error() return chainSlice, err }
// processRevealEntry validates the MsgRevealEntry and adds it to processlist func processRevealEntry(msg *wire.MsgRevealEntry) error { e := msg.Entry bin, _ := e.MarshalBinary() h, _ := wire.NewShaHash(e.Hash().Bytes()) // Check if the chain id is valid if e.ChainID.IsSameAs(zeroHash) || e.ChainID.IsSameAs(dchain.ChainID) || e.ChainID.IsSameAs(achain.ChainID) || e.ChainID.IsSameAs(ecchain.ChainID) || e.ChainID.IsSameAs(fchain.ChainID) { return fmt.Errorf("This entry chain is not supported: %s", e.ChainID.String()) } if c, ok := commitEntryMap[e.Hash().String()]; ok { if chainIDMap[e.ChainID.String()] == nil { fMemPool.addOrphanMsg(msg, h) return fmt.Errorf("This chain is not supported: %s", msg.Entry.ChainID.String()) } // Calculate the entry credits required for the entry cred, err := util.EntryCost(bin) if err != nil { return err } if c.Credits < cred { fMemPool.addOrphanMsg(msg, h) return fmt.Errorf("Credit needs to paid first before an entry is revealed: %s", e.Hash().String()) } // Add the msg to the Mem pool fMemPool.addMsg(msg, h) // Add to MyPL if Server Node if nodeMode == common.SERVER_NODE { if plMgr.IsMyPListExceedingLimit() { procLog.Warning("Exceeding MyProcessList size limit!") return fMemPool.addOrphanMsg(msg, h) } ack, err := plMgr.AddMyProcessListItem(msg, h, wire.ACK_REVEAL_ENTRY) if err != nil { return err } else { // Broadcast the ack to the network if no errors outMsgQueue <- ack } } delete(commitEntryMap, e.Hash().String()) return nil } else if c, ok := commitChainMap[e.Hash().String()]; ok { //Reveal chain --------------------------- if chainIDMap[e.ChainID.String()] != nil { fMemPool.addOrphanMsg(msg, h) return fmt.Errorf("This chain is not supported: %s", msg.Entry.ChainID.String()) } // add new chain to chainIDMap newChain := common.NewEChain() newChain.ChainID = e.ChainID newChain.FirstEntry = e chainIDMap[e.ChainID.String()] = newChain // Calculate the entry credits required for the entry cred, err := util.EntryCost(bin) if err != nil { return err } // 10 credit is additional for the chain creation if c.Credits < cred+10 { fMemPool.addOrphanMsg(msg, h) return fmt.Errorf("Credit needs to paid first before an entry is revealed: %s", e.Hash().String()) } //validate chain id for the first entry expectedChainID := common.NewChainID(e) if !expectedChainID.IsSameAs(e.ChainID) { return fmt.Errorf("Invalid ChainID for entry: %s", e.Hash().String()) } //validate chainid hash in the commitChain chainIDHash := common.DoubleSha(e.ChainID.Bytes()) if !bytes.Equal(c.ChainIDHash.Bytes()[:], chainIDHash[:]) { return fmt.Errorf("RevealChain's chainid hash does not match with CommitChain: %s", e.Hash().String()) } //validate Weld in the commitChain weld := common.DoubleSha(append(c.EntryHash.Bytes(), e.ChainID.Bytes()...)) if !bytes.Equal(c.Weld.Bytes()[:], weld[:]) { return fmt.Errorf("RevealChain's weld does not match with CommitChain: %s", e.Hash().String()) } // Add the msg to the Mem pool fMemPool.addMsg(msg, h) // Add to MyPL if Server Node if nodeMode == common.SERVER_NODE { if plMgr.IsMyPListExceedingLimit() { procLog.Warning("Exceeding MyProcessList size limit!") return fMemPool.addOrphanMsg(msg, h) } ack, err := plMgr.AddMyProcessListItem(msg, h, wire.ACK_REVEAL_CHAIN) if err != nil { return err } else { // Broadcast the ack to the network if no errors outMsgQueue <- ack } } delete(commitChainMap, e.Hash().String()) return nil } else { return fmt.Errorf("No commit for entry") } return nil }