func NetworkProcessor(state interfaces.IState) { netloop: for { // This loop looks at the input queues and the invalid queues and // Handles messages as they come in. If nothing is read, it sleeps // for 500 milliseconds. Note you only sleep if both queues test // to be empty. select { case msg, ok := <-state.NetworkInMsgQueue(): if ok { //log.Printf("NetworkIn: %s\n", spew.Sdump(msg)) if state.PrintType(msg.Type()) { log.Printf("Ignored: NetworkIn: %s\n", msg.String()) } state.InMsgQueue() <- msg continue netloop } default: } select { case msg, ok := <-state.NetworkOutMsgQueue(): if ok { var _ = msg //log.Printf("NetworkOut: %s\n", msg.String()) if state.PrintType(msg.Type()) { log.Printf("Ignored: NetworkOut: %s\n", msg.String()) } switch msg.(type) { case *messages.EOM: msgeom := msg.(*messages.EOM) server := state.GetServer().(*btcd.Server) server.BroadcastMessage(msgeom) default: } continue netloop } default: } select { case msg, ok := <-state.NetworkInvalidMsgQueue(): if ok { var _ = msg if state.PrintType(msg.Type()) { log.Printf("%20s %s\n", "Invalid:", msg.String()) } continue netloop } default: } time.Sleep(time.Duration(500) * time.Millisecond) } }
func (m *DBStateMissing) FollowerExecute(state interfaces.IState) { if len(state.NetworkOutMsgQueue()) > 1000 { return } // TODO: Likely need to consider a limit on how many blocks we reply with. For now, // just give them what they ask for. start := m.DBHeightStart end := m.DBHeightEnd if end-start > 200 { end = start + 200 } missingone := false for dbs := start; dbs <= end; dbs++ { msg, err := state.LoadDBState(dbs) if msg != nil && err == nil { if missingone { fmt.Println("dddd Missing some prior dbstates. Start", start, "End", end, "Found ", dbs) } // If I don't have this block, ignore. msg.SetOrigin(m.GetOrigin()) msg.SetNetworkOrigin(m.GetNetworkOrigin()) msg.SendOut(state, msg) state.IncDBStateAnswerCnt() } else { missingone = true } } return }
// Execute the leader functions of the given message func (m *EOM) LeaderExecute(state interfaces.IState) error { DBM := NewDirectoryBlockSignature() DBM.DirectoryBlockKeyMR = state.GetPreviousDirectoryBlock().GetKeyMR() DBM.Sign(state) state.NetworkOutMsgQueue() <- DBM state.InMsgQueue() <- DBM return nil }
// Try and Resend. Return true if we should keep the message, false if we should give up. func (m *MessageBase) Resend(s interfaces.IState) (rtn bool) { now := s.GetTimestamp().GetTimeMilli() if m.resend == 0 { m.resend = now return false } if now-m.resend > 10000 && len(s.NetworkOutMsgQueue()) < 1000 { // Resend every second m.resend = now return true } return false }
func (m *MissingEntryBlocks) FollowerExecute(state interfaces.IState) { if len(state.NetworkOutMsgQueue()) > 1000 { return } start := m.DBHeightStart end := m.DBHeightEnd if end-start > 20 { end = start + 20 } db := state.GetAndLockDB() defer state.UnlockDB() resp := NewEntryBlockResponse(state).(*EntryBlockResponse) for i := start; i <= end; i++ { dblk, err := db.FetchDBlockByHeight(i) if err != nil { return } if dblk == nil { return } for _, v := range dblk.GetDBEntries() { if v.GetChainID().IsMinuteMarker() == true { continue } eBlock, err := db.FetchEBlock(v.GetKeyMR()) if err != nil { return } resp.EBlocks = append(resp.EBlocks, eBlock) for _, v := range eBlock.GetBody().GetEBEntries() { entry, err := db.FetchEntry(v) if err != nil { return } resp.Entries = append(resp.Entries, entry) } } } resp.SetOrigin(m.GetOrigin()) resp.SetNetworkOrigin(m.GetNetworkOrigin()) resp.SendOut(state, resp) state.IncDBStateAnswerCnt() return }
func (m *EntryBlockResponse) FollowerExecute(state interfaces.IState) { if len(state.NetworkOutMsgQueue()) > 1000 { return } db := state.GetAndLockDB() defer state.UnlockDB() for _, v := range m.EBlocks { db.ProcessEBlockBatchWithoutHead(v, true) } for _, v := range m.Entries { db.InsertEntry(v) } return }
// Execute the leader functions of the given message func (m *FactoidTransaction) LeaderExecute(state interfaces.IState) error { if err := state.GetFactoidState().Validate(1, m.Transaction); err != nil { return err } b, err := m.Transaction.MarshalBinarySig() if err != nil { return err } msg, err := NewAck(state, primitives.Sha(b)) if err != nil { return err } state.NetworkOutMsgQueue() <- msg state.FollowerInMsgQueue() <- m // Send factoid trans to follower state.FollowerInMsgQueue() <- msg // Send the Ack to follower return nil }
// Execute the leader functions of the given message func (m *RevealEntryMsg) LeaderExecute(state interfaces.IState) error { v := m.Validate(state) if v <= 0 { return fmt.Errorf("Reveal is no longer valid") } b := m.GetHash() msg, err := NewAck(state, b) if err != nil { return err } state.NetworkOutMsgQueue() <- msg state.FollowerInMsgQueue() <- m // Send factoid trans to follower state.FollowerInMsgQueue() <- msg // Send the Ack to follower return nil }
func resend(state interfaces.IState, msg interfaces.IMsg, cnt int, delay int) { for i := 0; i < cnt; i++ { state.NetworkOutMsgQueue() <- msg time.Sleep(time.Duration(delay) * time.Second) } }