func processTableRequest(db *DB, request []byte) ([]byte, error) {

	var response []byte
	var serializedDestination []byte
	var destination *destination

	//Validate the length of the request
	if len(request) != messageTypeSize+tableRequestHeaderSize {
		return nil, errors.New("Invalid table request size")
	}

	//Extract the type of message
	requestTypeBytes := request[:2]
	requestType := binary.BigEndian.Uint16(requestTypeBytes)

	//Validate the type of message
	if requestType != tableRequestType {
		return nil, errors.New("Invalid table request message type")
	}

	//Read the starting block the peer asked for (uint64 - 8 bytes)
	startingBlockBytes := request[2:10]
	startingBlock := binary.BigEndian.Uint64(startingBlockBytes)

	if startingBlock < genesisBlock {
		return nil, errors.New("Starting block is too old")
	}

	//Serialize message type and add it to the response
	responseTypeBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(responseTypeBytes, tableResponseType)
	response = append(response, responseTypeBytes...)

	//Get the routing entries to be sent and
	//transform them into hops so they can be shared with the peer
	routingEntries := db.getLastRoutingEntries(startingBlock)
	entriesCount := len(routingEntries)

	//Serialize routing message count and add it to the response
	entriesCountBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(entriesCountBytes, uint16(entriesCount))
	response = append(response, entriesCountBytes...)

	for _, entry := range routingEntries {
		destination = routingEntryToDestination(entry)
		serializedDestination = serializeDestination(destination)
		response = append(response, serializedDestination...)
		log.Println("Table response adding destination:", destination)
	}

	return response, nil
}
// Generates the response for a certain table request
Generates the response for a certain table request
// Generates the response for a certain table request
func processTableRequest(db *DB, request []byte) ([]byte, error) {
	var response []byte
	var serializedDestination []byte
	var destination *destination
	if len(request) != messageTypeSize+tableRequestHeaderSize {
		return nil, errors.New("Invalid table request size")
	}
	requestTypeBytes := request[:2]
	requestType := binary.BigEndian.Uint16(requestTypeBytes)
	if requestType != tableRequestType {
		return nil, errors.New("Invalid table request message type")
	}
	startingBlockBytes := request[2:10]
	startingBlock := binary.BigEndian.Uint64(startingBlockBytes)
	if startingBlock < genesisBlock {
		return nil, errors.New("Starting block is too old")
	}
	responseTypeBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(responseTypeBytes, tableResponseType)
	response = append(response, responseTypeBytes...)
	routingEntries := db.getLastRoutingEntries(startingBlock)
	entriesCount := len(routingEntries)
	entriesCountBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(entriesCountBytes, uint16(entriesCount))
	response = append(response, entriesCountBytes...)
	for _, entry := range routingEntries {
		destination = routingEntryToDestination(entry)
		serializedDestination = serializeDestination(destination)
		response = append(response, serializedDestination...)
		log.Println("Table response adding destination:", destination)
	}
	return response, nil
}
func processTableRequest ( db * DB , request [ ] byte ) ( [ ] byte , error ) { var response [ ] byte var serializedDestination [ ] byte var destination * destination if len ( request ) != messageTypeSize + tableRequestHeaderSize { return nil , errors . New ( " " ) } requestTypeBytes := request [ : 2 ] requestType := binary . BigEndian . Uint16 ( requestTypeBytes ) if requestType != tableRequestType { return nil , errors . New ( " " ) } startingBlockBytes := request [ 2 : 10 ] startingBlock := binary . BigEndian . Uint64 ( startingBlockBytes ) if startingBlock < genesisBlock { return nil , errors . New ( " " ) } responseTypeBytes := make ( [ ] byte , 2 ) binary . BigEndian . PutUint16 ( responseTypeBytes , tableResponseType ) response = append ( response , responseTypeBytes ... ) routingEntries := db . getLastRoutingEntries ( startingBlock ) entriesCount := len ( routingEntries ) entriesCountBytes := make ( [ ] byte , 2 ) binary . BigEndian . PutUint16 ( entriesCountBytes , uint16 ( entriesCount ) ) response = append ( response , entriesCountBytes ... ) for _ , entry := range routingEntries { destination = routingEntryToDestination ( entry ) serializedDestination = serializeDestination ( destination ) response = append ( response , serializedDestination ... ) log . Println ( " " , destination ) } return response , nil }
Generates the response for a certain table request
Generates the response for a certain table request
//Validate the length of the request //Extract the type of message //Validate the type of message //Read the starting block the peer asked for (uint64 - 8 bytes) //Serialize message type and add it to the response //Get the routing entries to be sent and //transform them into hops so they can be shared with the peer //Serialize routing message count and add it to the response