anchor.diff:  %% A piece enters the valid state when it has been marked as valid.
   %% A piece may noy leave the valid state.
   %%
  +%% # Chunk sets
  +%% ## Assigned chunks
  +%% A set of chunks that has not yet been assigned to a peer is
  +%% associated with each piece. When this set is empty the piece
  +%% enters the assigned state.
  +%%
  +%% ## Stored chunks
  +%% A set of chunks that has not yet been written to disk is associated
  +%% with each piece. When this set is empty the piece enters the stored
  +%% state. 
  +%%
   
   -spec register_chunk_server(torrent_id()) -> true.
   register_chunk_server(TorrentID) ->
  
positive.code:     %% piece has been marked as stored.
      #state{
          pieces_valid=Valid,
          piece_chunks=PieceChunks} = State,
      IsValid = etorrent_pieceset:is_member(Index, Valid),
      Chunks = array:get(Index, PieceChunks),
      BytesLeft = etorrent_chunkset:size(Chunks),
  
negative.code:         %% None that we are not already downloading
          %% but one ore more that we would want to download
          {0, N} ->
              etorrent_pieceset:min(SubOptimal);
          %% One or more that we are already downloading
          {N, _} ->
              etorrent_pieceset:min(Optimal)
      end,
  
      case PieceIndex of
          none ->
              {reply, {error, not_interested}, State};
          Index ->
              Chunkset = array:get(Index, AssignedChunks),
              {Offs, Len} = etorrent_chunkset:min(Chunkset),
              NewChunkset = etorrent_chunkset:delete(Offs, Len, Chunkset),
              NewAssignedChunks = array:set(Index, NewChunkset, AssignedChunks),
              NewBegun = etorrent_pieceset:insert(Index, Begun),
              %% Add the chunk to this peer's set of open requests
              OpenReqs = etorrent_monitorset:fetch(PeerPid, Peers),
              NewReqs  = gb_trees:insert({Index, Offs}, Len, OpenReqs),
              NewPeers = etorrent_monitorset:update(PeerPid, NewReqs, Peers),
              %% The piece is in the assigned state if this was the last chunk
              NewAssigned = case etorrent_chunkset:size(NewChunkset) of
                  0 -> etorrent_pieceset:insert(Index, Assigned);
                  _ -> Assigned
              end,
  
              NewState = State#state{
                  pieces_begun=NewBegun,
                  pieces_assigned=NewAssigned,
                  chunks_assigned=NewAssignedChunks,
                  peer_monitors=NewPeers},
              {reply, {ok, [{PieceIndex, Offs, Len}]}, NewState}
      end;
  